-
Notifications
You must be signed in to change notification settings - Fork 2
/
shutdown_button.py
executable file
·42 lines (34 loc) · 997 Bytes
/
shutdown_button.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#! /usr/bin/env python
import os
import RPi.GPIO as GPIO
import sys
import signal
import time
# GPIO3 (pin 5) set up as input.
SHUTDOWN_PIN = 3
def signal_handler(signal, frame):
print('You pressed Ctrl+C!')
sys.exit(0)
def shutdown_button(channel):
print('shuting down')
GPIO.cleanup(SHUTDOWN_PIN)
GPIO.setup(SHUTDOWN_PIN, GPIO.OUT)
GPIO.output(SHUTDOWN_PIN, GPIO.LOW)
time.sleep(1)
GPIO.output(SHUTDOWN_PIN, GPIO.HIGH)
time.sleep(1)
GPIO.output(SHUTDOWN_PIN, GPIO.LOW)
time.sleep(1)
GPIO.output(SHUTDOWN_PIN, GPIO.HIGH)
# shut down the rpi
os.system("/sbin/shutdown -h now")
signal.signal(signal.SIGINT, signal_handler)
GPIO.setmode(GPIO.BCM)
#It is pulled up to stop false signals
GPIO.setup(SHUTDOWN_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(SHUTDOWN_PIN, GPIO.FALLING, callback = shutdown_button, bouncetime = 250)
try:
while True:
time.sleep(1)
except:
GPIO.cleanup()