Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import time
- import subprocess
- from threading import Timer
- SHUTDOWN_HOLD_TIME = 5 # Time in seconds that power button must be held
- SHUTDOWN_PIN = 5 # Pin to trigger shutdown, pin 5 will also start up the pi when it's off
- # GPIO.BOARD means use pin numbering to matching the pins on the Pi, instead of the
- # CPU's actual pin numbering (makes it easier to keep track of things)
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(SHUTDOWN_PIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)
- buttonReleased = True
- while buttonReleased:
- GPIO.wait_for_edge(SHUTDOWN_PIN, GPIO.FALLING)
- # button has been pressed
- buttonReleased = False
- for i in range(SHUTDOWN_HOLD_TIME * 10):
- time.sleep(0.1)
- if GPIO.input(SHUTDOWN_PIN):
- buttonReleased = True
- break
- GPIO.cleanup()
- subprocess.call("shutdown -h now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement