Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blink LED on Press, 3 Second Button Hold Delay #33

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
58 changes: 52 additions & 6 deletions listen-for-shutdown.py
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 11 additions & 0 deletions listen-for-shutdown.service
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions script/install.systemd
Original file line number Diff line number Diff line change
@@ -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"
18 changes: 18 additions & 0 deletions script/uninstall.systemd
Original file line number Diff line number Diff line change
@@ -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"