2020/02/18

Raspberry Pi - Add support for power on / off button

This post is a quick write up of me following the instruction here to create an on/off button for switching on/off the PI. When the PI is running, connecting GPIO3 to GND will cause the system to send out the sudo shutdown -h now command to switch off the PI. When the PI is not running, connecting GPIO3 to GND will cause the PI to wake up and start running.

***IMPORTANT!! This is extremely important to shut down the PI using the sudo shutdown -h now command to avoid corrupting the file system of the SD Card. If you experience trouble booting the PI after a sudden power outage, chances are the SD file system is corrupted and you will have to write a new Raspbian image to the SD Card and re-configure the Raspbian if a backup image is not available ***




1. Change the working directory to /home/pi using the cd /home/pi command.

2. Issue nano listen-for-shutdown.py to edit a program.

#!/usr/bin/env python

import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.wait_for_edge(3, GPIO.FALLING)

subprocess.call(['shutdown', '-h', 'now'], shell=False)

Press "ctrl + x" followed by "y" then enter to save the program.

3. Place the script in /usr/local/bin and make it executable:

sudo mv listen-for-shutdown.py /usr/local/bin/
sudo chmod +x /usr/local/bin/listen-for-shutdown.py

4. Add another script called listen-for-shutdown.sh that will start/stop our service. To create the script:

nano listen-for-shutdown.sh

Enter the following code in that file and save it:

#! /bin/sh

### BEGIN INIT INFO
# Provides:          listen-for-shutdown.py
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting listen-for-shutdown.py"
    /usr/local/bin/listen-for-shutdown.py &
    ;;
  stop)
    echo "Stopping listen-for-shutdown.py"
    pkill -f /usr/local/bin/listen-for-shutdown.py
    ;;
  *)
    echo "Usage: /etc/init.d/listen-for-shutdown.sh {start|stop}"
    exit 1
    ;;
esac

exit 0

5. Place this file in /etc/init.d and make it executable.

sudo mv listen-for-shutdown.sh /etc/init.d/
sudo chmod +x /etc/init.d/listen-for-shutdown.sh

6. Now we'll register the script to run on boot.

sudo update-rc.d listen-for-shutdown.sh defaults

7. Since the script won't be running, we'll go ahead and start it with:

sudo /etc/init.d/listen-for-shutdown.sh start

A message similar to the below one will appear. Ignore it.

pi@raspberrypi:~ $ /usr/local/bin/listen-for-shutdown.py:7: RuntimeWarning: A physical pull up resistor is fitted on this channel!
  GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)

There's nothing to build here, but we need to understand how to wake up the Pi from a halt state before we build the shutdown functionality. Simply put, shorting pins 5 and 6 (GPIO3 and GND) together will wake the Pi up from a halt state.

An easy way to test this is to shut down the Pi with sudo shutdown -h now, and connect pins 5 and 6 with a female to female cable. You only need to short them momentarily. Then you should find that the Pi is "awake".

Reference:

How to Add a Power Button to Your Raspberry Pi
https://howchoo.com/g/mwnlytk3zmm/how-to-add-a-power-button-to-your-raspberry-pi

No comments:

Post a Comment