Best Raspberry Pi Projects
Our pick of the most fun and useful things you can do with your Raspberry Pi
Setting up your Raspberry Pi
Using a mobile device is a useful way to test that the back end of your setup is working. The next steps show you how to play your music via your Raspberry Pi.
We’re now going to configure the software on the Raspberry Pi that turns it into a Squeezebox Player, capable of receiving and playing audio from the Squeezebox app running on your Squeezebox server. Configuring this software is tricky, but we’ve got a trick to ensure that you need to do it only once.
Before that, we need to transform the Pi into a Squeezebox player, and the first step is to increase the output volume of the Pi. Type ‘sudo alsamixer’ and you should see a curious ASCII volume control (pictured below). Check that Alsamixer has recognised your sound card by looking at the information in the top-left of the screen. It will probably say ‘bcm2835 ALSA’ (or bcm2836 for the new Pi 2), which is the Pi’s built-in audio.
Use Alsamixer to adjust your Raspberry Pi’s audio input and output levels
To change this to the USB sound card we decided to use for this project (see Part 1), press F6 and use the down arrow to highlight the C-Media option, then hit Enter. As our USB sound card has both a stereo output and a microphone input, it has two levels. Alsamixer should highlight the Headphone level by default (the blue bar will have red text and red arrow brackets) so you need only press the up arrow until the level bar is at 100. Oddly, you then have to exit Alsamixer (press Escape) and only then save the change you’ve made. Type ‘sudo alsactl store’ and hit Enter.
Your next job is to install a few audio libraries so that all the files – no matter what their format – can be played by the Pi. Type ‘sudo apt-get install libfaad2 libflac-dev libmad0’ (without the quotes) and hit Enter. You’ll have to agree to download and install at least one of these libraries – type ‘y’ and press Enter when asked.
Set up Squeezelite
Squeezelite is an open-source project that can turn a Linux computer into a Squeezebox player, and it comes in different flavours depending on the hardware you wish to run it on; you’ll need the ‘armv6hf’ version. You’ll have to download the file to the temporary folder of your Pi, then move this file to the /usr/bin folder. After this you’ll need to change to the /usr/bin folder, and make the file you downloaded executable. To perform all these steps, type the following lines into your Pi’s console:
cd /tmp
wget http://squeezelite-downloads.googlecode.com/git/squeezelite-armv6hf
sudo mv squeezelite-armv6hf /usr/bin
cd /usr/bin
sudo chmod a+x squeezelite-armv6hf
Now you should test that Squeezelite works, but you’ll need two pieces of information: the code for your USB sound card and the IP address of the Squeezelite server. You can find the USB sound card’s code with the command sudo ./squeezelite-armv6hf –l. A load of options will appear on your screen, and you need to find an entry for the C-Media Front output. In our case the code for this output is ‘front:CARD=Set,DEV=0’. You can find the IP address of the Squeezebox server by either logging into your router or loading the server’s web page in a browser (we set a static IP address for this server in last month’s guide, which was 192.168.1.145.) Now you can give Squeezelite a spin:
./squeezelite-armv6hf -o
front:CARD=Set,DEV=0 -s 192.168.1.145
Replacing the IP address at the end with your Squeezebox server’s IP. Oddly, if everything is working, the Pi should look as though it’s crashed, as it won’t give you a new command prompt. Don’t panic, as this means that Squeezelite is running.
If Squeezelite launches properly, it will probably look as though your Pi is broken, but check the Squeezebox web page and you’ll see that everything is running fine
You can check this is the case by opening Squeezebox’s web page; you should see ‘Squeezelite’ listed in the ‘players’ drop-down menu at the top-right of the page. Play a track via the Squeezebox web page and it should start playing through the speakers plugged into the USB sound card of your Pi. If not, check your speakers and their connection with the USB sound card (we found we had to rotate the speakers’ mini-jack connection within the USB sound card’s headphone port to make a reliable connection).
Running Squeezelite on startup
The next step is to ensure Squeezelite runs on startup. To do this you should use a script created by a Dutch VW enthusiast called Gerrelt. Press Ctrl-Z to end Squeezelite and your Pi should present its command prompt again. You’ll need to download a script file, so change to the temp folder again, download the script, move it, go to its new location and make it executable, just as you did with the original Squeezelite file:
cd /tmp
sudo wget http://www.gerrelt.nl/RaspberryPi/squeezelitehf.sh
sudo mv squeezelitehf.sh /etc/init.d/squeezelite
cd /etc/init.d
sudo chmod a+x squeezelite
sudo update-rc.d squeezelite defaults
The last command tells the Pi there’s an executable script to run during startup; the ‘update-rc.d’ command is linked to the /etc/init.d folder, which contains items to be loaded during the bootup sequence. However, although you’ve ensured that Squeezelite launches every time the Pi boots up, Gerrelt’s script can’t automatically set the correct settings for your Pi or network, so you’ll have to edit the script, by typing ‘sudo nano squeezelite.’
Hunt through Gerrelt’s amazing Squeezelite script for the few options you need to tweak
In the script, the first option to check is that Squeezelite (on your Pi) knows which port your Squeezebox server is using. Find the entry for SB_SERVER_CLI_PORT. Gerrelt’s default port is 9090, but the web page of our Squeezebox server says the port is 9002, so we needed to change the value.
The default sound card is the Pi’s integrated model, so find the entry for ‘SL_SOUND CARD’ and change the entry from “sysdefault:CARD=ALSA” to “front:CARD=Set,DEV=0” (or whatever code your USB sound card uses). Note once again that you’re declaring the sound card as a variable, so you need to include those double-quotes.
Quality control
For better audio quality, Gerrelt recommends you enable the ‘SL_ALSA_PARAMS=”80″‘ entry that increases the size of Squeezelite’s output buffer. It’s commented out by default, so you need only remove the hash symbol. This option can be tricky to find, so press Ctrl-W (to search), type ‘params’ and hit Enter; the option should be highlighted. To save and exit, press Crtl-X, Y and then Enter.
A simple script tweak can improve audio quality
You can test Gerrelt’s script by typing ‘sudo ./squeezelite start’ and pressing Enter. This is a much more elegant way to start a service on a Pi than the quick and dirty method we used earlier: the Pi will report the settings Squeezelite has used and even give you a command prompt so you can continue to work. You can check that Squeezelite is working by logging into your Squeezebox server and opening the Squeezebox player menu (in the top-right). Instead of the player being called ‘Squeezeslave’ it should now be called ‘raspberrypi’. That’s because Gerrelt’s clever script reports the name of the Pi to the Squeezebox server. Perform a final check by typing ‘sudo reboot’ and hitting Enter. The Pi should reboot and, once it has done so, your Squeezebox server should again see a Squeezebox player called ‘raspberrypi’.
Finally, Gerrelt recommends setting an automatic reboot each night (to clear some gremlin that can interfere with Squeezelite). Edit the crontab (which sets automated routines) by typing ‘sudo crontab –e’ and hitting Enter. On a new line at the end of the file, add ‘0 3 * * * sudo /sbin/shutdown –r now’ (without the quotes, but pay attention to the spaces) to reboot the Pi at 3am every day. Then save and exit by typing Ctrl-X, Y and then Enter.
Clone yours
We promised that you wouldn’t have to go through the same setup process for every multiroom audio speaker you wish to create, and here’s how. Power down your Pi (sudo shutdown –h now) and unplug it. Take out the microSD card, put it back in your PC and fire up Win32 Disk Imager, the tool you used to write Raspbian on to the card in part 1. You’ll need to cheat the software a little by selecting a file in a memorable location on your hard disk – perhaps the original Raspbian image.
Then edit the name in the Image File field from ‘…/2014-09-09’wheezy-raspbian.img’ to something like ‘…/audiopi.img’ and click the Read button (don’t click Write at any point during this process). Win32 Disk Imager will make a 1:1 clone of your SD card, so if you used an 8GB card (as we did) the resulting image will be 8GB; ensure your memorable location has sufficient space. You then use this image (and the Write button of Win32 Disk Image) when writing to the SD card of your other Pi-based wireless speaker units.
Use Win32 Disk Imager to clone your SD card. Type in a location and name for the clone file and press the ‘Read’ button
However, you’ll now have a bunch of speaker controller units all called ‘raspberrypi’. You’ll therefore need to change the name of each unit so you know where to send your music. Once a cloned Pi has booted up and you’ve logged on (with your updated password, remember), type ‘sudo raspi-config’ and hit Enter. Head to ‘8 Advanced Options’ and then ‘A2 Hostname’. Read the advice on the next screen and hit Enter to set a new host name for the Pi. This is the name that Gerrelt’s script reports to your Squeezebox server, so chose something like ‘Lroom1’ (remembering that if you use a character that’s not a letter, number or hyphen, the Pi will behave very oddly, just as the advice screen warns). Reboot and the updated host name should appear in the Squeezebox players menu of your Squeezebox server.
Repeat the host name update for each Pi and you can now use Logitech’s official app to control your Pi-based speakers. Each speaker will show up by (host) name in the app’s main screen, and you can set a playlist for each independently.