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:

woniol

Active Member
Licensed User
Longtime User
Thank you very much.
It works fine on my Raspberry pi.

Are You going to add some more features in like support for I2C communication.
which is supported by Pi4J
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to access I2C API:
B4X:
Sub GetBus(BusNumber As Int) As JavaObject
   Dim factory As JavaObject
   Return factory.InitializeStatic("com.pi4j.io.i2c.I2CFactory").RunMethodJO("getInstance", Array As Object(BusNumber))
End Sub
'Bus methods
Sub GetDevice(Bus As JavaObject, Address As Int) As JavaObject
   Return Bus.RunMethodJO("getDevice", Array As Object(Address))
End Sub

Sub CloseBus (Bus As JavaObject)
   Bus.RunMethod("close", Null)
End Sub

'Device methods
Sub Read(Device As JavaObject) As Int
   Return Device.RunMethod("read", Null)
End Sub

Sub Read2 (Device As JavaObject, Buffer() As Byte, Offset As Int, Size As Int) As Int
   Return Device.RunMethod("read", Array As Object(Buffer, Offset, Size))
End Sub

Sub Write(Device As JavaObject, b As Byte)
   Device.RunMethod("write", Array As Object(b))
End Sub

Sub Write2(Device As JavaObject, Buffer() As Byte, Offset As Int, Size As Int)
   Device.RunMethod("write", Array As Object(Buffer, Offset, Size))
End Sub

Example:
B4X:
Dim bus As JavaObject = GetBus(0)
Dim device As JavaObject = GetDevice(bus, 0)
Write(device, 120)

The API is described here: http://pi4j.com/apidocs/index.html

Serial should be already supported with jSerial library.
 
Last edited:

Toley

Active Member
Licensed User
Longtime User
Serial should be already supported with jSerial library.
Did anyone was able to do it ?

I've try with this simple code :
B4X:
'Non-UI application (console application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: False
#End Region

Sub Process_Globals
    Private myserial As Serial
    Private astream As AsyncStreams
    Private tmr_snd As Timer
    Private uart_list As List
    Private counter=0 As Int
    Private sndStr As String
End Sub

Sub AppStart (Args() As String)
    myserial.Initialize("")
    Log("Serial is Initialized")
    uart_list = myserial.ListPorts
    Log(uart_list.Get(0))
    Log(uart_list.Get(1))
    Log(uart_list.Get(2))
    Log(uart_list.Get(3))
    myserial.Open(uart_list.Get(0))
    myserial.SetParams(115200,8,1,0)
    astream.Initialize(myserial.GetInputStream,myserial.GetOutputStream,"astream")
    tmr_snd.Enabled = False
    tmr_snd.Initialize("tmr_snd",5000)
    tmr_snd.Enabled = True
    StartMessageLoop
End Sub

Sub tmr_snd_Tick
    sndStr = "Counter = " & counter & CRLF
    astream.Write(sndStr.GetBytes("UTF8"))
    counter = counter + 1
End Sub
The image show the error message from the Pi
 

Attachments

  • jSerial.png
    jSerial.png
    54 KB · Views: 1,769

salim safran

Member
Licensed User
Longtime User
i am looking for a i2c and spi library for b4a to communicate directly with these ports not usb, any ideas
 

salim safran

Member
Licensed User
Longtime User
woniol , u mean i can use pi4j for example on pcduino or other compatible Linux boards and shall work, I did not test it with boards from the same family processor, some how i see the pi not just slow but not supported in away under android, also trying to stick with android of more supported libraries from b4a. any ideas how to do that or still have to write it starting with sysfs commands. in fact i am trying to test the PCF8574 I/O Extender on android.
 
Last edited:

woniol

Active Member
Licensed User
Longtime User
jPi4J is wrapper for Pi4J library and it works with B4J.
I run my program on Raspbian (Raspberry Pi) and it should work on any platform that supports java
 

Toley

Active Member
Licensed User
Longtime User
Hi Erel, do you think SPI can be done in a similar way you did for I2C ?
 

Toley

Active Member
Licensed User
Longtime User
Seems to be the only supported way to do with this library. But it doesn't fit my needs since you have to send 1024 characters at a time. I'm not sure the connected device will like that.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
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
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
PiFace API

This is the code from the video above:
B4X:
Sub Process_Globals
   Private pf As PiFace
   Private ledIndex As Int
   Private timer1 As Timer
   Private direction As Int = 1
End Sub

Sub AppStart (Args() As String)
   pf.Initialize(0x40, 0)
   pf.GetSwitch(0).AddListener("sw0")
   pf.GetSwitch(1).AddListener("sw1")
   timer1.Initialize("timer1", 80)
   timer1.Enabled = True
   StartMessageLoop
End Sub

Sub timer1_Tick
   pf.GetLED(ledIndex).State = False
   ledIndex = (ledIndex + direction + 8) Mod 8
   pf.GetLED(ledIndex).State = True
End Sub

Sub Sw0_StateChange(State As Boolean)
   direction = -direction
End Sub

Sub SW1_StateChange(State As Boolean)
   If State = True Then
     direction = 2 * direction
   Else
     direction = direction / 2
   End If
End Sub

You can access the PiFace LEDs, switches, relays and I/O pins. The first step is to create a PiFace and initialize it. The default values are 0x40 for the address and 0 for the channel.

Then you can call GetSwitch, GetLED and GetRelay to access these features.

If you need to access the I/O ports then you should create a GpioController object and initialize it with controller.InitializePiFace.

See the driver installation section here: http://www.savagehomeautomation.com/piface
 
Last edited:
Top