Skip to content

Instantly share code, notes, and snippets.

@korzhyk
Created August 27, 2018 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korzhyk/05a4c9fe6cab83002da72ed898b434e0 to your computer and use it in GitHub Desktop.
Save korzhyk/05a4c9fe6cab83002da72ed898b434e0 to your computer and use it in GitHub Desktop.
Raspberry Pi3 Shutdown button with LED indicator

Installation

  1. Put shutdown_button.py to /usr/local/bin/
  2. Put shutdown_button.service to /etc/systemd/system/
  3. Enable service systemctl enable shutdown_button.service
  4. Start service systemctl start shutdown_button.service
  5. Enjoy!

Misc

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# example gpiozero code that could be used to have a reboot
# and a shutdown function on one GPIO button
# scruss - 2017-10
from gpiozero import Button, LED
from signal import pause, signal, SIGINT
from sys import exit
from subprocess import check_call
use_button=3 # lowest button on PiTFT+
use_led=17
held_for=0.0
def signal_handler(sig, frame):
led.off()
exit(0)
def rls():
global held_for
if (held_for > 1.0):
led.off()
check_call(['/sbin/poweroff'])
else:
held_for = 0.0
def hld():
# callback for when button is held
# is called every hold_time seconds
global held_for
# need to use max() as held_time resets to zero on last callback
held_for = max(held_for, button.held_time + button.hold_time)
led = LED(use_led)
led.on()
button=Button(use_button, hold_time=1.0, hold_repeat=True)
button.when_held = hld
button.when_released = rls
signal(SIGINT, signal_handler)
pause() # wait forever
[Unit]
Description=GPIO shutdown button
After=network.target
[Service]
Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=/usr/bin/python3 /usr/local/bin/shutdown_button.py
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment