Raspberry Pi Does Sonos – Part 1 – Making an AirPlay Receiver
Overview
So … I want Sonos. I want the ability to be able to select tunes from Spotify, my NAS drive, the radio, whatever – on a tablet and have it come out of my hifi without the tablet physically attached to it.
I could use Bluetooth. But the range is poor and the sound quality questionable.
But what if a Raspberry Pi could do the same thing? What if it could receive audio streamed over Wifi, from a tablet, or my phone, or a laptop etc?… turns out it can.
Apple’s AirPlay protocol is usually used by iTunes to allow it to communicate with Apple TV and is the backbone of the project. However as luck would have it, going beyond Apple and iTunes it is possible for other devices to use Airplay including a variety of Android apps and a few windows apps. There is also a linux project, shairport that allows linux devices to receive Airplay streams and play them.
This is the point I have to fess up and say that I should have written up the Raspberry Pi part of this as I was doing it – but hey ho, it’ a month later so this will lack detail but at least give a general overview.
The challenge with the Pi is simple enough: Get it running headless with shairport (which provides the audio listening service and outputs the audio through the Pi’s 3.5mm jack) running as a service from startup. And in fact, using the raspbian distribution this is relatively easy. However there were some issues: First, I quickly discovered that the Edimax micro USB wifi adapter I had, didn’t have the range to reach my router through a couple of walls. So I swapped it for a Ralink wifi usb adapter with a proper stick aerial I had lying around – but could I get the drivers to work? Could I hell!! Additionally, even without this problem and without launching a GUI, raspbian isn’t that quick to boot and I like quick booting!
For that reason I decided to do it again, using ArchLinux. ArchLinux boots fast and I knew from a previous project that the Ralink wifi adapter could be made to work.
Steps to Setup the Pi
So the steps to success on the Pi
- Install Archlinux
- Get Wifi Working
- Get a build environment working
- Download shairport package
- Compile
- Configure to get it working
To Install ArchLinux you can download an sd card image from here. And then bring the packages up to date using the instructions here. The most important section is that about updating the system. Don’t worry about installing additional software or a desktop environment.
Next – get the wifi working.
This page provides a guide to setting up wifi on ArchLinux. Personally I found the wpa_supplicant approach a real arse – in fact I didn’t get it working. By comparison the automatic Netctl approach mentioned, is very straightforward.
Now that the wifi is working, check that it can get a wifi signal from wherever in the house you tend to position the Pi. If you can’t and you’re using a micro adapter, you may need to change to an adapter with a larger / proper aerial.
Next, some development tools are required. The first command below installs gcc, make etc… the second provides support for git, which is where the shairport source repository is stored.
# sudo pacman -Sy base-devel # sudo pacman git
My own efforts from this point got a certain distance, including a lot of mucking around with perl packages and cpan, but I was unable to unravel is completely. However, this blog post by Orson Tyrell – works extremely well.
At the end of this, you should have shairport installed and set to automatically startup at system boot.
You may well find that when you send a test signal it is very quiet.
If so, launch
# alsamixer
Set the volume to 100% or close and exit.
If you then call
# alsactl store
(you may need to use sudo) then this volume will be stored away and reused after a reboot.
In Part 2 – I’ll talk a bit about broadcasting to the Pi from other devices.
There were quite a lot of pages used in doing this, but the key ones were:
http://orsontyrell.blogspot.co.uk/2013/11/airplay-to-arch-linux-raspberry-pi-via.html
http://engineer.john-whittington.co.uk/2013/05/airpi-diy-airplay-speakers-using-shairport-and-a-raspberry-pi-updated/
And on raspbian
http://trouch.com/2012/08/03/airpi-airplay-audio-with-raspberry/
https://github.com/abrasive/shairport
… thanks to all.