B4J Library [IoT] jPi4J - Raspberry Pi GPIO controller

Michael1968

Active Member
Licensed User
Longtime User

thx Erel ....it works now
 

StarinschiAndrei

Active Member
Licensed User
Longtime 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 ?

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
 

Toley

Active Member
Licensed User
Longtime User
Sub Timer1_Tick
pin1.State = Not(pin1.State)Log("Switching Pin1 state. Pin1 state = " & pin1.State)End Sub

It looks like the change is on pin1
 

StarinschiAndrei

Active Member
Licensed User
Longtime 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

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:

StarinschiAndrei

Active Member
Licensed User
Longtime User
Than
Thank you,
 

StarinschiAndrei

Active Member
Licensed User
Longtime 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
 

woniol

Active Member
Licensed User
Longtime 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
Longtime 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
Longtime 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
 

woniol

Active Member
Licensed User
Longtime User
I will try to find some time this weekend and write a class for DS1621 with pi4j.
 

Michael1968

Active Member
Licensed User
Longtime 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
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…