I made a second RuneAudio player this weekend.. The main difference, apart from the physical box, is power management.
On my first setup, toggling the switch abruptly cuts power to the Raspberry, which can cause filesystem corruptions on the MicroSD card (and experience shows, in just ten days, that the MPD database sometimes ends up corrupted).
So I wanted a system that would allow me to turn the Raspberry on by switching the switch, and turn it off cleanly by re-switching the same switch, and consume zero watt when off.
The simplest solution I have found is a switch capable of controlling two separate circuits, such as this one or that one (look for DPST, “Double pole single throw”).
One of the circuits of this switch will cut power to the power supply and will be setup in parallel with a Raspberry-controlled relay, while the state of the second circuit, connected to one of the Raspberry’s GPIO pins, will be monitored in software, and trigger a clean shutdown.
In the diagram above, the main power is on the blue and brown wires; the 5V on red and black; the relay control wire is the orange one, and the second switch’s circuit is in violet.
We’re going to write a small Python script that will start by setting GPIO 5 to output mode, then pull it high, which will close the relay. Then, we’ll configure GPIO 27 as input, then read its state in an infinite loop. Depending on the state, we’ll either continue to wait or start a proper shut down.
# Cat /root/power-control.py
import RPi.GPIO as GPIO
import os
import time
import sys
GPIO.setmode (GPIO.BCM)
#setup the relay control pin to high, so the relay closes
GPIO.setup (5, GPIO. OUT)
GPIO.output (5, GPIO.HIGH)
#Setup the switch monitoring pin
GPIO.setup (27, GPIO.IN, pull_up_down = GPIO.PUD_UP)
#Monitoring loop
while true:
state = GPIO.input (27)
if state == True:
#Switch is still ON
time.sleep (0.5)
else:
#Switch OFF, trigger shutdown
os.system ("/var/www/command/rune_shutdown poweroff")
os.system ("shutdown -h now")
sys.exit (0)
Two remarks:
- Depending on your cabling and the type of relay, the GPIO.HIGH may be LOW. Adapt to your setup.
- We never re-open the relay. This is done automatically, late in the process of the Pi shutdown, and this has the effect of cutting power immediately, so we don’t want to do it earlier.
We’ll now register this script as a Systemd service so that it starts automatically on boot:
# cat /usr/lib/systemd/system/power-control.service
[Unit]
Description=Power control
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /root/power-control.py
[Install]
WantedBy=multi-user.target
And finally, we activate and start it:
# systemctl enable power-control
# systemctl start power-control
# systemctl status power-control
The remaining software configuration is basically the same as in this first article: configuring RuneAudio, a network music player. There are also numerous good english-written howtos on the subject on Internet.
A few pictures of the physical setup, just because I like it:
This looks like a great solution.
What program do you use to add the code to the RuneAudio image?
Hi,
I use SSH.
Hi Colin, I am now able to SSH into my Pi. I am ready to add your code + build the circuit. Wish me luck; I will report back soon!
Hi Bob,
Good luck ! :)
Hi Colin,
It looks like the gpio command is not available. I followed your instructions. When I start the script the following message is displayed:
systemctl power-control
Unknown command verb power-control
I used the latest RuneAudio version for the Pi 4.
https://github.com/rern/rAudio-1