From c41f35a66e05e2765e2894f63f32838ab8389fe4 Mon Sep 17 00:00:00 2001 From: Birko Bergt Date: Mon, 14 Nov 2022 23:33:00 +0100 Subject: [PATCH 1/3] Add support for systemd. --- listen-for-shutdown.service | 11 +++++++++++ script/install.systemd | 21 +++++++++++++++++++++ script/uninstall.systemd | 18 ++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100755 listen-for-shutdown.service create mode 100755 script/install.systemd create mode 100755 script/uninstall.systemd 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" From 0e418a308d42c2fad506f06cdcc0d399989c65b0 Mon Sep 17 00:00:00 2001 From: "Austin St. Aubin" Date: Fri, 10 Feb 2023 23:35:41 -0600 Subject: [PATCH 2/3] Blink LED on Press, 3 Second Button Hold Delay --- listen-for-shutdown.py | 58 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index cecc42b..19a90ec 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 = 2 # GPIO2 / Pin #3 +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() From 3c3be61d3db7adda0b3775a907e8982405405b7c Mon Sep 17 00:00:00 2001 From: "Austin St. Aubin" Date: Fri, 10 Feb 2023 23:43:42 -0600 Subject: [PATCH 3/3] Moved LED pin https://embeddedcomputing.com/technology/open-source/development-kits/raspberry-pi-power-up-and-shutdown-with-a-physical-button --- listen-for-shutdown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index 19a90ec..5c51a4d 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -6,7 +6,7 @@ print('Raspberry PI GPIO Button & LED Shutdown Script') # Pin Definitions -PIN_LED = 2 # GPIO2 / Pin #3 +PIN_LED = 4 # GPIO4 / Pin #7 PIN_BUTTON = 3 # GPIO3 / Pin #5 try: