B4J Library [IoT] jPi4J - Raspberry Pi GPIO controller

Updated libraries: https://www.b4x.com/android/forum/threads/pi4j2-raspberry-pi-i-o-library.136113/#content

This is a wrapper for Pi4J library.
It allows you are control the Raspberry Pi board GPIO pins.

Using this library is quite simple. You can use it from a UI app or non-UI app.

First you should initialize a GpioController object.
The second step is to initialize one or more pins.

You can provision each pin to be an input pin or output pin. Input pins allow you to listen for state changes. Output pins allow you to set their state.

The following program initializes two pins. Pin1 is an input pin and pin2 is an output pin. Pin2 state is changed every 5 seconds with a timer:
B4X:
'Non-UI application (console application)
#Region  Project Attributes
   #CommandLineArgs:
   #MergeLibraries: true
#End Region

Sub Process_Globals
   Private controller As GpioController
   Private Pin2 As GpioPinDigitalInput
   Private Pin1 As GpioPinDigitalOutput
   Private Timer1 As Timer
End Sub

Sub AppStart (Args() As String)
   controller.Initialize
   Pin1.Initialize(1, True) 'GpioPinDigitalOutput
   Pin2.Initialize("Pin2", 2) 'GpioPinDigitalInput
   Pin2.SetPinPullResistance("PULL_DOWN")

   Log("Monitoring Pin2 state")
   Timer1.Initialize("Timer1", 5000)
   Timer1.Enabled = True
   StartMessageLoop
End Sub

Sub Timer1_Tick
   Pin1.State = Not(Pin1.State)
   Log("Switching Pin1 state. Pin1 state = " & Pin1.State)
End Sub

Sub Pin2_StateChange(State As Boolean)
   Log("Pin2 StateChange event: " & State)
End Sub

The pins scheme is available here: http://pi4j.com/usage.html

The output of this program:
SS-2014-02-05_14.09.17.png


B4J-Bridge is very useful when working with this board: http://www.b4x.com/android/forum/threads/remote-debugging-with-b4j-bridge.38804/
You can download it to the board with this command:
B4X:
wget https://www.b4x.com/b4j/files/b4j-bridge.jar
Then run it as root:
B4X:
sudo <path to java> -jar b4j-bridge.jar

SS-2014-03-25_12.42.39.png



PiFace extension

V1.00 adds support for the PiFace extension: http://www.savagehomeautomation.com/piface


See this post: http://www.b4x.com/android/forum/threads/jpi4j-raspberry-pi-gpio-controller.37493/#post-232767

Download link: www.b4x.com/b4j/files/jPi4J.zip
 
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
YES - just checked again and found on my test RPi, the latest Jessie is Linux 4.9.24-v7+ .

Q regarding the library:
  • did you copy the file libdht.so to app folder or the tempjars folder (when remote testing)?
  • is there an error message logged? if so, can you share.
if the libdht.so is not in the respective RPi folder, then an error message is displayed, i.e. "Caused by: java.lang.UnsatisfiedLinkError: no dht in java.library.path"
 

Ilya G.

Active Member
Licensed User
Longtime User
Please help to convert python i2c example to b4j

B4X:
#!/usr/bin/env python
# Test program for ADC-DAC PCF8591P
# 2016 https://ph0en1x.net

import os
import time
from smbus import SMBus

DEV_ADDR = 0x48
adc_channels = {
    'AIN0':0b1000000, # 0x40 (foto-resistor)
    'AIN1':0b1000001, # 0x41 (thermistor)
    'AIN2':0b1000010, # 0x42 (not connected)
    'AIN3':0b1000011, # 0x43 (variable resistor)
}
dac_channel = 0b1000000 # 0x40

bus = SMBus(1)          # 1 for RPi model B rev.2
tmp = 0

while(1):
    os.system('clear')
    print("Press Ctrl C to stop...\n")
    for channel in adc_channels:
        # read value from ADC input
        bus.write_byte(DEV_ADDR, adc_channels[channel])
        bus.read_byte(DEV_ADDR) # read last value
        bus.read_byte(DEV_ADDR) # repeat reading last value
        value = bus.read_byte(DEV_ADDR)
        if channel == 'AIN3':
            tmp = value
        print 'Channel ' + channel + ' ---> ' + str(value)
    # set value in DAC
    bus.write_byte_data(DEV_ADDR, dac_channel, tmp)
    time.sleep(0.1)
 

rbghongade

Active Member
Licensed User
Longtime User
Earlier I copied all files and then back tracked to identify that only pi4j-core.jar made the difference.
 

vcelak

Member
Licensed User
Longtime User
Hi Erel,

I've the same issue like others - I never had before this jpi4j library - just downloaded v2.00 and used and it is:
B4X:
Unable to determine hardware version. I see: Hardware    : BCM2835
,
 - expecting BCM2708 or BCM2709.
If this is a genuine Raspberry Pi then please report this
to [email protected]. If this is not a Raspberry Pi then you
are on your own as wiringPi is designed to support the
Raspberry Pi ONLY.

I also tried to replace jar files from official b4j webserver, but result is the same...
 

derez

Expert
Licensed User
Longtime User
Using RPi3 I have exactly the same report as the post above using jPi4j ver. 1.50 or 2.00
The application worked without problems on Rpi2.
It gives the same error with the example in the first post.

I have checked and updated the wiringpi version from here http://wiringpi.com/download-and-install/

^Cpi@raspberrypi:~ $ gpio -v
gpio version: 2.44
Copyright (c) 2012-2017 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
Type: Pi 3, Revision: 02, Memory: 1024MB, Maker: Embest
* Device tree is enabled.
*--> Raspberry Pi 3 Model B Rev 1.2
* This Raspberry Pi supports user-level GPIO access.
 
Last edited:

derez

Expert
Licensed User
Longtime User
Following this link https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=182191 I "bumped" the RPI's kernel down to 4.4.49 (instead of 4.9.28), using the instructions there and the links inside. It sounds complicated but it is not - one command :
B4X:
sudo rpi-update 2ca627126e49c152beb1bf7abd7122ce076dcc65
and a reboot after the process is done.
That solved the problem and gpio works ! :D
 
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
FYI: Regarding Post #154 - After installing & test on a Raspberry Pi 3, results in error:
B4X:
Unable to determine hardware version. I see: Hardware   : BCM2835 - expecting BCM2708 or BCM2709.
If this is a genuine Raspberry Pi then please report this to [email protected]. If this is not a Raspberry Pi then you are on your own as wiringPi is designed to support the Raspberry Pi ONLY.

SOLVED by replacing the jars pi-core, pi-device, pi-gpio-extension (date 25.6.2017) with the ones from the pi4j-1.2-SNAPSHOT (date 15.3.2017).
Note: wiringPi version 2.44 installed.
 

jayel

Active Member
Licensed User
Longtime User
Hello,
Pi 3, with Piface connected, the piface emulator program works fine. But with pi4j lib, it doesn't work.
No errors, nothing !
I have enabled the spi on the raspberry.
Strange but after I enabled the module I get : modprobe: FATAL: Module spi-bmc2708 not found.

Any ideas?

John
 

jayel

Active Member
Licensed User
Longtime User
I have prepared a new sd card and now when i do rasp-config and enable the spi, I got no more errors, the emulator is ok but B4J app doesn't work with the piface.
No errors, no output led, no input read... nothing....

Are there steps to check what I am doing wrong?
 
Top