B4J Code Snippet [IoT] Raspberry 2/piFace: Get status of digital inputs (and switches)

Here's another one stripped from Erel's examples (raw, no extra's to understand how the basics work). It will monitor all 8 digital inputs (0-7) and display the status.

You will need the jPi4J library. Get it from this post https://www.b4x.com/android/forum/threads/iot-jpi4j-raspberry-pi-gpio-controller.37493/(under the video) and copy it into the (additional) librariers folder. Create a new project (this example is a non-ui, but the code can be used 1:1 for a ui app, except the line "StartMessageLoop".

B4X:
'Non-UI application (console application)

#Region  Project Attributes
   #CommandLineArgs:
   #MergeLibraries: true
#End Region

Sub Process_Globals

   Private controller As GpioController
   Private pin0,pin1,pin2,pin3,pin4,pin5,pin6,pin7 As GpioPinDigitalInput
 
End Sub

Sub AppStart (Args() As String)
   controller.InitializePiFace(0x40, 0)
 
   pin0.Initialize("pin0",0)
   pin1.Initialize("pin1",1)
   pin2.Initialize("pin2",2)
   pin3.Initialize("pin3",3)
   pin4.Initialize("pin4",4)
   pin5.Initialize("pin5",5)
   pin6.Initialize("pin6",6)
   pin7.Initialize("pin7",7)
        
   StartMessageLoop
End Sub

Sub Pin0_StateChange(State As Boolean)
   Log("Pin0 StateChange event: " & State)
End Sub
Sub Pin1_StateChange(State As Boolean)
   Log("Pin1 StateChange event: " & State)
End Sub
Sub Pin2_StateChange(State As Boolean)
   Log("Pin2 StateChange event: " & State)
End Sub
Sub Pin3_StateChange(State As Boolean)
   Log("Pin3 StateChange event: " & State)
End Sub
Sub Pin4_StateChange(State As Boolean)
   Log("Pin4 StateChange event: " & State)
End Sub
Sub Pin5_StateChange(State As Boolean)
   Log("Pin5 StateChange event: " & State)
End Sub
Sub Pin6_StateChange(State As Boolean)
   Log("Pin6 StateChange event: " & State)
End Sub
Sub Pin7_StateChange(State As Boolean)
   Log("Pin7 StateChange event: " & State)
End Sub


Very nice documentation to see wiring examples: http://www.farnell.com/datasheets/1881551.pdf

How to use:

- Get a simple isolated wire. Strip both ends. Screw one end to port #9 (GND=Ground) at the edge (see the pictures, the green connectors)
- start the bridge on the RB
- connect the B4J bride from your desktop
- start the app
- put the loose end to ports 0-7 (=8)and watch the log
- press the buttons/switches on the board

Note: Switching the digital inputs 0-3 is the same as pressing the build in switches...

What's next?

You could use some reed contacts to check if windows/doors are opened and/or closed. The status' can be send using httputils (or direct via ntwork) to a server with a database. With that you could create an B4A app to check your home or let it notify you.
 
Top