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:

Marek Sukop

New Member
Licensed User
Longtime User
Hi, I am new here. I have installed on my computer B4A (enterprise version). Simple apps for mabile phone are well, but I have problem with my banana Pi. I have installed there android 4.2.2. I have transfered all files from jPi4J.zip to library folder (B4A). Application fals if I use controller.Initialize(). I'm the begginer and I don't understant it.

LogCat connected to: 20080411
--------- beginning of /dev/log/main
--------- beginning of /dev/log/system
** Activity (main) Create, isFirst = true **
java.lang.UnsatisfiedLinkError: Native method not found: com.pi4j.wiringpi.Gpio.wiringPiSetup:()I
at com.pi4j.wiringpi.Gpio.wiringPiSetup(Native Method)
at com.pi4j.io.gpio.RaspiGpioProvider.<init>(RaspiGpioProvider.java:47)
at com.pi4j.io.gpio.GpioFactory.getDefaultProvider(GpioFactory.java:102)
at com.pi4j.io.gpio.impl.GpioControllerImpl.<init>(GpioControllerImpl.java:67)
at com.pi4j.io.gpio.GpioFactory.getInstance(GpioFactory.java:85)
at anywheresoftware.b4j.objects.GpioControllerWrapper.Initialize(GpioControllerWrapper.java:52)
at b4a.example.main._activity_create(main.java:303)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at b4a.example.main.afterFirstLayout(main.java:98)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:582)
at dalvik.system.NativeStart.main(Native Method)

Second question: In my menu is no possibility to choose New Non-UI app. Whats wrong???

Thank's for answer.
 

Hypnos

Active Member
Licensed User
Longtime User
I want to try the "Piface control and display" board, can I use this library to change the text on the LCD display and also detect the button status on the board ? Am I correct that I have to use I2C to write my text ? thanks!
 

YIM bunchhat

Active Member
Licensed User
Longtime User
I want to use library Jpi4j to generate signal PWM with GPIO of Raspberry Pi. How can I do? Thank
 

daniedb

Active Member
Licensed User
Longtime User
Thanks. Some guys did email me the file. For some reason stop downloading by me.

Cheers
 

GPN

Member
Licensed User
Longtime User
Hi All,
I'm using jPi4J but it doesn't work with Digital Output.
I can't change state of Pins. I use RPi v2 B+

controller.Initialize
Pin1.Initialize(15,False)

or

controller.Initialize
Pin1.Initialize(15,True)

is the same thing: the Pin1 state go high and I can't change it's state.

Is there any way to change one specific initialised output to an input?
I've to implement one-wire protocol.

Pin numbering is limited to 1-20. Could you expand the pinout?

Could you help me?

Thanks,
regards,
Giuseppe.
 

GPN

Member
Licensed User
Longtime User
'Non-UI application (console / server application)
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
#End Region

Sub Process_Globals
Dim controller As GpioController
Dim Pin1 As GpioPinDigitalOutput
End Sub

Sub AppStart (Args() As String)
controller.Initialize
Pin1.Initialize(15,False))
controller.Shutdown
End Sub
 

GPN

Member
Licensed User
Longtime User
Hi,
I've solved by copying the ne library Pi4J in the library dir.
Now, I've to tu use GpioPinDigitalMultipurpose.
How I can do it?
Could you help me?
 

Mark Turney

Active Member
Licensed User
Longtime User
Just ordered the Canakit Ultimate Pi 2 kit, Piface rev 2 board, case to hold both and iPazzPort keyboard. Should be here next week. Can't wait!! :D
 
Top