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:

jayel

Active Member
Licensed User
Longtime User
When I try to run in debug mode I get a error and the program stops : unable to open SPI device.

Any ideas?

John
 

Mark Turney

Active Member
Licensed User
Longtime User
Silly question probably, but did you enable SPI in raspi-config?
 

Herbert32

Active Member
Licensed User
Longtime User
actual Version ?

in first Thread i see 1.50

but here i see 1.60

which information is true ? if second, where can i download?

thx in advance
 

jayel

Active Member
Licensed User
Longtime User
Hey,

Strange :
B4X:
pin5.Initialize("pin5",5)
    pin5.SetPinPullResistance("PULL_DOWN")
    pin5.Name = "Detectie binnen-buiten"
    jo = pin5
    jo.RunMethod("setDebounce", Array(25))

I get this error :
Waiting for debugger to connect...
Program started.
Error occurred on line: 196 (Main)
com.pi4j.io.gpio.exception.UnsupportedPinPullResistanceException: This GPIO pin [INPUT 6] does not support the pull resistance specified [down]
at com.pi4j.gpio.extension.piface.PiFaceGpioProvider.setPullResistance(PiFaceGpioProvider.java:429)
at com.pi4j.io.gpio.impl.GpioPinImpl.setPullResistance(GpioPinImpl.java:189)
at anywheresoftware.b4j.objects.GpioControllerWrapper$GpioPinDigitalWrapper.SetPinPullResistance(GpioControllerWrapper.java:75)
at b4j.example.main._initinputsoutputs(main.java:235)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:612)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:226)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAcces
sorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)

PULL_UP works???

Why doesn't PULL_DOWN work ?

Greets

John
 
Last edited:

coslad

Well-Known Member
Licensed User
Longtime User
No, not error , but in the highlights is written : Added support for Raspberry Pi 3 - Model B


And I thought were some update.
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

Which jar does i need to download(include) if i want to use following code to access SPI ?

Try this code:
B4X:
Sub WiringPiSPISetup(Channel As Int, Speed As Int) As Int
   Dim jo As JavaObject
   Return jo.InitializeStatic("com.pi4j.wiringpi.Spi").RunMethod("wiringPiSPISetup", Array As Object(Channel, Speed))
End Sub

Sub WiringPiSPIDataRW(Channel As Int, Data() As Byte, Len As Int) As Int
   Dim jo As JavaObject
   Return jo.InitializeStatic("com.pi4j.wiringpi.Spi").RunMethod("wiringPiSPIDataRW", Array As Object(Channel, Data, Len))
End Sub
Make sure to go over the documentation: http://pi4j.com/apidocs/com/pi4j/wiringpi/Spi.html
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
I try following to access spi in PI but it always return 12 to me !? OK or Not ok !?

B4X:
Sub WiringPiSPISetup(Channel As Int, Speed As Int) As Int
   Dim jo As JavaObject
   Return jo.InitializeStatic("com.pi4j.wiringpi.Spi").RunMethod("wiringPiSPISetup", Array As Object(Channel, Speed))
End Sub

Sub WiringPiSPIDataRW(Channel As Int, Data() As Byte, Len As Int) As Int
   Dim jo As JavaObject
   Return jo.InitializeStatic("com.pi4j.wiringpi.Spi").RunMethod("wiringPiSPIDataRW", Array As Object(Channel, Data, Len))
End Sub
 

rbghongade

Active Member
Licensed User
Longtime User
Dear Erel,
It seems the library is not compatible with Raspberry Pi Zero W. Maybe pi4J library at http://pi4j.com is not updated. I get the following error message:;
B4X:
Unable to determine hardware version. I see: Hardware    : BCM2835
,
- expecting BCM2708 or BCM2709. Please report this to projects@drogon.net
Let us hope this problem is sorted out.
Just reporting.
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Answer related to Post#137.
Hi,

also stumbled recently about this issue. After various testing (based on thread discussions), resolved by [status 20170506]:
Note: To check the RPi CPU use command "cat /proc/cpuinfo". See at the bottom under hardware ie hardware : BCM2835.
 

Ilya G.

Active Member
Licensed User
Longtime User
Not working on Linux 4.9.24-v7+ :(

B4X:
Unable to determine hardware version. I see: Hardware   : BCM2835
,
- expecting BCM2708 or BCM2709.

To rwblinn, after updating pi4j-core, pi4j-device, pi4j-gpio-extension doesn't work with jDHT library
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Not working on Linux 4.9.24-v7+ :(

Thanks for the info. I can not verify as do not have Linux 4.9.24-v7+ - can only test on a RPi.
Note: Tested jDHT 1.0 again with success under Raspbian GNU/Linux 8 (jessie) with hardware BCM2835 (RPi 3) & DHT11 - just to go for sure.
 
Top