I recently got and old pair of speakers, and wanted to set them up to stream wirelessly with AirPlay. One of the best ways to do this with 3rd-party software seems to be to run shairport-sync on a Raspberry Pi.
However, it turned out that my speakers had a bit of a "hum" when no audio was playing, so I needed a way to switch them off when not in use. Also saving a couple of watts of power.
(Youtube Video)
- Raspberry Pi 3 model B
- TP-Link Powerline Adapter
- Raspberry Pi Case
- A compatible Relay. I used a 4x relay block which I had lying around but really you just need a single one
- Some wires
- Some female connectors and plugs
The starting point was this question which seemed to do something I wanted. Shairport-sync has a feature which runs a script when music starts or stops, and this was perfect for controlling the power.
I set up the python script to take an ON
or OFF
parameter and control the relay switch.
I used pins 4, 6, 8 and 10 on the Raspberry Pi (more convenient, as they're next to each other). Pin 4 supplies power at 5V. Pin 6 is ground, and pin 8 is the I/O for the switch. Pin 10 is connected to a second relay in case I decide to add something else to the system later on.
I ran the wires through the conveniently sized holes in the case, to keep them safe in case they catch on anything.
Then I set up the relay connection in a little plastic box on the other end.
And closed it up...
As you can see on the outside, I split the power cable to the speakers, and spliced the relay in the middle of the live wire.
I chose the Python GPIO library because the pin state is retained after the script exits. This isn't the case with gpiozero (a popular, simpler alternative).
Finally I had to change some configuration parameters in shairport-sync to point to my scripts:
...
sessioncontrol =
{
run_this_before_play_begins = "/home/pi/shairport-power/scripts/gpioControl.py ON";
run_this_after_play_ends = "/home/pi/shairport-power/scripts/gpioControl.py OFF";
...
And restarted the shairport-sync service. On the first run this didn't work, with a syslog error:
RuntimeError: No access to /dev/mem. Try running as root!
This requires a solution to fix permissions for the shairport-sync
user. The following will grant the shairport-sync user permission to use gpio
:
sudo adduser shairport-sync gpio
And then a system restart applies the change. Worked the next time around!