B4J Library [IoT] jPi4J - Raspberry Pi GPIO controller

Erel

Administrator
Staff member
Licensed User
It should be simple to call this API with JavaObject.

B4X:
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"))
 

Michael1968

Active Member
Licensed User
It should be simple to call this API with JavaObject.

B4X:
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"))
thx Erel ....it works now ;)
 

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

B4X:
'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(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
upload_2014-8-29_18-47-18.png
 

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.
 

StarinschiAndrei

Active Member
Licensed User
The timer updates pin1 state every 5 seconds.

In my tests I short-circuited pin2 to change its state.
Thank you Erel, it was my fault. Below the right circuit
upload_2014-9-1_20-29-52.png

and the code :
B4X:
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(1, True) '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:

Erel

Administrator
Staff member
Licensed User
I don't think that it is possible as the pin state is either high or low.
 

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.
 

Attachments

StarinschiAndrei

Active Member
Licensed User
Than
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.
Thank you,
 

StarinschiAndrei

Active Member
Licensed User
Than

Thank you,
Hi Erel,
I try the new version :( with different pins (1,4 and the last was 16) below the code and the error message
B4X:
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
 

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.
 

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:

StarinschiAndrei

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.
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
 

Michael1968

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.
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
 
Top