Created
January 31, 2019 22:59
-
-
Save imjosh/48fe746f25d7e5089d108791bf1014e7 to your computer and use it in GitHub Desktop.
Raspberry Pi Safe Shutdown + Restart Switches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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