DIY WiFi speaker using a Raspberry Pi
Published: 2024-11-18
I recently used a Sonos speaker for the first time. The experience sent me down rabbit hole that resulted in a purchase of a Raspberry Pi and working on a more hardware-focused project that I can’t say I imagined myself doing a few weeks prior.
What made that Sonos speaker, and generally all WiFi speakers, so great is that they are incredibly easy to use – no Bluetooth pairing or dealing with having multiple devices connected. As long as you are on the same network, directing audio to the speaker is usually as simple as a click or two on on a device like a phone or laptop. What’s more, WiFi speakers, like the Sonos, often have special integrations with services like Spotify. For Spotify specifically, this means that the speaker exists as a device that anyone on the network can start a session on. Once you start this session, the device where you started it (e.g your phone) acts more like a remote than a audio source, meaning you could recieve phone calls or even turn your phone off without interupting the session.
After using the Sonos Speaker, I was sold. Unfortunately, most Sonos Speakers are hundreds of dollars. What’s more, I have a speaker at home, which, besides for not having WiFi functionality, is perfectly fine. This got me thinking if there was a way to somehow augment the speaker so that I could connect to it over WiFi.
Using a Raspberry Pi as a AirPlay Server
Turns out, turning a speaker into a WiFi speaker is very simple using a Raspberry Pi, an aux cord and some open-source software called shairport-sync. shairport-sync is only compatible with the Apple ecosystem, however similar solutions exist for Android and Windows devices.
The only setup that was needed on the Pi was to install shairport-sync, make some changes to a configuration file and start it as a service:
sudo apt install shairport-sync # Install shairport-sync
sudo vi /etc/shairport-sync.conf # Edit configuration file to direct output to headphones
sudo systemctl enable shairport-sync # Start shairport-sync as a systemd service
I then plugged in one end of the aux cord to the Pi’s headphone jack, the other end into my speaker and was pleasantly surprised to see I had a functioning AirPlay device. This setup worked great and I could cast the audio from my iPhone or Mac to the Pi (and by proxy to my speaker) using AirPlay.
Using a Raspberry Pi as a Spotify Connect device
There were, however, a few issues I had with the AirPlay Server approach. First, AirPlay is obviously only compatible with Apple devices. Second, AirPlay casts all audio from your device to the speaker. For most purposes, this is fine, but I really liked how the Sonos speaker allowed me to use the speaker as a Spotify Connect device, having the speaker run the Spotify session while another device, like my phone, simply acted as a remote. A good solution for these problems was running another open-source software on the Pi called Raspotify.
Setup was similar to shairport-sync: installing the software, making some configuration changes and starting it as a service:
sudo apt-get -y install curl && curl -sL https://dtcooper.github.io/raspotify/install.sh | sh # Install Raspotify
sudo sudo vi /etc/raspotify/conf # Edit configuration file to direct output to headphones
sudo systemctl enable raspotify # Start Raspotify as systemd service
With shairport-sync and Raspotify running side by side on the Pi, I was able to replicate the features of that Sonos speaker I had used. I can now connect to the Pi using AirPlay on my Apple devices as well as connect directly to the Pi using Spotify on any device. This project saved me some money and opened my eyes to all the use cases of running a Pi in my home. Neat!