-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshutdownSwitch.py
More file actions
executable file
·28 lines (22 loc) · 895 Bytes
/
shutdownSwitch.py
File metadata and controls
executable file
·28 lines (22 loc) · 895 Bytes
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
#!/bin/python
#This script was authored by AndrewH7 and belongs to him (www.instructables.com/member/AndrewH7)
#You have permission to modify and use this script only for your own personal usage
#You do not have permission to redistribute this script as your own work
#Use this script at your own risk
import RPi.GPIO as GPIO
import os
gpio_pin_number=40
GPIO.setmode(GPIO.BOARD)
GPIO.setup(gpio_pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#It's very important the pin is an input to avoid short-circuits
#The pull-up resistor means the pin is high by default
try:
GPIO.wait_for_edge(gpio_pin_number, GPIO.FALLING)
#Use falling edge detection to see if pin is pulled
#low to avoid repeated polling
os.system("sudo shutdown -h now")
#Send command to system to shutdown
except:
pass
GPIO.cleanup()
#Revert all GPIO pins to their normal states (i.e. input = safe)