If you want to automatically power off your Pi when you run the shutdown command (ex. shutdown menu in xbmc), this can be done pretty easily through a small addition to the ATXRaspi circuit. Basically we need to tell ATXRaspi to issue a shutdown, from software rather than from the button press. We can simulate the power button press usgin a NPN transistor from any Pi GPIO. Below is an illustration how this can be done with GPIO10:Please note: this only works for momentary button operation. It will not work in latching/slide switch mode.
Here’s a sample script that should run when you want to shutdown or reboot your Pi, save this script as /usr/bin/local/softshutdown.sh:
#!/bin/bash
BUTTON=10
#setup GPIO 10 as output and set to HIGH
echo "$BUTTON" > /sys/class/gpio/export;
echo "out" > /sys/class/gpio/gpio$BUTTON/direction
echo "1" > /sys/class/gpio/gpio$BUTTON/value
#keep GPIO 10 high for at least 4 seconds for shutdown, 1s for reboot (same as pressing ATXRaspi button)
SLEEP=${1:-4} #default to 4 seconds if no delay value was passed
re='^[0-9\.]+$'
if ! [[ $SLEEP =~ $re ]] ; then
echo "error: sleep time not a number" >&2; exit 1
fi
echo "ATXRaspi button press for: $SLEEP seconds..."
/bin/sleep $SLEEP
#restore GPIO 10
echo "0" > /sys/class/gpio/gpio$BUTTON/value
Make it executable with sudo chmod +x softshutdown.sh, then the permissions on this script should be the following:
pi@raspberrypi3:/usr/local/bin $ ls -la
-rwxr-xr-x 1 root staff 580 Dec 29 17:59 softshutdown.sh
Since /usr/local/bin is in your PATH variable (check with printenv command) you can now execute/edit the softshutdown.sh from anywhere). Also note this script takes a single decimal/numeric variable which represents the delay of the “button press”, if no value is given then the default is 4 (seconds), and this simulates a shutdown. For a reboot, pass a value of 1: softshutdown.sh 1.
Let’s do something cool and create aliases to execute shutdown and reboot in raspbian, run the following (use your own custom alias if you’d like):
$ printf "%s\n" "alias atxshutdown='sudo shutdown.sh'" >> ~/.bashrc
$ printf "%s\n" "alias atxreboot='sudo shutdown.sh 1'" >> ~/.bashrc
$ source ~/.bashrc
This adds permanent aliases to your .bashrc file and activates them. Now you can run these commands from anywhere:
- atxreboot to trigger a reboot – ATXRaspi status LED will blink and wait for Pi to reboot
- atxshutdown to trigger a shutdown – ATXRaspi button status LED pulses until Pi is halted, then it cuts power off as usual!
Using with XBMC
For XBMC you can do it somewhat simpler since there is a shutdown menu which invokes a shutdown command that can be replaced with invoking a custom script. You can use the following equivalent script, save it as /home/pi/softshutdown.sh:
#!/bin/bash
sudo gpio -g mode 10 out;
sudo gpio -g write 10 1;
sleep 4;
sudo gpio -g write 10 0
Next modify this file: /opt/xbmc-bcm/xbmc-bin/share/xbmc/addons/skin.confluence/720p/DialogButtonMenu.xml, and in the “Shutdown description” section replace XBMC.Powerdown() with System.Exec(“/home/pi/softshutdown.sh”). You may need a reboot for this to take effect. Now when you shutdown XBMC, the transistor will simulate shutting down from ATXRaspi, and will power off your Pi as usual !
This was also posted as a guide here, and was inspired from this forum thread, thanks to all contributors!