There are two options for building the sleep functionality: using our install script or installing everything manually. I recommend using the install script, but the manual approach will help you understand how this works.

Option 1: Use the install script (easiest)

The simplest way to install the required scripts is to clone our power button repository, and run the install script.

SSH into your Pi, install git (if it's not already), and then run:

git clone https://github.com/Howchoo/pi-power-button.git ./pi-power-button/script/install

Read the next section if you want to understand how these scripts work.

Option 2: Install manually

The wake solution is fairly straight forward, but in order to safely shut down the Pi we're going to have to use a software solution. Since we know we're going to use pins 5 and 6 to wake the Pi, let's also use these pins to shut it down. That way we can wire up our button to those pins and it will both shutdown and wake the Pi.

We'll have to write a script that listens for a button press and shuts down the Pi. Before we look at the script, let's discuss a few different approaches. First, we could write a script that starts an infinite loop and waits for a change in the state of the GPIO pin. Using pseudo-code, that might look something like:

while True: if GPIO3 is pressed: shutdown the pi

While this would work, and probably doesn't have any real performance concerns, there's actually a better way. We can listen for an interrupt (edge detection). An interrupt is the change in state from LOW to HIGH or HIGH to LOW. The RPi.GPIO library provides a method called wait_for_edge that will block execution of our script until an interrupt is detected. So we'll setup GPIO3, and once we detect a FALLING edge we'll shutdown the Pi.

First, we need to connect to the Pi via SSH. Then, we'll use a script called listen-for-shutdown.py.

Mentioned here How to Connect to a Raspberry Pi Remotely via SSH The preferred (and most common) method of connecting to your Pi to run commands.

To create the script, we can use the nano editor. After connecting to your Pi, run the following command to create a file called listen-for-shutdown.py:

sudo nano listen-for-shutdown.py

Then, paste the following code into that file, and press CTRL-X to exit, and Y to save when prompted.

#!/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)

Next we need to start this script on boot. So we'll 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

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

sudo 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

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

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

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

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

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

Note: These scripts have been added to a Github repo to make them easier to pull down. Feel free to submit pull requests with improvements.