Adding a Halt Button to the
Raspberry Pi 2

Introduction

Since most of the audience here has used the Beaglebone Black with Allstar you are probably use to having a halt or shut-down button on the board. The Raspberry Pi 2 does not have any buttons on the PC board to facilitate this feature. This short article shows how to add a button. It is a worthwhile addition which allows an orderly shut-down should you not be able to ssh into the board and do not have radio control via a halt DTMF function. Pushing and holding the button down for at least 6 seconds is required to start the processor halt. The halt will be announced over the radio link.

Adding the switch

The switch is just a momentary single pole push button mounted on the case cover. It is wired to pin 11 (GPIO0 or GPIO17 depending on the stardard used. I use the wiringpi standard or GPIO0. The other side of the switch is wired to ground, pin 9. See the photos below for more detail.

The photos show an optional pin identification shield (available from Adafruit) placed over the pins. The wires are tacked at the bottom of the pins making sure to not get solder further up the pin. This will allow a 40 pin female connector to still be placed over the pins for other I/O functions. More information on RPi GPIO pinouts can be found at these sites:

These two sites show different naming schemes for some of the GPIO pins. I use the wiringpi scheme in this article which is the default for the gpio command.

Software

Software is included in the RPi2 edition of Allstar. Simply make the mod and set SHUTDOWN_MONITOR="enabled" in /usr/local/etc/allstar.env The default setting is "disabled" After boot if you would like to halt the system for shut-down simply hold the button down for at least 6 seconds. You should hear a confirmation over the radio. About 10 seconds later the system should halt. This is indicated by the stopping of the green LED heartbeat indication. The red LED will not go out. It is safe to remove power after the green light stays off. It is important to halt the processor and not just power off when shutting down a Linux system. On the BBB and RPi2 there are at least three ways of halting the processor to shut-down. Using a login method, either via ssh or a HDMI terminal, using a DTMF sequence over the radio (example in rpt.conf), or using the shut-down button. Be very careful when using a remote method if you are not present where the board resides. Once halted the only way to restart is to remove and reapply power.

For those who would like to implement this on a non Allstar system the code is below. Modify to your liking, make the script executable and run it at boot via rc.local or however you start jobs at boot.

#!/bin/bash

# Wait with interrupt for falling edge of pin 0 (RPi pin 11)
# Then poll the pin until it goes high. It elapsed time
# low is more than 6 seconds halt processor otherwise
# fall back into interrupt mode waiting for falling edge
# This script would be run at boot by rc.allstar
# WA3DSP 3/2015

 . /usr/local/etc/allstar.env

# Take pin from command line or use default
# Default pin 0 (wiringpi layout) (RPi pin 11)
if [ -z "$1" ]
  then
    PIN=0
  else
    PIN=$1
fi

ps ax | grep "[g]pio wfi $PIN falling" > /dev/null
if [ $? -eq 0 ]; then
  echo "Process is already running."
  exit 0
fi

# Required button hold down seconds
HOLDTIME=6

gpio mode $PIN in
gpio mode $PIN up
TIME1=1

while [ 1=1 ]
#echo "got here 1"
   gpio wfi $PIN falling
   do
#echo "got here 2"
   while [ `gpio read $PIN` -eq "0" ];
   do
#echo $TIME1 $STATE
      sleep 1
      let TIME1+=1
      if [ $TIME1 -gt $HOLDTIME ]; then
         break 3;
      fi
   done
   TIME1=1
   continue
done
# Call code here to execute before shutdown

/usr/bin/asterisk -rx "rpt localplay $NODE1 /usr/local/sbin/powerdown"
sleep 10
/usr/bin/shutdown -h now

© 2015 - WA3DSP