From 11a13b972e79a1d5c49457d7b3bbb151806c6557 Mon Sep 17 00:00:00 2001 From: ggasconn Date: Tue, 1 Oct 2019 13:27:15 +0200 Subject: [PATCH] Added script to turn off the system with a button --- gpio/turn_off_button/README.md | 37 +++++++++++++++++++++++++++ gpio/turn_off_button/turnOffButton.py | 28 ++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 gpio/turn_off_button/README.md create mode 100644 gpio/turn_off_button/turnOffButton.py diff --git a/gpio/turn_off_button/README.md b/gpio/turn_off_button/README.md new file mode 100644 index 0000000..214ece8 --- /dev/null +++ b/gpio/turn_off_button/README.md @@ -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 +``` \ No newline at end of file diff --git a/gpio/turn_off_button/turnOffButton.py b/gpio/turn_off_button/turnOffButton.py new file mode 100644 index 0000000..e8e708c --- /dev/null +++ b/gpio/turn_off_button/turnOffButton.py @@ -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) \ No newline at end of file