B4J Tutorial Switch to shutdown RPI

The RPi hasn't a switch to shutdown the RPI; if you cut off power from the RPi, SD card could be corrupted easely.

An easy solution is to add a switch to shutdown the RPI connected to a GPIO pin and listening this pin to call shutdown command via B4J.

Look full article at http://www.raspberry-pi-geek.com/Archive/2013/01/Adding-an-On-Off-switch-to-your-Raspberry-Pi

Code:

B4X:
'Non-UI application (console / server application)
Sub Process_Globals
    
    Private controller AsGpioController
    Private Pin1 As GpioPinDigitalInput
   
End Sub

Sub AppStart (Args() As String)
 
    controller.Initialize
    Pin1.Initialize("Pin1",1) 'GpioPinDigitalInput
    Pin1.SetPinPullResistance("PULL_DOWN")
     
    StartMessageLoop
End Sub

'Shutdown at Pin1 Change
Sub Pin1_StateChange(State As Boolean)
   Log("Pin1 StateChange event: " & State)
   If Pin1.State=True Then
        Dim shl As Shell
        shl.Initialize("shl", "sudo",Array("shutdown","now"))
        shl.Run(-1)
    Log("Shutdown")
   End If
End Sub


upload_2017-6-1_18-0-56.png
 

Cableguy

Expert
Licensed User
Longtime User
No, it either does a shutdown sequence or a reset sequence, according to the python script, but I guess, being it a script, the reset function can perhaps be Modified to accommodate a different function
 
Top