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:

Mark Turney

Active Member
Licensed User
Longtime User
Just received my RPI kit in the mail yesterday. Looking forward to using this library and B4J for some cool projects. I actually wound up ordering the Sunfounder kit too, as it added a number of components not included in the Canakit Ultimate. Specifically an ADC, lots of sensors, servo, ultrasonic sensor, another breadboard and more wires. It is the old style GPIO adapter and cable (26 pin), but that's not an issue as the Canakit includes the 40 pin model designed for the newer RPI.
 

alexhi

Member
Licensed User
Longtime User
Seems like the Pi4J library doesn't recognize this device as it is not a stock Raspberry Pi.
Hi
I try in console OrangePI PC:
echo 1 > /sys/class/gpio_sw/PA10/data and its work (LED switch ON)

and I try in b4j

shl.Initialize("shl","echo",Array As String( "1 > /sys/class/gpio_sw/PA10/data"))
its not work :( Why?
 

alexhi

Member
Licensed User
Longtime User
Hi Erel!
I have Orange PI PC board and
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) Client VM (build 25.60-b23, mixed mode)

When a do its:
sudo java -jar b4j-bridge.jar
I see


B4J-Bridge v1.00
Waiting for connections (port=6790)...
My IP address is: 192.168.0.220

When I start my app in IDE I see:

B4J-Bridge v1.00
Waiting for connections (port=6790)...
My IP address is: 192.168.0.220
Connected!
Starting program
ProcessCompleted

But I don't see myapp.jar in folder OrangePi

What could be wrong ? Thank you.
 

Cableguy

Expert
Licensed User
Longtime User
Hi guys,

Does anyone have an example of how to read a ds18b20 temperatura sensor with a raspi 2b?
 

Cableguy

Expert
Licensed User
Longtime User
Hi Roycefer,

Indeed I had, and I kind of understand what needs to be done and in wich order BUT I can't convert that thinking into code. Another factor is that my sensor is still on the mail.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Have you looked for code snippets that do what you want in other languages (Python, Arduino C, Java, etc...)? The forum could help you translate them into B4J. I haven't checked but there's gotta be something on Arduino.cc in the forums.
 

Cableguy

Expert
Licensed User
Longtime User
Have you looked for code snippets that do what you want in other languages (Python, Arduino C, Java, etc...)? The forum could help you translate them into B4J. I haven't checked but there's gotta be something on Arduino.cc in the forums.

Found out that 1wire is a simple serial protocol, and has been successfully implemented by using the Tx/Rx pins of an Arduino tied together... I'll keep on searching for more info...
 
Last edited:

Michael1968

Active Member
Licensed User
Longtime User
Found out that 1wire is a simple serial protocol, and has been successfully implemented by using the Tx/Rx pins of an Arduino tied together... I'll keep on searching for more info...

reading the ds18b20 with the pi in is no problem.
you only have to read the file: /sys/bus/w1/devices/28-xxxx

see reply #39

best regards Michael
 

Cableguy

Expert
Licensed User
Longtime User
Thanks Michael, I am aware of that approach, and if I can't find a direct "real-time" way to read the sensor, I will have to go with it.
I understand that the file is very small, but it will add to the SD read/write capability and reduce its life time.
 

MbedAndroid

Well-Known Member
Licensed User
Longtime User
Someone has any idea why i get this error message?
got the lib installed in b4j lib
okt 19, 2015 9:54:47 AM 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 b4j.example.main._appstart(main.java:45)
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:483)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
main._appstart (java line: 45)
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 b4j.example.main._appstart(main.java:45)
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:483)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
 

MbedAndroid

Well-Known Member
Licensed User
Longtime User
got a rasp now b2 version, run sudo java-jar b4j-bridge.jar, that works, got connection
still the same error runnng this example



Program started.
okt 28, 2015 7:11:56 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:483)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:637)
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:483)
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:483)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:637)
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)
Program terminated (StartMessageLoop was not called).
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
... 3 more
 
Top