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
I have now used the piface library and this on works :
B4X:
Dim PFace as PiFace

PFace.initialize(0x40,0)
Pface.GetSwitch(0).AddListener("Pin1")

Ok, but how can I set debounce with this library.
 

Gary Miyakawa

Active Member
Licensed User
Longtime User
I was having the same issue with the latest Raspian code. I updated my B4J jPi4J from Erel's msg #154. Put them in the right spot and it's working fine now.

Thank you for the fix!

Gary Miyakawa
 
Hi all!
First of all, I love your job Erel...
I receive this error using Pin 7 as input.
(Programming via Netbeans and C++ works well, with B4J no).

Raspberry Pi 3
jPi4J 2.00

B4X:
Private Col4 As GpioPinDigitalInput
Col4.Initialize("Col4",7)
B4X:
java.lang.RuntimeException: Unable to open GPIO direction interface for pin [7]: File o directory non esistente
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
I have get this message. Where can i get latest version of Pi4J ? My version is 1.50 o_O
 

rasaliad

Member
Licensed User
Longtime User
Hi community

I have a raspberry pi 400, it is a Pi4 i think.

When compile my app with jPi4J, the one in the first post, I get the following error, What I need to do, to solve the problem.

Thanks in advanced


WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
WARNING: package com.sun.glass.ui.win not in javafx.graphics
WARNING: package com.sun.prism.d3d not in javafx.graphics
WARNING: Unknown module: javafx.swt specified to --add-opens
Waiting for debugger to connect...
Program started.
Unable to determine hardware version. I see: Hardware : BCM2711
,
- expecting BCM2708, BCM2709 or BCM2835.
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.
 

rasaliad

Member
Licensed User
Longtime User
Hi MbedAndroid thanks,

I did what the link said, and check that the version is 2.52, but the error persist.

WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
WARNING: package com.sun.glass.ui.win not in javafx.graphics
WARNING: package com.sun.prism.d3d not in javafx.graphics
WARNING: Unknown module: javafx.swt specified to --add-opens

Waiting for debugger to connect...
Program started.
Unable to determine hardware version. I see: Hardware : BCM2711
,
- expecting BCM2708, BCM2709 or BCM2835.
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.
 

MbedAndroid

Active Member
Licensed User
Longtime User
the 4b has indeed a BCM2711. I'm using the PI4j on a Pi3+, Pi2B and PiZero without issues.
I'm afraid the the 4B isnt supported, as you say the wiring SPI is already at 2.52,
 

FrankDelporte

New Member
WiringPi which is used by Pi4J to interface with the GPIOs has been deprecated and the last version was indeed 2.52.
But in Pi4J V1.3 (for Java 8) and V1.4 (for Java 11) you can easily update to a non-official WiringPi version 2.60 which supports Raspberry Pi 4, CM4 and 400.
See https://pi4j.com/1.3/dependency.html
 

gazaduk

Member
Licensed User
Longtime User
Hi All, Has anyone managed to get the jPi4J library working on a Raspberry PI CM4 module? I'm in the process of updating a project from a PI2 to a CM4 module and am getting the same error as rasaliad (Post 172).

After trying everything to the best of my knowledge I have come to the conclusion the B4J jPi4J library needs updating to either the unofficial version https://pi4j.com/1.3/index.html or the new version 2 https://pi4j.com/about/new-in-v2/

If this is the case would it be possible to update the library to cover the new devices?

Thanks in advance.
 

gazaduk

Member
Licensed User
Longtime User
Yes tried it and still the same error. I'm fairly sure the error is coming from the B4J library not the PI side.
 
Top