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:

tmf

Member
Licensed User
Longtime User
I see this is an older thread, but I am wanting to use B4A on Rasp PI or Orange PI or Banana PI or ODROID C1+ I need to pick one that will work with B4A, but I have not a clue, how are these supported? And how to access the I/O??

Richard.
 

Cableguy

Expert
Licensed User
Longtime User
With B4A? or with B4J?
 

Michael1968

Active Member
Licensed User
Longtime User
B4a is notrunning on Raspberry..there is no Android.B4a can run on pcduino,olimex a13 and cubieboard2-cubietruck
 

tmf

Member
Licensed User
Longtime User
B4a is notrunning on Raspberry..there is no Android.B4a can run on pcduino,olimex a13 and cubieboard2-cubietruck

Yes sorry, I was being "vanilla" about what I was saying I am looking at orange pi, banana pi, or nano pi 2 they all have android available... sorry for the confusion.

Richard.
 

derez

Expert
Licensed User
Longtime User
Got my RPI 2 and tried to run something with GPIO using jPi4j library, I get this error:
Program started.
׳™׳ ׳• 13, 2016 7:23:48 PM com.pi4j.util.NativeLibraryLoader load
SEVERE: Unable to load [libpi4j.so] using path: [/lib/libpi4j.so]
java.lang.IllegalArgumentException: The path has to be absolute, but found: \lib\libpi4j.so
at com.pi4j.util.NativeLibraryLoader.loadLibraryFromClasspath(NativeLibraryLoader.java:120)
at com.pi4j.util.NativeLibraryLoader.load(NativeLibraryLoader.java:92)
at com.pi4j.wiringpi.Gpio.<clinit>(Gpio.java:174)
at com.pi4j.io.gpio.RaspiGpioProvider.<init>(RaspiGpioProvider.java:51)
at com.pi4j.io.gpio.GpioFactory.getDefaultProvider(GpioFactory.java:106)
at com.pi4j.io.gpio.impl.GpioControllerImpl.<init>(GpioControllerImpl.java:54)
at com.pi4j.io.gpio.GpioFactory.getInstance(GpioFactory.java:89)
at anywheresoftware.b4j.objects.GpioControllerWrapper.Initialize(GpioControllerWrapper.java:51)
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.runVoidMethod(Shell.java:651)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
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.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)
java.lang.RuntimeException: java.lang.UnsatisfiedLinkError: com.pi4j.wiringpi.Gpio.wiringPiSetup()I
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:114)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
Caused by: java.lang.UnsatisfiedLinkError: com.pi4j.wiringpi.Gpio.wiringPiSetup()I
at com.pi4j.wiringpi.Gpio.wiringPiSetup(Native Method)
at com.pi4j.io.gpio.RaspiGpioProvider.<init>(RaspiGpioProvider.java:51)
at com.pi4j.io.gpio.GpioFactory.getDefaultProvider(GpioFactory.java:106)
at com.pi4j.io.gpio.impl.GpioControllerImpl.<init>(GpioControllerImpl.java:54)
at com.pi4j.io.gpio.GpioFactory.getInstance(GpioFactory.java:89)
at anywheresoftware.b4j.objects.GpioControllerWrapper.Initialize(GpioControllerWrapper.java:51)
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.runVoidMethod(Shell.java:651)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
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.BA.raiseEvent2(BA.java:93)
Program terminated (StartMessageLoop was not called).
... 3 more

This is after installing wiringPi from here http://wiringpi.com/
The error ocours at the "controller.Initialize" line. Seems like I haven't installed something.
In another app I got this :
wiringPiSetup: Must be root. (Did you forget sudo?)
Help ?
 
Last edited:

viriato

Member
Licensed User
Longtime User
Hi,
I have working B4J program on Raspberry B version, using the WiringPi and jPi4j.jar library
I made new installation on raspberry 2 with Jessie OS Version and new WiringPi library (nut same jPi4j java library Version 1.0)
Same b4J program as in the past , I can start the program , i can see the pin status on log (but no change ) same on the output no change , I can drive leds , all outputs still static
If I use the GPIO tool on Raspberry 2 , i can read , write on the pins without any issue.

Any help will be appreciated
Thanks a lot
 

rbghongade

Active Member
Licensed User
Longtime User
Pin
Hi,
I have working B4J program on Raspberry B version, using the WiringPi and jPi4j.jar library
I made new installation on raspberry 2 with Jessie OS Version and new WiringPi library (nut same jPi4j java library Version 1.0)
Same b4J program as in the past , I can start the program , i can see the pin status on log (but no change ) same on the output no change , I can drive leds , all outputs still static
If I use the GPIO tool on Raspberry 2 , i can read , write on the pins without any issue.

Any help will be appreciated
Thanks a lot
Numbers for the Raspberry pi models are different pls check out the same.
 
Top