The Raspberry Pi is a full Linux computer that’s about the size of a credit card and sells for $25–$35. It has a 700MHz ARM CPU and 512MB of RAM. Because of its capabilities and its low price, it has become wildly popular with the hobbyist market. Some people have done ingenious things with theirs.

If you’re like me, you’ve toyed with the idea of getting an AirPort Express but the $100 price seemed a bit steep, since all you really wanted to do with it is play audio using AirPlay. Sound familiar?

raspberry pi project main image

Well, guess what – you can turn a Raspberry Pi into an AirPlay speaker for around $45. That’s what you’re going to do in this tutorial. By the end of it you’ll have your very own Raspberry Pi AirPlay receiver!

After that, you’ll look further into the usefulness of the Raspberry Pi to iOS developers. Most complex apps need some sort of backend web service and a Raspberry Pi is a perfect server for development or perhaps even a small app. You’ll install the Apache web server, MySQL database and PHP, then use them to create a web service that you can access from an iOS chat app.


You’re going to download and run Shairport, a project that emulates the proprietary Apple AirPort protocol with software so that your Raspberry Pi will appear as an AirPort receiver on your network. The AirPlay protocol isn’t something that Apple publishes, but James Laird and other clever coders have reverse engineered it and created an executable that will appear as an AirPlay receiver. You can check out the open source project here.

This project originally appeared on at raywenderlich.com. Reprinted and updated with permission.

Steps

Step #1: A Brief Introduction to the Raspberry Pi

PrevNext
Raspberry Pi AirPlay Speaker
  • The Raspberry Pi is quite a little device. Although not the first of its kind, it has exploded in popularity because it has combined very capable, full feature computer hardware in a tiny form factor at a unbelievable price ($25-35). It has a 700Mhz ARM CPU with 512 MB of RAM with all the standard connectors that you would expect to come on a computer like an analog audio out port, 2 USB ports, wired ethernet, and analog RCA and digital HDMI video out for connecting a monitor. In addition to that, it has a bank of GPIO (General Purpose Input/Output) pins for connecting to hardware. These can be used for anything from simply controlling a LED to a motor to reading from any number of different kinds of sensors.
  • There is also a SD Card reader onboard. There is no onboard memory on the Raspberry Pi so all files are kept on a SD Card. Think of it exactly like the hard drive in your computer.
  • The final component of interest onboard is the micro-USB port. This is not used for data or connecting to your computer but rather as the power for the Raspberry Pi. The Raspberry Pi used very little power and 5V that USB provides is enough to power it as long as you aren’t using any addition devices plugged into the Pi that use a lot of power.
  • Here is a quick start guide to help you get started if need to help hooking it up and powering it on.

Step #2: Loading the Operating System

PrevNext
Raspberry Pi AirPlay Speaker
  • Every computer needs an operating system (OS). Your Mac runs Mac OS X and your iPhone, iPad, etc run iOS. The OS handles all the interfacing to the hardware so that “apps” can just do what they need to do.
  • A Raspberry Pi can run many different OSs, but the one you’re going to use here is Raspbian. Raspbian is a version of the Debian Linux distribution dedicated to running on a Raspberry Pi. Therefore it’s perfect for this tutorial!
  • To get your Raspberry Pi up and running with Raspbian, you’ll first need to download and install it. This is done by downloading something called an “image”. An image is simply a snapshot of a pre-installed copy of Raspbian, ready to copy over onto your memory card to run on your Raspberry Pi.

Step #3: Downloading the Image

PrevNext
Raspberry Pi AirPlay Speaker
  • Go here and look down the page for the Raw Images section. Under the Raspbian sub-section, download the image either as the direct ZIP or using the torrent. The torrent is downloaded using the bit torrent protocol. If you don’t know what that is just download the image directly.
  • The zip file contains a disk image (a .img file) of the entire installed operating system. Download and extract the image from the zip anywhere you have access to it. Then you are just going to copy the data in the disk image on the SD card (in the next section).
  • The default root (administrator) username and password are:
    Username: pi Password: raspberry
  • Make a note of these, because they’ll come into play later.

Step #4: Loading the Image onto the Card: Part 1

PrevNext
Raspberry Pi AirPlay Speaker
  • In order to copy the image on the card, you’re going to have to use the terminal or command line. Though all the typing is a little intimating at first, in some ways it’s a more powerful interface that a standard GUI interface for some tasks. Don’t be afraid of it, the hardest part is just memorizing the commands but hey, that’s what this tutorial is here for!
  • Open a Terminal window. You can do this quickly by hitting Command+Space and typing terminal into Spotlight. Run the following command, without the memory card inserted:
    df -h
  • As you can see, the df command shows the free space on each disk. Notice that the command also shows the address of each disk (e.g. /dev/disk0s2).

Step #5: Loading the Image onto the Card: Part 2

PrevNext
Raspberry Pi AirPlay SpeakerRaspberry Pi AirPlay SpeakerRaspberry Pi AirPlay Speaker
  • You’re going to use df to discover the address of your memory card. Insert the memory card now into either the memory card reader on your Mac or into a separate reader that’s connected to your Mac. Run the df –h command again, and you should see something like Image 1.
  • You should see a new disk, something like /dev/disk1s1, that matches the size of your memory card. The description of the new disk is the card’s address. Unmount the memory card’s disk with this command:
    sudo diskutil unmount /dev/disk1s1
  • Next you need to get the device address. This is the address of you memory card “reader”. To do this, take the address you were using before, in my case /dev/disk1s1, remove the s1 portion from the end of it and add r in front of disk so that /dev/disk1s1 becomes /dev/rdisk1. The two addresses are different in that /dev/disk1s1 is the address where first file system partition is mounted. /dev/rdisk1 is the address of the memory card reader device (has nothing to do with the card that is inserted at the time).
  • You can now write the image file that you downloaded above onto the memory card. Run the following command:
    sudo dd bs=5m if=~/Downloads/wheezy-raspbian.img of=/dev/YOURDISKNAME
  • This is where all the magic happens. You are using the dd command to copy an input stream byte-for-byte. You can’t use a standard drag-and-drop copy to move the files over since special formatting for system boot partitions are different and the Raspberry Pi wouldn’t know that it was a boot partition if you just dragged them over. Let’s take a look at each component of this command:
    • bs specifies the block size that the dd tool uses. Specifying a larger block size helps copy the image faster.
    • if specifies the input file. You point this to the image file you downloaded earlier.
    • of specifies the output file. You set this to the disk name you came up with earlier, /dev/rdisk1.
  • Hit enter to run the command. It will take a few minutes to complete – use this time to start building that rocking playlist you’ll soon be streaming!
  • When the command is done running, you’ll see something similar to what is in the second image.

Step #6: Loading the Image onto the Card: Part 3

PrevNext
Raspberry Pi AirPlay SpeakerRaspberry Pi AirPlay SpeakerRaspberry Pi AirPlay SpeakerRaspberry Pi AirPlay Speaker
  • Now pop the memory card out of your computer and put it into the Raspberry Pi.
  • Plug in the Ethernet cable and then the micro-USB cable for power. Connect the other end of the ethernet cable to your router and the micro-USB cable into a computer or power adapter (like the one that comes with an iPhone or iPad).
  • Now how do you get access to the Raspberry Pi? In this tutorial, you are going to be setting up the Raspberry Pi computer without a monitor, keyboard or mouse. Instead you’re going to remotely log into it from your Mac.

Step #7: SSHing into Raspberry Pi: Part 1

PrevNext
Raspberry Pi AirPlay Speaker
  • SSH stand for Secure SHell and is a way to open a terminal session into a remote computer, letting you run a command just as if you were physically on that computer. This is convenient when you need to makes changes to a computer you don’t have physical access to, or, as in your case, one without a keyboard or monitor.
  • You’re going to be SSHing into the Raspberry Pi for a couple of reasons:
    • Plugging in a mouse, keyboard, and monitor can add a large power drain that can lead to hardware failures and/or erratic behavior.
    • SSH is a important and good skill to learn.
  • Before you can SSH into your Raspberry Pi, you need to figure out its IP address. This can be tricky, because by default it will use DHCP to automatically obtain an IP address. So you don’t know what it is. There’s a few ways to get the IP address, but I recommend you download an iPhone app called Fing that will do a network scan.
  • Download Fing and make sure your iPhone/iPad and the Raspberry Pi are on the same network. Run the app and you should see a list of all the devices on the network (see image).

Step #8: SSHing into Raspberry Pi: Part 2

PrevNext
Raspberry Pi AirPlay SpeakerRaspberry Pi AirPlay SpeakerRaspberry Pi AirPlay SpeakerRaspberry Pi AirPlay Speaker
  • Though I don’t recommend it, if you want to try connecting a monitor, keyboard and mouse to the Raspberry Pi, then you can use ifconfig, a Linux command, to list information about your network connections. If you do decide to go with route, connect a monitor and a keyboard and power it on. Once you’ve got your components connected and power on, you should be taken directly to a login where you should enter your username and password.
  • Once logged in, you should be left at a terminal command line and should be able to type ifconfig (image 3).
  • It is a little pointless to ssh in if you went through all the trouble to connect a keyboard, mouse, and monitor but it is still a good skill to learn.

Step #9: SSHing into Raspberry Pi: Part 3

PrevNext
Raspberry Pi AirPlay SpeakerRaspberry Pi AirPlay SpeakerRaspberry Pi AirPlay Speaker
  • Once you’ve obtained the IP address in one of these ways, you can SSH into the Raspberry Pi. Let’s say your IP address is 192.168.1.10. In the Terminal window, type:
    ssh [email protected] // remember to replace with YOUR IP address
    You’ll get what you can see in the first image.
  • Type yes and hit enter. Terminal will now ask you for a password, which by default is raspberry. Enter that password and you should be remotely logged into the Raspberry Pi and ready to go.
  • NOTE: If you can’t seem to find your Raspberry Pi on the network, then it might be because it is not booting properly. This can happen if you aren’t supplying enough power to the device. You can check for that by plugging in a monitor via HDMI. If it continually reboots, then power is likely the problem.

Step #10: Expanding the File System: Part 1

PrevNext
Raspberry Pi AirPlay SpeakerRaspberry Pi AirPlay SpeakerRaspberry Pi AirPlay Speaker
  • Now you need to expand the file system to take up the entire size of the SD card. You see, the image you loaded onto the SD card was of minimal size so that it would be quick to download and fit on all SD card sizes. Since we copied over the contents of the disk image onto the SD card, the partition size is the exact size of the disk image even though the size of your SD card might be bigger. But this leaves you with little space with which to work.
  • To set up the AirPlay receiver components and all the server components, you’re going to need more space. Luckily, it couldn’t be simpler to resize the partition to take advantage of the entire card. SSH back into your Raspberry Pi if you aren’t there already and run the following command:
    sudo raspi-config
  • When the config menu comes up, select expand_rootfs.

Step #11: Expanding the File System: Part 2

PrevNext
Raspberry Pi AirPlay SpeakerRaspberry Pi AirPlay SpeakerRaspberry Pi AirPlay SpeakerRaspberry Pi AirPlay Speaker
  • You’ll then see the following (image 1), letting you know that the root partition has been resized.
  • When it asks if you want to reboot now, select Yes.
  • You’ll have to wait a minute or two and then SSH back in. You should now have plenty of space for the server components.

Step #12: Setting Up the Project

PrevNext
Raspberry Pi AirPlay Speaker

Now that you’re logged into the Raspberry Pi, you need to perform some basic setup. These include:

  • Update the current system.
  • Update the audio settings so sound comes out of the green, mini-jack port instead of the HDMI port.
  • Install some needed libraries for the Shairport project.
  • Install the Shairport project.
  • Daemonize the Shairport project so that it start at startup

Let’s get cracking!

Step #13: Updating the Packages

PrevNext
Raspberry Pi AirPlay Speaker
  • First you should update the current package sources and packages. This is kind of like running Windows Update or Software Update on OS X. With Raspbian, the tool apt-get handles all software packages for you.
  • Run the following two commands, one at a time:
    sudo apt-get update
    sudo apt-get upgrade
  • The first command, update, updates the list of packages and the versions. The second command, upgrade, compares that new information to all the packages you currently have installed and upgrades them if an upgrade is available.
  • You will probably notice quite a few packages being updated. Just sit tight!

Step #14: Changing the Default Audio Port

PrevNext
Raspberry Pi AirPlay Speaker
  • By default, the audio comes out of the HDMI port on the Raspberry Pi. You want to change that to the mini-audio jack since pretty much anything that connects to audio can connect to a mini-audio jack and if you leave it set to HDMI, you’d probably have to have it hooked up to a TV all the time. Run the following command:
    sudo amixer cset numid=3 1
  • You should see the results as shown in the image above.
  • The amixer command lets you view and set the options of sound cards. Here, you are setting the third soundcard element to 1, thereby activating the analog output for the Raspberry Pi. That might all sound magic, and, well, it is really. You just need to know that command. But fortunately I’ve found that out for you – that’s why you’re here!

Step #15: Installing the Prerequisites

PrevNext
Raspberry Pi AirPlay Speaker
  • The Shairport project you are going to be using has a number of prerequisites. Enter the following command to download and install these prerequisites using the same mechanism you used to update the system earlier:
    sudo apt-get install git libao-dev libssl-dev libcrypt-openssl-rsa-perl libio-socket-inet6-perl libwww-perl avahi-utils libmodule-build-perl
  • This is a pretty long command but its actually pretty simple. Lets look at each individual part:
    • sudo – Runs the next command in administrator mode
    • apt-get – This the package manager program
    • install – Tells the package manager program to install the following libraries
    • git libao-dev libssl-dev libcrypt-openssl-rsa-perl libio-socket-inet6-perl libwww-perl avahi-utils libmodule-build-perl – These are the libraries we are installing. You can see there’s a couple here like git, some security/encryption libraries, and some libraries for the Perl programming language.
  • You’ll see what is shown in the image above.
  • It’s telling you what will be installed. This will include the libraries you asked specifically to be installed, and any prerequisites.

Step #16: Installing Perl Net-SDP

PrevNext
Raspberry Pi AirPlay SpeakerRaspberry Pi AirPlay SpeakerRaspberry Pi AirPlay Speaker
  • Since iOS 6, AirPlay has used the SDP protocol. The Perl Net-SDP project will help communicate using this protocol. So you’re now going to install it.
  • Run the following commands (still in your Raspberry Pi SSH session!):
    git clone https://github.com/njh/perl-net-sdp.git perl-net-sdp
  • This clones the GitHub repository of Perl Net-SDP so that now you have a copy of it on your Raspberry Pi. You’ll get the following output shown in the first image.
  • Now enter the following commands, one line at a time:
    cd perl-net-sdp
    perl Build.PL
    sudo ./Build
    sudo ./Build test
    sudo ./Build install
    cd ..
  • These commands compile and install the components of Perl Net-SDP in the proper locations.

Step #17: Setting up your Raspberry Pi AirPlay receiver

PrevNext
Raspberry Pi AirPlay Speaker
  • You have reached the meat and potatoes of the project, the purpose of all of that setting up you just accomplished. Once you download and run Shairport, it’s only a few simple steps until you hear music!

Installing and Running Shairport

  • Run these commands, which will clone the Shairport repository, then build the software:
    git clone https://github.com/hendrikw82/shairport.git
    cd shairport
    make
  • Then run one last command to start the Shairport script:
    ./shairport.pl -a RayPi
  • The -a command specifies the name of the AirPlay receiver, which in this case you are calling RayPi.

Step #18: Live From Your Raspberry Pi…

PrevNext
Raspberry Pi AirPlay Speaker
  • Now if you open any audio or music app on your iOS device or open iTunes on your Mac, you should see RayPi on the list of AirPlay devices!
  • When you’re done, hit Control+C to stop the process. You can change RayPi in the previous command to anything you want to call your AirPlay receiver.
  • As it stands right now, every time you want to use Shairport, you have to start up the Raspberry Pi, SSH into it and run the Shairport program. That sounds horrible to me. What can you do about it?
  • *Deep daemon voice* DAEMONIZE IT *End deep daemon voice*
    *cough*

Step #19: Daemonizing Shairport

PrevNext
Raspberry Pi AirPlay Speaker
  • That sounds evil, doesn’t it? Actually, it’s quite the opposite – daemonizing will make your life much simpler.
  • So what is it? Daemons are tasks on Unix and Linux systems that run in the background and typically begin at system startup. It’s the perfect solution to your problem. You can create a daemonize the executable you just ran above so that it runs in the background and is started right at startup; that way you never have to worry about starting it up yourself.
  • SSH into the Raspberry Pi (if you aren’t still) and enter following commands to copy and configure the necessary files:
    cd shairport
    sudo make install
    sudo cp shairport.init.sample /etc/init.d/shairport
  • This copies a sample init file to the daemon directory, the daemon directory was create by the ‘make install’ command.
  • Now enter these commands, one line at a time:
    cd /etc/init.d
    sudo chmod a+x shairport
    sudo update-rc.d shairport defaults
  • Here you move to the daemon directory, change the permissions and then “install” the Shairport init file with the default options.
  • Now enter the following commands, one line at a time:
    sudo vi shairport
    DAEMON_ARGS="-w $PIDFILE -a RayPi"
  • You can use nano or any other text editor here if you’re more comfortable with that, but what you are doing is going into the Shairport config file and adding the -a RayPi part to the DAEMON_ARGS option. Just as when you trigger the script manually, you are modifying this to set the default name of the AirPlay receiver.
  • If you are using the vi text editor, hit escape, then type :wq and hit enter to save the changes. Now whenever you startup the Raspberry Pi, it will start the Shairport executable.

  • Adam

    On step 14, you can change the default audio output in raspi-config under advanced options, instead of using the terminal command.

  • Paul M.

    Wonder if you could do this to turn a regular speaker to pick up on a Sonos device

    • Sarchasm

      Yes.

      • Paul M.

        Where do you recommend starting? I don’t have a Pi yet but this would be my first programming project

        • Sarchasm

          Google.there are a number of Sonos integration projects.

          Getting Sonos integration isn’t a simple task, there are a number of steps. I am not a Sonos user, I have gone open-source to achieve a similar result.

          If your music library exists outside of Sonos, you might find it easier to just access the library and use a product like Moode (http://moodeaudio.org)

          If you have an existing powered speaker, you can use a Pi, either on its own or with a DAC, but if you need amplification, I can recommend the HiFiBerry Amp+. It’s a very competent 50W Class-D amp tha piggy-backs the Pi and offers you a fully-digital path, right to the speaker terminals.

          HiFiBerry DACs and Amps are top-class, yet affordable. https://www.hifiberry.com

  • AliceSPratt

    Last few days to get smarts deal with makezine

    ……………….. Find Get More </b

  • RachelTHarris

    Available Real place jobs makezine Next Tip Here

  • Throffes1992

    < ??????.+ zeldadungeon+ *********<-Part-time working I Saw at the draft which said [email protected] < Now Go R­e­­a­d M­o­r­e

    <???????????????????????????????

    20

  • Throffes1992

    < ??????.+ zeldadungeon+ *********<-Part-time working I Saw at the draft which said [email protected] < Now Go R­e­­a­d M­o­r­e

    <???????????????????????????????

    25

  • JessicaGMahoney

    [email protected] Real place jobs makezine…. Next Tip Here

  • Michelle J

    ******* i like me makezine < Find Get More

  • WilliamBParkhill

    *******Big time hit th e makezine ……..Find Here

  • Christine F

    Yes Offer Of The Day by zine < Start Work Online

  • StephenKWilliams

    [email protected] Real place kezine…. Next Tip

  • Sean Townsend

    Shairport (or maybe even airplay itself) is way too unreliable and shairport is no longer under development, there are a few branches off it such as shairport sync but they still suffer from the same problem. I have had both the original shairport and shairport sync working on my Pi and both stop working every few days or so and i have to reboot the Pi to get it working again.

    • Sarchasm

      You want to look at Volumio or Moode -my Pi stays up for months at a time and streaming AirPlay has been flawless so far.

      • Sean Townsend

        I dont think either of those are compatible with Spotify which is the only way i listen to music, and chances are if they did they would be using the same library (shairport) for airplay support, I am very surprised you get months before a reboot on you Pi when using shairport.

        • Sarchasm

          Yep. Volumio sure does. I think RuneAudio does too. But I live in a country where Spotify isn’t available, so it wasn’t a priority with me.

          https://volumio.org/volumio-1-5-best-volumio-audiophile-player-ever/

          As to reboots, the last time I rebooted was to upgrade to the new version. Moode does use shairport, but just because a product is a few years old is no indication that its faulty – if it’s developed right, there’s no reason to update it if there are no bugs. I have git repositories that haven’t been updated for more than 3 years – but it’s still active in downloads and forks.

          I suspect the rest of your implimentation – mine’s just fine – no issues, no hangs or lockups – but then, all my Pi are Model 2Bs. Trying to run an MPD iteration on a ‘classic’ Model 1 is an exercise in frustration.

    • Mike Brady

      Shairport and Shairport Sync have diverged quite a bit, so it’s unlikely they share the same fault. In my experience, the usual cause of this kind of a problem is the WiFi network. Some WiFi drivers drop the network after a few days and never find their way back – it’s a know problem.

      Volumio and Moode both use Shairport Sync, AFAIK. So, with respect, I think you should be looking elsewhere for the source of the unreliability…

  • AndyTomas

    Hunter . even though Valerie `s article is super… yesterday I got a great McLaren F1 after I been earnin $4774 this-past/5 weeks and-also, ten-grand lass month . without a doubt its the best-job Ive ever done . I began this seven months/ago and right away was earning over $85… per-hour . blog here

    Check it out,,,…,

    >>>>> www●MakeYourownboss●C0m-Clickthis

  • Eduardo Minoru Hirose

    it’s easier and way better to use Volumio (https://volumio.org/) since it’s airplay complatible and have a lot of more interesting functions, even compatible with android too.

  • HelenaMSteere

    My first choice makezine
    …………. Find Get More </b

  • CarlosLMcCoy

    My first choice makezine
    …………. Find Get More </b

  • ronaldobi

    That’s Real Article by zm ez ne View Info ……………

  • longwing
  • Sarchasm

    I’m surprised at make, being so far behind the technology curve on this.

    There are multiple specialised disturbs for this, such as Volumio, MusicBox, and my personal favourite, Moode, which extend and enhance the audio experience with web front-ends, centralised music databases, PC, phone and tablet interfaces, multi-room implementations, then there’s the audiophile end of the market with i2s and USB DACs, Pi-mounted Class-D amplifiers, etc.

    Really this ‘make’ is a couple of years behind the current tech. I don’t have a TV or stereo or boom box in (and outside) the house that isn’t equipped with a Pi, a DAC or amp and a piece of MPD-compliant software that fully integrates the media centre database to every room that needs it.

    And I question the $45 price tag -with a bare Pi at $35, then a 2A USB wall-wart, a case, an SD card and a wifi adapter, you ain’t getting out the door for much under $70. If it was about being cheaper than an Airport device, you have missed the point -the Pi should be better, stronger, faster, and recyclable.

    My wife uses her Pi in the workroom to listen to Internet radio while she sews, I listen to podcasts while I cook and my daughter watches documentaries on her 23″ monitor in the bedroom, as well as streams her iPhone and.

    There’s so much more you can do than emulate an Airport.

  • josephby

    Handy tip – if you find the volume too quiet you can use _amixer_ to adjust the volume from the command line (http://unix.stackexchange.com/questions/21089/how-to-use-command-line-to-change-volume)

  • Vinay Adithya

    does the pi need to be connected to the internet while setting this up?

    • Parth Saxena

      I originally believed that this speaker would run through Bluetooth, however it does not. All that is required is that the device which will airplay to the raspberry pi and the raspberry pi itself need to be on the same WiFi network.

      If you are using your phone to airplay, what i am doing is turning on my Hotspot then setting my Pi to defaultly connect to my Hotspot. Now I can “artificially” use my raspberry pi as a bluetooth speaker.

  • GK

    Best tutorial, still works after all these years, assuming this was written many years ago. Wasted my time on OSMC. Simple is always the best. My original Pi 1 was gathering dust and now it is a airplay receiver.