Last active
January 22, 2018 18:51
-
-
Save wmontg5988/52d2f1f33fe5f362714c549777d343ad to your computer and use it in GitHub Desktop.
This python script was found on Stackoverflow.com forums. It is designed to reboot an Rpi remotely using another device by pulling up on a GPIO by use of a relay across GPIO input and 3.3v
This file contains 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
import os | |
import time | |
import sys | |
# Import the modules to send commands to the system and access GPIO pins | |
from subprocess import call | |
import RPi.GPIO as gpio | |
from time import sleep | |
from os import system | |
gpio.setmode(gpio.BCM) # Set pin numbering to BCM numbering | |
gpio.setup(22, gpio.IN) # Set up pin 22 as an input | |
def confirmation(pin): | |
#print "Confirmation Function on pin %d" % pin | |
system('echo Press again to confirm Shut down menu! | wall -n') # let all logged in users know | |
gpio.remove_event_detect(pin) | |
gpio.add_event_detect(pin, gpio.RISING, bouncetime=200) | |
sleep(2) # if button has been pressed again within 2 seconds, shut down function is called | |
if gpio.event_detected(pin): | |
shutdown(pin) | |
else: | |
#print "canceled" | |
system('echo Shut down canceled! | wall -n') # let all logged in users know | |
main() | |
def resetPin(pin): | |
#print "Pin %d has been reset" % pin | |
gpio.remove_event_detect(pin) | |
gpio.add_event_detect(pin, gpio.RISING, bouncetime=200) | |
def reboot(pin): | |
#print "Reboot Function on pin %d" % pin | |
call('reboot', shell=False) | |
exit(0) | |
# Define a function to run when an interrupt is called | |
def shutdown(pin): | |
#print "ShutDown function on pin %d" % pin | |
system('echo Press again if you want to reboot instead of shut down! | wall -n') # let all logged in users know | |
gpio.remove_event_detect(pin) | |
gpio.add_event_detect(pin, gpio.RISING, bouncetime=200) | |
sleep(2) # if the button has been pressed for a third time, within 2 seconds, Pi will reboot | |
if gpio.event_detected(pin): | |
reboot(pin) | |
else: | |
call('halt', shell=False) | |
exit(0) | |
# Define a function to keep script running | |
def main(): | |
#print "Main Function" | |
resetPin(22) | |
while True: | |
sleep(1) | |
if gpio.event_detected(22): | |
confirmation(22) | |
main() # Run the main function to keep script running |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment