I've built the shutdown circuit previously linked to by Ingohz.
http://www.mosaic-industries.com/embedded-systems/microcontroller-projects/raspberry-pi/on-off-power-controllerI originally built it for a RetroPie setup, but intend to make another one for RuneAudio once (if?) I get the software side working. Apologies in advance if some of this is specific to Raspbian and not applicable in Arch.
Setting up a Python script to detect a button press on the GPIO pin and issue shutdown commands is simple enough.
- Code: Select all
import RPi.GPIO as gpio
import os
gpio.setmode(gpio.BCM)
gpio.setup(4, gpio.IN, pull_up_down=gpio.PUD_UP)
gpio.wait_for_edge(4, gpio.FALLING)
os.system('sudo shutdown -h now')
My problem is, I can't figure out how to drive the pin low at the last moment before halt/poweroff, but not at reboot.
I've tried the following in systemd, but it doesn't work, presumably because it's after everything is unmounted and I'm unsure of a way round this. The Python script sets pin 4 output low and instantly kills the power if I slightly modify the service and remove umount.target.
- Code: Select all
[Unit]
Description=Cut power on shutdown
DefaultDependencies=no
Conflicts=reboot.target
After=umount.target
Before=shutdown.target
[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/usr/bin/python /home/pi/cutpower.py
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
I've also tried the following script in /lib/systemd/system-shutdown, but I don't think it's doing anything at all.
- Code: Select all
#!/bin/sh
if [ "$1" = "poweroff" -o "$1" = "halt" ]
then
echo "4" > /sys/class/gpio/export
sleep .5
echo "out" > /sys/class/gpio/gpio4/direction
echo "0" > /sys/class/gpio/gpio4/value
fi
Since the circuit only uses 1 GPIO pin, I can't use dtoverlay=gpio-poweroff in /boot/config.txt, otherwise pin4 is changed to low at boot, and the power is lost.
I'm hoping someone has managed to get this working with a single GPIO pin.