Raspberry Pi GPIO Shutdown Button: Difference between revisions
mNo edit summary |
|||
| Line 32: | Line 32: | ||
with open("/var/tmp/gpio-shutdown.log", "a") as myfile: | with open("/var/tmp/gpio-shutdown.log", "a") as myfile: | ||
myfile.write(datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') + " GPIO Shutdown triggered\n") | myfile.write(datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') + " GPIO Shutdown triggered\n") | ||
os.system("sudo shutdown -h now") | |||
# Add our function to execute when the button pressed event happens | # Add our function to execute when the button pressed event happens | ||
Latest revision as of 22:24, 17 January 2018
Based on the project by Inderpreet Singh:
To safely take the power of your Raspberry Pi it is best to shutdown the machine before you do. Just unplugging your little server might go well a couple of times but at some point your luck will run out. Something like a hardware failure or a corrupt sdcard will ruin the rest of your day. A shutdown-button for your Pi is one of the easiest things to add and will ease your mind as well.
The below Python program creates an event that will trigger when GPIO18 (pin 12) is connected to a ground pin (for example pin 14). These two pins are next to each other but you are free to use other pins. Just shortly connect the two pins (for example with a mouse button switch) and the event will trigger.
Note: the pins should not be connected continuously.
Python program
#!/bin/python
import RPi.GPIO as GPIO
import time
import os
import datetime
with open("/var/tmp/gpio-shutdown.log", "a") as myfile:
myfile.write(datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') + " Start\n")
# Use the Broadcom SOC Pin numbers, setup the Pin with Internal pullups enabled and PIN in reading mode.
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
# Our function on what to do when the button is pressed
def Shutdown(channel):
with open("/var/tmp/gpio-shutdown.log", "a") as myfile:
myfile.write(datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') + " GPIO Shutdown triggered\n")
os.system("sudo shutdown -h now")
# Add our function to execute when the button pressed event happens
GPIO.add_event_detect(18, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)
# Wait
while 1:
time.sleep(1)
Install as a service
We will then install this script as a service to make sure it is always started at boot.
Service definition file (Systemd):
sudo nano /lib/systemd/system/gpio-shutdown.service
[Unit] Description=gpio shutdown button After=multi-user.target [Service] Type=simple ExecStart=/usr/bin/python /home/pi/Scripts/gpio-shutdown.py Restart=on-abort [Install] WantedBy=multi-user.target
Install the service:
sudo chmod 644 /lib/systemd/system/gpio-shutdown.service chmod +x /home/pi/Scripts/gpio-shutdown.py sudo systemctl daemon-reload sudo systemctl enable gpio-shutdown
We can now stop, start and query the status with the standard service commands:
sudo systemctl stop gpio-shutdown sudo systemctl start gpio-shutdown sudo systemctl status gpio-shutdown
Show log file:
tail -10 /var/tmp/gpio-shutdown.log
