Skip to content

Instantly share code, notes, and snippets.

@hornc
Last active January 3, 2019 02:56
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 hornc/b28d9cda1afe91132ede0cf17aecc245 to your computer and use it in GitHub Desktop.
Save hornc/b28d9cda1afe91132ede0cf17aecc245 to your computer and use it in GitHub Desktop.
Retropie GPIO control script
# shutdown.py script to be at
/home/pi/scripts/shutdown.py
# Copy the unit file to
/lib/systemd/system/pi_shutdown.service
# ensure permissions are correct
sudo chmod 644 /lib/systemd/system/pi_shutdown.service
# To enable the service:
sudo systemctl enable --now pi_shutdown.service
# TODO: make an install script to do all this.
[Unit]
Description=GPIO Shutdown and Lights
After=Multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/scripts/shutdown.py
[Install]
WantedBy=multi-user.target
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
# Set pin 11 HIGH to switch on cab lights
GPIO.setup(11, GPIO.OUT)
GPIO.output(11, 1)
# we will use the pin numbering to match the pins on the Pi, instead of the
# GPIO pin outs (makes it easier to keep track of things)
# DO NOT use the same pin that is used for the reset button as other shields are using the I2C pins. We will use a different
# pin to moinitor for the shutdown signal, but still use pin 7 to startup.
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
oldButtonState1 = True
while True:
#grab the current button state
buttonState1 = GPIO.input(13)
# check to see if button has been pushed
if buttonState1 != oldButtonState1 and buttonState1 == False:
# shutdown
GPIO.output(11, 0) # cab lights off
subprocess.call("shutdown -h now", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
oldButtonState1 = buttonState1
time.sleep(.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment