diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index cecc42b..5c51a4d 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -1,12 +1,58 @@ #!/usr/bin/env python - - import RPi.GPIO as GPIO import subprocess +import time + +print('Raspberry PI GPIO Button & LED Shutdown Script') + +# Pin Definitions +PIN_LED = 4 # GPIO4 / Pin #7 +PIN_BUTTON = 3 # GPIO3 / Pin #5 + +try: + # Button Press Logic + def button_interupt(channel): + time_start = time.time() + time_button = 0 + led_state = GPIO.LOW + time_pressed_max = 3 # seconds + + # Loop while button is pressed, and not passed max time + while GPIO.input(channel) == GPIO.LOW and time_button <= time_pressed_max: # wait for the button up + # DEBUGGING OUTPUT + print("Button:", time_button, led_state) + + # Blink LED + GPIO.output(PIN_LED, led_state) # blink LED + led_state = not led_state + time.sleep(0.1) #loop time and led blink interation rate. + + # How long was the button down? + time_button = time.time() - time_start + + # Set LED back to High, just in case was low. + GPIO.output(PIN_LED, GPIO.HIGH) # blink LED + + # Determine Button Time + if time_button >= time_pressed_max: + print("Power Button Pressed & Held:", time_button) + subprocess.call(['shutdown', '-h', 'now'], shell=False) # Power Off + + # Ignore Warrnings or not + GPIO.setwarnings(False) # Ignore warning for now + + # Setting GPIO layout + GPIO.setmode(GPIO.BCM) # GPIO.setmode(gpio.BOARD) | Use boards header pin order or lableing GPIO##. https://iot4beginners.com/difference-between-bcm-and-board-pin-numbering-in-raspberry-pi/ + # # Set pin as input pin pulled down to GND + GPIO.setup(PIN_LED, GPIO.OUT, initial=GPIO.HIGH) # LED ON + GPIO.setup(PIN_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Button -GPIO.setmode(GPIO.BCM) -GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP) -GPIO.wait_for_edge(3, GPIO.FALLING) + # # Button Press Event + GPIO.add_event_detect(PIN_BUTTON, GPIO.FALLING, callback=button_interupt, bouncetime=100) -subprocess.call(['shutdown', '-h', 'now'], shell=False) + # Sleep Forever, to keep script alive, button_interupt handles everything. + while True: + time.sleep(86400) +except: + GPIO.cleanup() diff --git a/listen-for-shutdown.service b/listen-for-shutdown.service new file mode 100755 index 0000000..9a986ce --- /dev/null +++ b/listen-for-shutdown.service @@ -0,0 +1,11 @@ +[Unit] +Description=Listen for shutdown service at GPIO3 +After=syslog.target network.target + +[Service] +Type=simple +ExecStart=/usr/bin/python3 /usr/local/bin/listen-for-shutdown.py +PIDFile=/run/listen-for-shutdown.pid + +[Install] +WantedBy=multi-user.target diff --git a/script/install.systemd b/script/install.systemd new file mode 100755 index 0000000..3e0a758 --- /dev/null +++ b/script/install.systemd @@ -0,0 +1,21 @@ +#! /bin/sh + +set -e + +cd "$(dirname "$0")/.." + +echo "=> Installing shutdown listener...\n" +sudo cp listen-for-shutdown.py /usr/local/bin/ +sudo chmod +x /usr/local/bin/listen-for-shutdown.py + +echo "=> Installing shutdown listener systemd service...\n" +sudo cp listen-for-shutdown.service /etc/systemd/system/ +sudo chmod +x /etc/systemd/system/listen-for-shutdown.service + +echo "=> Starting shutdown listener...\n" +sudo systemctl daemon-reload +sudo systemctl enable listen-for-shutdown.service +sudo systemctl start listen-for-shutdown.service +sudo systemctl status listen-for-shutdown.service + +echo "Shutdown listener installed.\n" diff --git a/script/uninstall.systemd b/script/uninstall.systemd new file mode 100755 index 0000000..5a86842 --- /dev/null +++ b/script/uninstall.systemd @@ -0,0 +1,18 @@ +#! /bin/sh + +set -e + +cd "$(dirname "$0")/.." + +echo "=> Stopping shutdown listener...\n" +sudo systemctl stop listen-for-shutdown.service + +echo "=> Removing shutdown listener...\n" +sudo systemctl disable listen-for-shutdown.service +sudo systemctl status listen-for-shutdown.service +sudo rm -f /etc/systemd/system/listen-for-shutdown.service +sudo rm -f /usr/local/bin/listen-for-shutdown.py + +echo "=> Reloading systemd services...\n" +sudo systemctl daemon-reload +echo "Shutdown listener uninstalled.\n"