B4J Library [IoT] jPi4J - Raspberry Pi GPIO controller

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

  1. Michael1968

    Michael1968 Active Member Licensed User

  2. Erel

    Erel Administrator Staff Member Licensed User

    It should be simple to call this API with JavaObject.

    Code:
    Dim lcd As JavaObject
    lcd.InitializeStatic(
    "com.pi4j.wiringpi.Lcd")
    Dim handle as Int = lcd.RunMethod("lcdInit"Array(<all the ints go here>))
    lcd.RunMethod(
    "lcdPuts"Array(handle, "text"))
     
  3. Michael1968

    Michael1968 Active Member Licensed User

    thx Erel ....it works now ;)
     
  4. StarinschiAndrei

    StarinschiAndrei Active Member Licensed User

    Hi Erel,
    I try to reproduce your example on my rpi but there is no changes on pin2 when i press the button.Could you please tell me what's wrong ?
    upload_2014-8-29_18-43-17.png
    Code:
    'Non-UI application (console / server 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
    upload_2014-8-29_18-47-18.png
     
  5. Toley

    Toley Active Member Licensed User

    It looks like the change is on pin1
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    The timer updates pin1 state every 5 seconds.

    In my tests I short-circuited pin2 to change its state.
     
  7. StarinschiAndrei

    StarinschiAndrei Active Member Licensed User

    Thank you Erel, it was my fault. Below the right circuit
    upload_2014-9-1_20-29-52.png
    and the code :
    Code:
    Sub Process_Globals
    Private controller As GpioController
      
    Private pin2 As GpioPinDigitalInput
      
    Private pin1 As GpioPinDigitalOutput
      
    Private Timer1 As Timer
      
    Private ms As Long
    End Sub

    Sub AppStart (Args() As String)
        controller.Initialize
      pin1.Initialize(
    1True'GpioPinDigitalOutput
      pin2.Initialize("Pin2"2'GpioPinDigitalInput
      pin2.SetPinPullResistance("PULL_DOWN")
      ms =
    1000
      
    Log("Monitoring Pin2 state")
      Timer1.Initialize(
    "Timer1", ms)
      Timer1.Enabled = 
    True
      StartMessageLoop
    End Sub
    Sub Timer1_Tick
      pin1.State = 
    Not(pin1.State)
      
    Log("Switching Pin1 state. Pin1 state = " & pin1.State & "Interval :" & ms )
    End Sub

    Sub Pin2_StateChange(State As Boolean)
    If State = False Then
        
    If ms <9000 Then
              
    Log("Pin2 StateChange event: " & State)
              ms =ms+ 
    1000
        
    Else
              ms =
    1000
        
    End If   
       Timer1.Initialize(
    "Timer1",ms)
    End If
    End Sub
     
    Last edited: Sep 1, 2014
  8. StarinschiAndrei

    StarinschiAndrei Active Member Licensed User

    Hi Erel,

    i would like to read a temperature sensor on pin2 , how can i get this value ?

    Thank you
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    I don't think that it is possible as the pin state is either high or low.
     
  10. StarinschiAndrei

    StarinschiAndrei Active Member Licensed User

    This is not good news for me , do you think that will be possible in near future to read values from GPIO pins ?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    I've uploaded a new beta version that allows you to provision an analog input pin. I haven't tested this functionality.

    Initialize a GpioPinAnalogInput object and then read the value with pin.Value. You should use a timer to periodically read the value.
     

    Attached Files:

    Toley likes this.
  12. StarinschiAndrei

    StarinschiAndrei Active Member Licensed User

    Than
    Thank you,
     
  13. StarinschiAndrei

    StarinschiAndrei Active Member Licensed User

    Hi Erel,
    I try the new version :( with different pins (1,4 and the last was 16) below the code and the error message
    Code:
    Sub Process_Globals
    Private controller As GpioController
      
    Private pin16 As GpioPinAnalogInput

      
    'Private pin1 As GpioPinDigitalOutput
    Private timer1 As Timer
    End Sub

    Sub AppStart (Args() As String)
        controller.Initialize
    pin16.Initialize(
    16)
      
    Log("Monitoring Pin2 state")
        timer1.Initialize(
    "Timer1"1000)
          timer1.Enabled = 
    True
      StartMessageLoop
    End Sub
    Sub timer1_tick
        
    Log(pin16.Value)
    End Sub
    upload_2014-9-12_20-24-33.png
     
  14. woniol

    woniol Active Member Licensed User

    I haven't tried this yet, but the RPi pins are all digital. To use analog input you will probably need another circuit with an interface to the Pi.
     
  15. StarinschiAndrei

    StarinschiAndrei Active Member Licensed User

    Yes i know. i use an LM35 temperature sensor it is similar with is on the following example http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/temperature/ and also i saw many example with this circuit but with python.
     
  16. woniol

    woniol Active Member Licensed User

    LM35 is not similar to DS18b20.
    LM35 is analog temperature sensor - it gives analog output
    DS18b20 is digital temperature sensor - is uses 1wire bus for data output.

    1wire is not yet supported by pi4j

    Plese have a look at DS1621 - it uses i2c bus supported by pi4j.
     
    Last edited: Sep 12, 2014
  17. StarinschiAndrei

    StarinschiAndrei Active Member Licensed User

    Yes , you are right. I found this how to read analog sensor, i didn't tested but sounds good.
    https://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi/overview
    probably i will buy MCP3008 chip this is the easiest solution
     
  18. woniol

    woniol Active Member Licensed User

    I will try to find some time this weekend and write a class for DS1621 with pi4j.
     
    irda likes this.
  19. Michael1968

    Michael1968 Active Member Licensed User

    Hi Woniol
    to use DS18b20 it's very easy

    Load the Kernel Modul:
    $sudo modprobe w1-gpio
    $sudo modprobe w1-therm

    this creates a folder:
    /sys/bus/w1/devices/10-00080293bd31
    10-00080293bd31 is the rom-ID of the DS18b20

    in this folder there is a file named w1_slave with the temperature

    I read the file with textreader line by line to get the temperature
    it works perfect for me

    Best regards
    Michael
     
  20. YIM bunchhat

    YIM bunchhat Active Member Licensed User

    Untitled.png Hi Erel,

    I try to test your code but I got an error don't know why?
     
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