Coupled with an onboard display, the Raspberry Pi makes a great choice for an internet radio receiver. Follow our guide to start streaming stations
The radio, a remarkable invention, was all but supplanted as the centre of home entertainment by the television, and as a source of up-to-the-minute news by the internet. Its biggest drawback, aside from sometimes questionable audio quality, is its limited range. Even today, with the rise of digital radio, channels are typically national rather than international in their reach.
Internet radio is a different beast. Streaming over a global network rather than the airwaves, a single station can reach almost anywhere on Earth if only the receiver has a device capable of decoding and playing the audio stream. A device, in fact, like the Raspberry Pi.
The Pi is a great choice for an internet radio receiver. It’s small, unobtrusive and silent in operation, while its analogue audio output means it can be connected to powered speakers or an existing hi-fi setup to extend its capabilities. Coupled with an onboard display, you won’t even need a TV or monitor to control the playback.
Installing the software
The software that drives the PiFace Control & Display is available through the Apt package management system in Raspbian. Log into your Pi, and type the following commands at the terminal to download and install the required software:
sudo apt-get update↵
sudo apt-get upgrade↵
sudo apt-get install python-pifacecad python3-pifacecad mplayer↵
This installs versions of the PiFace software for both Python 2 and Python 3, allowing you to make use of it regardless of which version of the language you prefer. Note, however, that the internet radio utility that comes with the PiFace works only with Python 3 and won’t run at all under Python 2. This isn’t a distinction you need to worry about if you just want to stream some music; both versions of Python are installed by default in Raspbian. The command-line music player Mplayer is also installed via the last command.
Before you can begin using its software, the PiFace requires a configuration change on the Pi: the enabling of the Serial Peripheral Interface, or SPI, a feature of the generalpurpose input-output (GPIO) header. To load the Raspberry Pi Software Configuration Tool, type the following at the terminal:
sudo raspi-config↵
At the menu, use the cursor keys to scroll to the Advanced Options choice and press Enter to confirm. At the next screen, scroll to the option labelled ‘A5 SPI’ and press Enter again. A window will appear confirming the current status of SPI. Usually, this will be disabled and you can press Enter to toggle the setting to Yes. If the setting is already enabled, be sure to move the cursor off the No option before pressing Enter. Confirm the change again with Enter, then exit the menus by pressing the Tab key until Finish is highlighted, and then pressing Enter.
If the Raspberry Pi Software Configuration Tool prompts you to reboot – it may not, depending on any other changes you’ve made – choose No and press Enter. As you’re about to install the board itself, you should instead manually shut down the Pi with the following command before removing the Micro USB power and any other connected cables:
sudo halt↵
The shutdown process will take a short while to complete; check the Pi’s status LEDs to see if it’s finished before pulling the power. When the Pi has shut down, all LEDs except ‘PWR’ should be switched off.
Installing the PiFace
Although it’s possible to stream internet radio on the Pi without any additional hardware, it’s not convenient. You need to have it connected to a TV or monitor at all times to see what you’re doing, and you need a keyboard and mouse to perform even simple tasks such as pausing the stream or skipping to a different station.
The PiFace Control & Display board, designed by a team led by honorary research fellow Dr Andrew Robinson specifically for use with the Pi, is an alternative to traditional control methods. The front of the board is dominated by a LCD panel offering two lines of up to 16 characters, while the edges feature five buttons and a three-way joystick. The board also includes an onboard infrared receiver designed to work with your existing remote controls with a little extra configuration.
The PiFace is designed to install on top of the Raspberry Pi as a piggy-back board, connecting to the GPIO header at the top-left of the board. Its lower board includes a supporting strut designed to sit on top of the Ethernet port to provide rigidity once installed. Although the board works perfectly well with the Ethernet-less Model A, it can wobble a little when held via that corner.
Whenever you’re installing hardware on to the Pi’s GPIO header, ensure you’ve shut down the Pi safely – using the ‘sudo halt’ command at the terminal – and removed the Micro USB power connector. Plugging devices into the GPIO header while the Pi is powered on is not advisable. The pins connect directly to various components of the Broadcom BCM2835 processor, and have no protection against shorts or spikes.
The Pi should be positioned face-up on a flat surface with the HDMI connector facing you. Take the PiFace, with the five buttons facing you and the joystick and infrared receiver furthest away, and carefully position the black connector on its underside over the Pi’s GPIO header.
Make sure it’s aligned correctly and press it down gently but firmly with your fingers to either edge of the board on the top of the connector. It should slide into place easily; if it feels like you’re having to apply excess pressure, remove the PiFace and check the Pi for bent pins before trying again.
The PiFace takes its power directly from the Pi, and needs no power adaptor of its own. This can, however, put a strain on lowerspecification power adaptors. If after installing the PiFace you find that the Pi begins to respond erratically to the keyboard – especially by skipping letters or repeating them as though you’ve held down the key – this is a sign that the power supply can’t cope. Try replacing it with one rated to a higher amperage, removing any overclock you have set on the Pi, or both.
With the PiFace firmly installed, and its seating double-checked to ensure it hasn’t missed any pins, you can reinsert the Micro USB cable and allow the Pi to boot back up again.
Testing the PiFace
When the Pi has loaded up, you may notice that the PiFace has remained switched off; this is normal. The PiFace’s display is controlled entirely from the Pi’s GPIO header via software. Until it receives an instruction from the software, it won’t do anything at all.
Before you start configuring the Pi to stream internet radio, it’s worth testing that the PiFace is properly installed and working. The canonical way of doing this is by using the system status script supplied with the software. Log into the Pi, and type this command at the terminal:
python3 /usr/share/doc/python3-pifacecad/examples/sysinfo.py↵
After a few seconds, the display on the PiFace should light up and display a message regarding the Pi’s IP address. A few more seconds later, and the screen will refresh to show the Pi’s currently assigned IP address – a handy way of finding it on the network if you haven’t assigned a static IP – and two statistics: the temperature of the Pi’s BCM2835 processor, as reported by its internal sensor, and the percentage of the Pi’s memory that’s currently in use.
You may notice that while this system status script is running, your terminal remains in use. The Python script to drive the PiFace, as called using the above command, always runs in the foreground, meaning it ties up your terminal. To quit the script, now you’ve proven the PiFace is working as expected, press Ctrl-C. Later, you’ll learn a trick for running scripts like this in the background, so you don’t lose your terminal and can multitask.
Configuring the radio script
Handily, the creators of the PiFace Control & Display have included an example Python script designed for streaming internet radio, which works out of the box and can be configured to stream almost any standards-compliant station. This script is compressed, along with some other example scripts, and must be uncompressed before use. Type the following commands at the terminal:
cd /usr/share/doc/python3-pifacecad/examples ls↵
The first command takes you to the directory where the PiFace example scripts are kept. The second lists the contents of that directory, showing you all the scripts provided as standard with the PiFace. As well as the system status script you’ve already used and the radio script you’re looking for, there will be a game of hangman, a tool for looking up train times, one for displaying Twitter messages, and a script for generating weather forecasts. They can all be extracted and used for experimentation by following the same instructions as for the radio script.
The majority of scripts are compressed using a tool called gzip to save space. Before they can be easily used, the scripts must be uncompressed back into their plain-text formats. To uncompress just the radio script, type the following command at the terminal:
sudo gunzip radio.py.gz↵
Alternatively, if you’d prefer to extract all the compressed scripts at the same time, type:
sudo gunzip *gz↵
If you type ‘ls’ again now, you’ll see that the ‘.gz’ extension has disappeared from all the files in the directory. This shows that they’re uncompressed and ready for use.
It’s possible to run the radio script right now and start streaming, but it’s worth spending some time customising it to your tastes first. Run the following command to edit the file in Nano:
sudo nano radio.py↵
The Python code for streaming internet radio stations isn’t well documented, but is thankfully very simple in its layout. Part-way down the file, there’s a list of radio stations that the script can access. These are specified in the following format:
{'name': "Friendly Station Name",↵
'source': 'URL of stream playlist or file',↵
'info': 'URL of information stream' or None},↵
Note that each station is enclosed in braces, and has spaces at the beginning. Python’s syntax relies heavily on indentation to know which lines belong to which part of
the program. To add your own choice of radio station to the list, you can simply copy these lines, making sure to place them before the ‘}’ symbol that sits at the bottom of the list. Ensure you include the spaces, four on the first line and five on the remaining two, or the script won’t be able to understand your formatting.
For example, to add the station Champion Radio UK to the list, you’d write:
{'name': "Champion Radio UK",↵
'source': 'http://uk2.internet-radio.com:31216/listen.pls', ↵
'info': None},↵
You can create as many custom entries as you like, and delete or edit the existing entries until the list is more to your taste. Compatible radio stations are easily found on a web search. Try www.internet-radio.com for a wide selection. Stations are played in the order in which they are listed; try putting favourite stations closer to the top of the list, or sorting alphabetically. When creating a new entry, bear in mind that not all stations have an information URL, as with the example above.
When you’ve completed the list, and double-checked that it includes the right indentation, commas at the end and matching braces, save the file with Ctrl-O and exit Nano with Ctrl-X.
Streaming radio
To start the radio-streaming script, simply type the following command at the terminal:
python3 radio.py↵
The PiFace display will clear, and after a few seconds display the name of the first radio station in the list, then status information about the stream will appear on the terminal. At the same time, you should be able to hear the station’s stream coming out of your speakers. If you can’t hear the stream, the Pi is most likely trying to output it via the HDMI connection instead of the 3.5mm connection. Exit the radio script with Ctrl-C, then type the following command at the terminal:
amixer cset numid=3 1↵
This tells the Pi to send all audio through the analogue output. Replacing the number 1 at the end of the command with 0 returns to the default of attempting to automatically detect the appropriate output, and using 2 forces the audio to output via the HDMI connector. Restart the script with the following command:
python3 radio.py↵
The radio script makes use of the buttons on the PiFace to control playback. The joystick at the top-right of the board can be moved left and right to skip between stations. Pressing the joystick inwards while at the centre position toggles between play and pause. To exit streaming altogether, press the right-most button at the bottom of the PiFace; this quits the script.
Having to plug in a display and keyboard to start up the script makes the PiFace a little redundant, however. To have the script automatically load in the background every time the Pi boots, load the ‘rc.local’ file in Nano with the following command:
sudo nano /etc/rc.local↵
Add the following line above ‘exit 0’, then save the file with Ctrl-O and exit with Ctrl-W.
nohup python3 /usr/share/doc/python3-pifacecad/examples/radio.py &↵
This will run the script in the background every time the Pi restarts. Test it by rebooting the Pi with the following command:
sudo reboot↵