Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions gpio/turn_off_button/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Turn off the PI using a push button

Simply run the script and hold the push button attached to the correct GPIO port for 5 seconds, this will invoke a subprocess call which will turn off the system.

The script canbe added as a service with the following conf file:

```
#! /bin/sh

### BEGIN INIT INFO
# Provides: turnOffButton.py
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting turnOffButton. Listening for 5s pulse..."
/usr/local/bin/turnOffButton.py &
;;
stop)
echo "Stopping turnOffButton."
pkill -f /usr/local/bin/turnOffButton.py
;;
*)
echo "Usage: /etc/init.d/turnOffButton.sh {start|stop}"
exit 1
;;
esac

exit 0
```
28 changes: 28 additions & 0 deletions gpio/turn_off_button/turnOffButton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import subprocess

gpioPin = 3
definedTime = 5

GPIO.setmode(GPIO.BCM)
GPIO.setup(gpioPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
time.sleep(0.2)

if GPIO.input(gpioPin) == False:
print "Button pressed"
pressedTime = time.time()

while GPIO.input(gpioPin) == False:
time.sleep(0.2)

pressedTime = time.time()-pressedTime

if pressedTime < definedTime:
print("Button pressed less than 5 seconds. Time: "+str(pressedTime))
else:
#print("Button pressed more than 5 seconds. Time: "+str(pressedTime))
subprocess.call(['init','0'], shell=False)