Skip to content

Instantly share code, notes, and snippets.

@imjosh
Created January 31, 2019 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imjosh/48fe746f25d7e5089d108791bf1014e7 to your computer and use it in GitHub Desktop.
Save imjosh/48fe746f25d7e5089d108791bf1014e7 to your computer and use it in GitHub Desktop.
Raspberry Pi Safe Shutdown + Restart Switches
# Safe Shutdown/Restart Switches
# untested
shutdownPin = 5
restartPin = 15
GPIO.setup(shutdownPin, GPIO.IN)
GPIO.setup(restartPin, GPIO.IN)
shutdownStatePrev = True
restartStatePrev = True
while True:
# grab the current state of the buttons
shutdownStateNow = GPIO.input(shutdownPin)
restartStateNow = GPIO.input(restartPin)
# check to see if button has been pushed
if shutdownStateNow != shutdownStatePrev and shutdownStateNow == False:
# shutdown
subprocess.call("sudo shutdown -h now", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
shutdownStatePrev = shutdownStateNow
elif restartStateNow != restartStatePrev and restartStateNow == False:
# Reboot
subprocess.call("sudo reboot", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
restartStatePrev = restartStateNow
time.sleep(.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment