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

I added events handling for shutdown + reboot and a status LED. #20

Open
wants to merge 2 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Scripts used in our official [Raspberry Pi power button guide](https://howchoo.com/g/mwnlytk3zmm/how-to-add-a-power-button-to-your-raspberry-pi).

I added events handling for shutdown + reboot and a status LED.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this. If this gets merged, I can update the original power button guide to include instructions for adding the status LED.


## Installation

1. [Connect to your Raspberry Pi via SSH](https://howchoo.com/g/mgi3mdnlnjq/how-to-log-in-to-a-raspberry-pi-via-ssh)
Expand Down
50 changes: 45 additions & 5 deletions listen-for-shutdown.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
#!/usr/bin/env python


import signal
import sys
import RPi.GPIO as GPIO
import subprocess
import time

PWR_BUTTON_GPIO = 3 # 9(GND) 5-GPIO3(+)
RST_BUTTON_GPIO = 4 # 9(GND) 7-GPIO4(+)
LED_STATUS_GPIO = 18 # 6(GND) 12-GPIO18(+)
Comment on lines +10 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two should be configurable. I'm thinking:

Suggested change
RST_BUTTON_GPIO = 4 # 9(GND) 7-GPIO4(+)
LED_STATUS_GPIO = 18 # 6(GND) 12-GPIO18(+)
RESET_BUTTON_GPIO = int(os.environ.get('RESET_BUTTON_GPIO', 4)) # 9(GND) 7-GPIO4(+)
LED_STATUS_GPIO = int(os.environ.get('LED_STATUS_GPIO', 18)) # 6(GND) 12-GPIO18(+)

Not sure if casting to int is actually required, and we'd need to import os above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't thought through yet, what is the recommended way for the user to set these since this script runs at startup. Let me know if you have any alternative ideas here.

DEBOUNCE_TIME = 300

# DEBUG
# if __name__ == '__main__':
# GPIO.setmode(GPIO.BCM)
# GPIO.setup(PWR_BUTTON_GPIO, GPIO.IN)
# pressed = False
# while True:
# # button is pressed when pin is LOW
# if not GPIO.input(PWR_BUTTON_GPIO):
# print("Button pressed!")
# else:
# print("Button not pressed!")
# time.sleep(0.5)
Comment on lines +14 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments will need to be removed.


def signal_handler(sig, frame):
# print("Cleanup and exit...")
GPIO.cleanup()
sys.exit(0)

def button_power_pressed_callback(channel):
# print("POWER BUTTON PRESSED!")
subprocess.call(['shutdown', '-h', 'now'], shell=False)

GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.wait_for_edge(3, GPIO.FALLING)
def button_reboot_pressed_callback(channel):
# print("REBOOT BUTTON PRESSED!")
subprocess.call(['shutdown', '-r', 'now'], shell=False)

subprocess.call(['shutdown', '-h', 'now'], shell=False)
if __name__ == '__main__':
GPIO.setmode(GPIO.BCM)

GPIO.setup(LED_STATUS_GPIO, GPIO.OUT)
GPIO.setup(PWR_BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(RST_BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Comment on lines +43 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd need to figure out how to make LED and RESET conditional. POWER can stay because I believe it HAS to use pin 3.


GPIO.output(LED_STATUS_GPIO, GPIO.HIGH)
GPIO.add_event_detect(PWR_BUTTON_GPIO, GPIO.FALLING, callback=button_power_pressed_callback, bouncetime=DEBOUNCE_TIME)
GPIO.add_event_detect(RST_BUTTON_GPIO, GPIO.FALLING, callback=button_reboot_pressed_callback, bouncetime=DEBOUNCE_TIME)

signal.signal(signal.SIGINT, signal_handler)
signal.pause()