B4J Library [IoT] jPi4J - Raspberry Pi GPIO controller

Discussion in 'B4J Libraries & Classes' started by Erel, Feb 5, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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:
    Code:
    '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(
    1True'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:
    [​IMG]

    B4J-Bridge is very useful when working with this board: http://www.basic4ppc.com/android/forum/threads/remote-debugging-with-b4j-bridge.38804/
    You can download it to the board with this command:
    Code:
    wget http://www.b4x.com/b4j/files/b4j-bridge.jar
    Then run it as root:
    Code:
    sudo <path to java> -jar b4j-bridge.jar
    [​IMG]


    PiFace extension

    V1.00 adds support for the PiFace extension: http://www.savagehomeautomation.com/piface



    See this post: http://www.basic4ppc.com/android/forum/threads/jpi4j-raspberry-pi-gpio-controller.37493/#post-232767

    Download link: www.b4x.com/b4j/files/jPi4J.zip
     
    Last edited: Jan 8, 2018
    MarkusR, joulongleu, jmon and 11 others like this.
  2. woniol

    woniol Active Member Licensed 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
     
    joulongleu likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    You can use this code to access I2C API:
    Code:
    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 JavaObjectAddress 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 JavaObjectAs 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:
    Code:
    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: Mar 25, 2014
  4. woniol

    woniol Active Member Licensed User

    Thanks again. I'll do some test later.
     
  5. woniol

    woniol Active Member Licensed User

    I2c tested with PCF8574 I/O Extedner and works fine for me.
    I works with bus no 1 -> GetBus(1)
     
    salim safran and Erel like this.
  6. Toley

    Toley Active Member Licensed User

    Did anyone was able to do it ?

    I've try with this simple code :
    Code:
    '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
     

    Attached Files:

  7. Erel

    Erel Administrator Staff Member Licensed User

    Set #MergeLibraries to True.
     
    Peter Simpson and Toley like this.
  8. Toley

    Toley Active Member Licensed User

    Fantastic Erel you are the king, it works very well.
     
  9. salim safran

    salim safran Member Licensed User

    i am looking for a i2c and spi library for b4a to communicate directly with these ports not usb, any ideas
     
  10. woniol

    woniol Active Member Licensed User

    I2s works fine Pi4J - I tested it with PCF8574 I/O Extender.
     
  11. salim safran

    salim safran Member Licensed 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: Mar 12, 2014
  12. woniol

    woniol Active Member Licensed 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
     
  13. salim safran

    salim safran Member Licensed User

    will check it out.
     
  14. Toley

    Toley Active Member Licensed User

    Hi Erel, do you think SPI can be done in a similar way you did for I2C ?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

  16. Toley

    Toley Active Member Licensed 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.
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    The maximum size is 1024. The messages can be shorter.
     
  18. Toley

    Toley Active Member Licensed User

    Ok I missread the documentation. So this API should be perfect.
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Try this code:
    Code:
    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
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    PiFace API

    This is the code from the video above:
    Code:
    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(
    0x400)
       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 + 
    8Mod 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: Mar 25, 2014
    joulongleu likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice