Skip to content
Go to file
Cannot retrieve contributors at this time
45 lines (38 sloc) 1.28 KB
# script to restart/shutdown a raspberry pi
import RPi.GPIO as GPIO
import time
import os
from subprocess import call
from datetime import datetime
buttonTime = None
shutdownSeconds = 3
# hack function creates file and tails the created file
# used for avoiding a while true loop here
# raw_input() would be nice but systemd doesn't like that
def loop():
os.system('date > /tmp/piboot')
os.system('tail -f /tmp/piboot')
# function to run when an interrupt is called
def shutdown(pin):
global buttonTime
if GPIO.input(pin):
# button is down
if buttonTime is None:
buttonTime =
# button is up
if buttonTime is not None:
elapsed = ( - buttonTime).total_seconds()
buttonTime = None
if elapsed >= shutdownSeconds:
# button pressed for 3 seconds then shutdown
call(['shutdown', '-h', 'now'], shell=False)
# if not then reboot
call(['shutdown', '-r', 'now'], shell=False)
GPIO.setmode(GPIO.BOARD) # set pin numbering to board numbering
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # set up pin 5 as an input
GPIO.add_event_detect(5, GPIO.BOTH, callback=shutdown, bouncetime=50) # set up an interrupt to look for button presses
loop() # run loop function to keep script running
You can’t perform that action at this time.