B4J Question [ABMaterial] [Solved]Using jPi4J library for I2C and SPI

Jose M. Tasende

New Member
Licensed User
Hi,
I am just starting with ABMaterial to make a Webapp for Raspberry PI to manage some external hardware, I am a c++ programmer so my experience is only doing native apps.
I did some basic testing using a template from ABMaterial and I can manage GPIO pins at will using the jPi4J wrapper library and it works perfect but for I2C or SPI protocols I need to access with calls to the Pi4J java library as Erel explained on some threads:
B4X:
factory.InitializeStatic("com.pi4j.io.i2c.I2CFactory").RunMethodJO("getInstance", Array As Object(BusNumber))
....
and

B4X:
Dim jo As JavaObject
jo.InitializeStatic("com.pi4j.wiringpi.Spi").RunMethod("wiringPiSPISetup", Array As Object(Channel, Speed))
jo.InitializeStatic("com.pi4j.wiringpi.Spi").RunMethod("wiringPiSPIDataRW", Array As Object(Channel, Data, Len))

What is the right way to access the library from the web app? If I do direct calls from a clicked button, per example, I get an I/O error from java so I suspect that I need some routine or library to pass data between the web app and the native routines running inside the Raspberry .
Thanks for your help.
 
Last edited:

Jose M. Tasende

New Member
Licensed User
Finally, I solved it. I include what I did to help others:
B4X:
'Previously be sure that you include the Pi4J jars needed in your aditional libraries folder:
'pi4j-core.jar
'pi4j-device.jar
'pi4j-gpio-extension.jar
'pi4j-service.jar
'You can get them from the Pi4J project page

'I create a bas module for the I2C bus and add it to my app:
Sub Process_Globals
   Public i2cbus As JavaObject
   Public i2cdevice As JavaObject
End Sub

'Routine to Initialize the bus and to get the device
Public Sub Init_I2C(i2caddress As Int)
  Dim factory As JavaObject

'From here I create calls to the library for I2c bus from Pi4J jar not present in actual jPi4J wrapper:
  Try
     Log("----------------------")
     i2cbus = factory.InitializeStatic("com.pi4j.io.i2c.I2CFactory").RunMethodJO("getInstance", Array As Object(1))
     i2cdevice = i2cbus.RunMethodJO("getDevice", Array As Object(i2caddress))
  Catch
     Log(LastException.Message)
  End Try
End Sub

Public Sub CloseBusI2C (bus As JavaObject)
   bus.RunMethod("close", Null)
End Sub

Public Sub WriteI2C(device As JavaObject, b As Byte)
   device.RunMethod("write", Array As Object(b))
End Sub

Public Sub ReadI2C(device As JavaObject) As Int
   Return device.RunMethod("read", Null)
End Sub

Public Sub Write2I2C(device As JavaObject, Buffer() As Byte, Offset As Int, Size As Int)
   device.RunMethod("write", Array As Object(Buffer, Offset, Size))
End Sub

Public Sub Read2I2C (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


'Finally, this is the button click event inside the web app page made with ABMaterial from the template
Sub btn1_Clicked(Target As String)
   If page.BlockEvent("btn1_clicked") Then Return
   Dim myTexts, myReturns As List
   Dim Bufferi2cout(256) As Byte   'BUFFER FOR WRITING
   Dim Bufferi2cin(256) As Byte   'BUFFER FOR READING
   Private bc As ByteConverter   'utility for showing data in Hex format

   I2cController.Init_I2C(0x50)   'Device Address for my M24M01 Eeprom memory
'Initialize the write buffer with some arbitrary data for test:
   For a=2 To 129
     Bufferi2cout(a)=a-2
   Next
   Bufferi2cout(0)=0   'Testing address to access (2 bytes needed for M24m01 chip)
   Bufferi2cout(1)=0
'write the address(2 bytes) + 128 bytes of data
  I2cController.Write2I2C(I2cController.i2cdevice,Bufferi2cout,0,130)
   Log("Write: ")
   Log("Data: " & bc.HexFromBytes(Bufferi2cout))   'Show data in Hex format
'Now I read the 128 bytes from the same address to confirm that it works:
   I2cController.Write2I2C(I2cController.i2cdevice,Bufferi2cout,0,2) 'write the address
   I2cController.Read2I2C(I2cController.i2cdevice,Bufferi2cin,0,128) 'read the data
   Log("Read: ")
   Log("Data: " & bc.HexFromBytes(Bufferi2cin))   'Show data in Hex format to compare
   page.UnblockEvent("btn1_Clicked")
End Sub

Next is to do the same for SPI bus, I will add my example when ready.
 
Last edited:
Upvote 0

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi
merry chrimas, How about your spi bus ?
 
Upvote 0
Top