Labels

lego (12) Linux (39) pi (20) Thinkpads (4)

Tuesday 17 December 2013

Modified switch.sh for Raspberry Pi (Raspbmc or Raspbin) (for illuminated and rocker-style power switches) [day 14 of 20-days-of-posts series]

Here is my modified switch.sh (for illuminated and rocker-style power switches) that were discussed in an earlier post.
(download switch.sh)
#!/bin/bash
#this is the GPIO pin connected to the lead on switch labeled OUT
GPIOpin1=23
#this is the GPIO pin connected to the lead on switch labeled IN
GPIOpin2=24
echo "$GPIOpin1" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction
echo "$GPIOpin2" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction
echo "1" > /sys/class/gpio/gpio$GPIOpin2/value
oldpower=$(cat /sys/class/gpio/gpio$GPIOpin1/value)

while [ 1 = 1 ]; do
sleep 2
power=$(cat /sys/class/gpio/gpio$GPIOpin1/value)
if [ $power = 1 -a $oldpower = 0 ]; then
oldpower=1
sudo initctl stop xbmc
sleep 5
sudo shutdown -h now
fi
done

Install the script by placing it in /etc/switch.sh.  Add /etc/switch.sh & to the bottom of /etc/rc.local but before the exit.

What this script handles that the original does not:
  • The original script provided by the vendor had a flaw where when the power off is triggered, the sudo poweroff is repeately ran over and over in a loop without any interruption inbetween.  This would cause my Raspberry to crash while powering off.  It would appear that it was turning off and would shutdown, but in reality it was turning off before it could safely shutdown.
  • Gracefully stop XBMC (if it is running) before shutting down.
  • I prefer to use shutdown -h now rather than poweroff.
  • I've witnessed an odd behaviour that on a fresh startup/boot up, the switch will be in a shutdown mode, meaning that it thinks it detected a shutdown request during the startup request, so that GPIOpin1 is pushing a value of 1, that would normally trigger the script to shutdown immediately.  So the Raspberry Pi shutdown shortly after starting up, and continuing in this loop.  The revision to the script ignores the switch input if it is instructing the system to shutdown on its initialization. 

1 comment:

  1. Hi Dylan,

    thank you for your nice tweak to the mausberry shutdown switch with rocker.

    I tried this with Volumio (volumio.org) on my Raspberry Pi. But no luck. Could please give me a hint to realize this for Volumio (I´am not very familiar in writing such code) .

    Thank you and best regards, Tom

    ReplyDelete