A Raspberry Pi Zero W or a Raspberry Pi 4, with a USB audio card and some amplified speakers, can become an internet radio (without any soldering).
I made this project to provide an internet radio to an 89-year-old person with bad FM reception in her apartment. My goals for the internet radio were:
- Make it as simple as possible. Turn up the volume to hear the music. Turn down the volume to turn off the music.
- Make it foolproof. If it loses the Wi-Fi signal, it continuously tries to reconnect. If it is unplugged accidentally, it starts when plugged in again.
- Make it quick to assemble. The speakers are connected to the 3.5 mm jack or a USB port.
Note. If you want more a more complicated project, with more than one station, then try this instructable or this instructable or many other instructables. Alternatively, you can make the Raspberry Pi into a dedicated music player with Volumio or Moode. But I needed simple and quick.
The project requires about two hours, after you have the parts.
- Gather the parts
- Find the URLs
- Set up the Raspberry Pi 4
- Change the display settings
- Add Omxplayer
- Add an autostart command
- Check the card number of the USB audio card
- Save and edit the Python script
- Start the radio
The total cost can be about $50, even if you need to buy all the parts.
- For version 1, a Raspberry Pi 4, any GB size, in a kit with a microSD card, Micro HDMI to HDMI cable, power supply, and case, such as this kit or this kit for about $65.
- For version 2, a Raspberry Pi Zero W in a kit with a microSD card, USB OTG cable, Mini HDMI to HDMI adapter, power supply, and case, such as this kit or this kit for about $35.
Note. Any Raspberry Pi model with Wi-Fi can work for this project, but I only tried the Zero W and the 4. - USB audio card, about $7 for low end, up to $200 for high end.
Note. The USB audio card isn't strictly necessary with version 1, because a Raspberry Pi 4 has a 3.5 mm audio jack, but the sound is better from a USB audio card. - External speakers with a 3.5 mm audio plug and a power supply, for about $5 for used, up to $200 for high end.
Note. If you see external speakers in a thirft store, be sure they have a power supply that fits. - Keyboard, mouse, monitor, HDMI cable, and a laptop or other computer.
Note. The keyboard and other items are only for setup, not for operation.
Find the URLs of an internet radio station. Try to find more than one URL, in case one is over committed and won't accept another connection. Save the URLs to use when you edit the Python script.
Note. Finding the URLs doesn't need to be the first task. You can set up the Raspberry Pi 4 first, then use the Chromium browser on the Raspberry Pi to find the URLs. But if there is one specific station you want to hear, you might want to find its URLs before you go further.
The URLs in the Python script—before you customize it—are streams at SomaFM.
- Open https://somafm.com/.
- Click a channel, such as ThistleRadio.
- Click Direct Stream Links.
- Copy the URL of a main PLS, such as http://somafm.com/thistle.pls.
- Paste the URL in the browser bar and press enter.
The file downloads. - Open the downloads folder.
- Rename the PLS file with the extension .txt, such as thistle.pls.txt.
- Open the file with a text editor.
- Delete everything in the file except the URLs (and save the file).
For example, thistle.pls.txt looked like this:
[playlist]
numberofentries=4
File1=http://ice2.somafm.com/thistle-128-mp3
Title1=SomaFM: ThistleRadio (#1): Exploring music from Celtic roots and branches
Length1=-1
File2=http://ice6.somafm.com/thistle-128-mp3
Title2=SomaFM: ThistleRadio (#2): Exploring music from Celtic roots and branches
Length2=-1
File3=http://ice4.somafm.com/thistle-128-mp3
Title3=SomaFM: ThistleRadio (#3): Exploring music from Celtic roots and branches
Length3=-1
File4=http://ice1.somafm.com/thistle-128-mp3
Title4=SomaFM: ThistleRadio (#4): Exploring music from Celtic roots and branches
Length4=-1
Version=2
I changed thistle.pls.txt to this:
http://ice2.somafm.com/thistle-128-mp3
http://ice6.somafm.com/thistle-128-mp3
http://ice4.somafm.com/thistle-128-mp3
http://ice1.somafm.com/thistle-128-mp3
To give another example, I found URLs for a classical music stream.
- In Chrome or Chromium, open a radio station's site and open its online player.
For example, I opened capradio.org and clicked ClassicalStream. - In the brower's address bar, click . . . > More tools > Developer tools.
- In the tools window, click the Network tab.
- Repeat the following steps until you find three or more URLs.
- In the browser's menu bar, click Reload this page (↻).
- In the tools pane, click Clear (🚫).
- In the site pane, click Play (▶).
- In the tools pane, right-click the line where the file size is increasing.
- Select Copy > Copy link address.
- Open a text editor and paste the link.
- Delete extra parameters after the URL (and save the file).
For example, the link I copied was this:
https://14523.live.streamtheworld.com/CLASSICALSTREAMAAC.aac?dist=triton-web-sdk-CapRadio&tdsdk=js-2.9&pname=TDSdk&pversion=2.9&banners=300x250&sbmid=6e658814-a66a-4827-e6a4-f950ee4e38e2
I changed it to this:
https://14523.live.streamtheworld.com/CLASSICALSTREAMAAC.aac
I copied the link several times and found three URLs:
https://14523.live.streamtheworld.com/CLASSICALSTREAMAAC.aac
https://14923.live.streamtheworld.com/CLASSICALSTREAMAAC.aac
https://18543.live.streamtheworld.com/CLASSICALSTREAMAAC.aac
The first version I made was the simplest and quickest. It was only speakers attached to a Raspberry Pi 4. (The second version was a Raspberry Pi Zero W with a USB audio card and spearkers.)
Install Raspberry PI OS and connect to Wi-Fi.
- Place the Raspberry Pi in its case, according to the case instructions.
- Insert a microSD card that has a copy of either NOOBs or Rapsberry Pi OS.
If the microSD card is blank, first insert it in another computer and install Raspberry Pi OS on it with Raspberry Pi Imager. - Connect a monitor, keyboard, and mouse.
Connect the USB audio card, if using. - Connect a power supply.
The Raspberry Pi boots. - If the microSD card has NOOBs, select Rapsberry Pi OS and install it.
- When prompted, select settings, including country, password, and Wi-Fi.
- When prompted, update the system.
- When prompted, restart.
Specify the monitor resolution in the settings.
- Open Terminal.
- Type
xdpyinfo | grep dimensions
and press Enter.
The resolution of your monitor is displayed. (I found the command on this page.)
For example, the resolution of my monitor was 1920x1200 pixels. - Type
sudo raspi-config
and press Enter. - Select Display Options.
- Select Resolution
- Select the resolution of your monitor.
For example, I selected DMT Mode 59 1920x1200 60 Hz 16:10.
CEA is for TVs and DMT is for monitors. - Select OK, select Finish, and select No (not to reboot).
Note. There are two reasons to select a specific resolution, to keep the card number of your USB audio card from changing after the monitor is disconnected and to enable the Raspberry Pi to boot without a monitor.
Install OMXPlayer and a Python library for it.
In Terminal, type sudo apt install omxplayer && pip3 install omxplayer-wrapper && sudo apt clean
and press Enter.
Note. I tried VLC media player, which is installed with the Raspberry Pi OS, but VLC stayed open even when the Wi-Fi signal was lost. By constrast, OMXPlayer closed when the Wi-Fi signal was lost, which let the Python script detect a problem and try to reconnect.
Edit the autostart file.
- In Terminal, type
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
and press Enter. - Move down to the line beginning
@xscreensaver
. - Type
@python3 /home/pi/stream.py &
and press Enter.
You can use a different name for the Python script, such as thistle.py or classical.py instead of stream.py. - Press Ctrl+X and Y and Enter, which saves the file.
Note. A Python script can be made to autostart in many ways, supposedly. This way that worked for me, which I found in this project, 1970 Flirt Pi Internet Radio.
If using a Raspberry Pi 4, find the card number of the USB audio card.
In Terminal, type aplay -l
and press Enter.
The card number of the USB audio card is displayed, with other information.
On my Raspberry Pi 4, the card number of the USB audio card was 2. So, 2 was the number I put in the Python script in the next step. (If not using a USB audio card, use the card number of the 3.5 mm audio jack.)
Note. The output parameter came from this gist, Playback on USB audio device . . . with Omxplayer.
The Python script is ready to download and play ThistleRadio on a Raspberry Pi Zero W. Change it to play another stream.
- Open Text Editor or Thonny.
- Save the file as /home/pi/stream.py.
Use the file name in the autostart command, if you used a file name other than stream.py in the autostart command. - Copy and paste the code for stream.py.
- If using a Raspberry Pi 4, change the 1 in the output parameter to the card number you found.
For example, if the card number as a 2, change the code tooutput = 'alsa:hw:2,0'
.
(If using a Raspberry Pi Zero W, leave the 1 in the output parameter,output = 'alsa:hw:1,0'
.) - Change the URLs to the URLs you found.
- If you found fewer than four URLs, then delete the unused lines of code.
For example, the classical stream has three URLs and requires these changes:
• Change url1, url2, and url3 to the classical stream.
• Delete the line beginningurl4 =
.
• Delete eight lines fromTries url4
toexcept:
.
See stream-classical.py. - Save the file.
To test a Raspberry Pi 4, run the Python script in Terminal.
- Set up the external speakers.
- Connect the external speakers to the USB audio card.
- Connect the external speakers to a power supply.
- Turn on the external speakers and adjust the volume to about 50 percent.
- In Terminal, type
python3 stream.py
and press Enter.
Use the file name in the autostart command, if you used a file name other than stream.py.
The music starts after a two minute delay. - To stop the music stream, press Ctrl+C twice in Terminal.
If Terminal is closed, open Terminal, typekillall omxplayer.bin && killall python3
and press Enter.
- Shut down the Raspberry Pi.
• On the desktop, click the raspberry menu and select Shutdown.
• In Terminal, typesudo shutdown -h now
and press Enter. - Disconnect the power supply.
Disconect the monitor, keyboard, and mouse. - Connect the USB audio card and external speakers. \
- Connect the USB audio card to the Raspberry Pi.
- Connect the external speakers to the USB audio card.
- Connect the external speakers to a power supply.
- Connect the Raspberry Pi to a power supply.
The Raspberry Pi boots.
After a two minute delay, the music stream plays.
Note. The two minute delay is in the code to wait for the Raspberry Pi to finish booting.
If you move the Raspberry Pi to a new location later, remember to bring a keyboard, mouse, and cable for a monitor or TV, so that you can connect to Wi-Fi at the new location. Alternatively, bring a laptop and change settings on the Raspberry Pi using SSH through USB. After connecting to Wi-Fi in the new location, start the radio.
Install Raspberry PI OS and connect to Wi-Fi.
- Place the Raspberry Pi in its case, according to the case instructions.
• A Raspberry Pi Zero W probably fits in its case after the microSD card is inserted.
• A Raspberry Pi 4 probably fits in its case before the microSD card is inserted. - Insert a microSD card with either NOOBs or Rapsberry Pi OS.
If you have a blank microSD card, install Raspberry Pi OS on it with Raspberry Pi Imager. - Connect a monitor, keyboard, and mouse.
If using a Raspberry Pi 4, connect the USB audio card too. - Connect a power supply.
The Raspberry Pi boots. - If the microSD card has NOOBs, select Rapsberry Pi OS and install it.
- When prompted, select settings, including country, password, and Wi-Fi.
- When prompted, update the system.
- When prompted, restart.
Note. Raspberry Pi OS Lite is probably a better option than a full installation of Raspberry Pi OS with desktop and recommended software. However, I wanted this project to quick and simple, so I used the operating system I knew best.
Access through SSH isn't required for this project, but might be convenient after the monitor and keyboard are disconnected.
- In Terminal, type
sudo raspi-config
and press Enter. - Select Interface Options.
- Select SSH.
- Select Yes and OK and Finish.
- Type
hostname -I
and press Enter.
The Raspberry Pi's IP number is displayed. - Write down the IP number to use later.
Specify the monitor resolution in the settings.
- Open Terminal
- Type
xdpyinfo | grep dimensions
and press Enter.
The resolution of your monitor is displayed. (I found the command on this page.)
For example, the resolution of my monitor was 1920x1200 pixels. - Type
sudo raspi-config
and press Enter. - Select Display Options.
- Select Resolution
- Select the resolution of your monitor.
For example, I selected DMT Mode 59 1920x1200 60 Hz 16:10.
CEA is for TVs and DMT is for monitors. - Select OK, select Finish, and select No (not to reboot).
Note. There are two reasons to select a specific resolution, to keep the card number of your USB audio card from changing after the monitor is disconnected and to enable the Raspberry Pi to boot without a monitor.
Install OMXPlayer and a Python library for it.
In Terminal, type sudo apt install omxplayer && pip3 install omxplayer-wrapper && sudo apt clean
and press Enter.
Note. I tried VLC media player, which is installed with the Raspberry Pi OS, but VLC stayed open even when the Wi-Fi signal was lost. By constrast, OMXPlayer closed when the Wi-Fi signal was lost, which let the Python script detect a problem and try to reconnect.
Edit the autostart file.
- In Terminal, type
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
and press Enter. - Move down to the line beginning
@xscreensaver
. - Type
@python3 /home/pi/stream.py &
and press Enter.
You can use a different name for the Python script, such as thistle.py or classical.py instead of stream.py. - Press Ctrl+X and Y and Enter, which saves the file.
Note. A Python script can be made to autostart in many ways, supposedly. This way that worked for me, which I found in this project, 1970 Flirt Pi Internet Radio.
If using a Raspberry Pi 4, find the card number of the USB audio card.
In Terminal, type aplay -l
and press Enter.
The card number of the USB audio card is displayed, with other information.
On my Raspberry Pi 4, the card number of the USB audio card was 2. So, 2 was the number I put in the Python script in the next step. (If not using a USB audio card, use the card number of the 3.5 mm audio jack.)
Note. The output parameter came from this gist, Playback on USB audio device . . . with Omxplayer.
The Python script is ready to download and play ThistleRadio on a Raspberry Pi Zero W. Change it to play another stream.
- Open Text Editor or Thonny.
- Save the file as /home/pi/stream.py.
Use the file name in the autostart command, if you used a file name other than stream.py in the autostart command. - Copy and paste the code for stream.py.
- If using a Raspberry Pi 4, change the 1 in the output parameter to the card number you found.
For example, if the card number as a 2, change the code tooutput = 'alsa:hw:2,0'
.
(If using a Raspberry Pi Zero W, leave the 1 in the output parameter,output = 'alsa:hw:1,0'
.) - Change the URLs to the URLs you found.
- If you found fewer than four URLs, then delete the unused lines of code.
For example, the classical stream has three URLs and requires these changes:
• Change url1, url2, and url3 to the classical stream.
• Delete the line beginningurl4 =
.
• Delete eight lines fromTries url4
toexcept:
.
See stream-classical.py. - Save the file.
To test a Raspberry Pi 4, run the Python script in Terminal.
- Set up the external speakers.
- Connect the external speakers to the USB audio card.
- Connect the external speakers to a power supply.
- Turn on the external speakers and adjust the volume to about 50 percent.
- In Terminal, type
python3 stream.py
and press Enter.
Use the file name in the autostart command, if you used a file name other than stream.py.
The music starts after a two minute delay. - To stop the music stream, press Ctrl+C twice in Terminal.
If Terminal is closed, open Terminal, typekillall omxplayer.bin && killall python3
and press Enter.
- Shut down the Raspberry Pi.
• On the desktop, click the raspberry menu and select Shutdown.
• In Terminal, typesudo shutdown -h now
and press Enter. - Disconnect the power supply.
Disconect the monitor, keyboard, and mouse. - Connect the USB audio card and external speakers. \
- Connect the USB audio card to the Raspberry Pi.
- Connect the external speakers to the USB audio card.
- Connect the external speakers to a power supply.
- Connect the Raspberry Pi to a power supply.
The Raspberry Pi boots.
After a two minute delay, the music stream plays.
Note. The two minute delay is in the code to wait for the Raspberry Pi to finish booting.
If you move the Raspberry Pi to a new location later, remember to bring a keyboard, mouse, and cable for a monitor or TV, so that you can connect to Wi-Fi at the new location. Alternatively, bring a laptop and change settings on the Raspberry Pi using SSH through USB. After connecting to Wi-Fi in the new location, start the radio.
If you want the internet radio to play a different stream occassionally, copy and paste the code for stream-override.py. That code gives some remote control over what the internet radio plays.
- Save and edit the Python script, as above, but copy and paste the code for stream-override.py.
- Save a lookup file online, such as in a GitHub repository.
An example lookup file is https://github.com/italicize/quick-internet-radio/blob/main/override-url.txt. - Add the URL of the lookup file to the code.
The example code sayslookup = 'https://raw.githubusercontent.com/italicize/quick-internet-radio/main/override-url.txt'
.
The URL in the code opens the raw file. - Test or start the radio.
- To play a special program, edit the lookup file, paste in a URL, and save.
For example, to play an episode of an old-time radio show, I saved this URL in my lookup file. \https://archive.org/details/OTRR_Lux_Radio_Theater_Singles/Lux_Radio_Theatre_46-02-18_515_Captain_January.mp3
. - To end a special program, edit the lookup file, delete the URL, and save.
For example, an hour later, after the show is finished, I saved my lookup file as a blank file again.
The Python script returnd to playing the URLs in the code, when the lookup file is blank.
To test a Raspberry Pi Zero W, run the Python script through SSH. However, it's easier to start the radio and see whether it plays.
- Shut down the Raspberry Pi.
• On the desktop, click the raspberry menu and select Shutdown.
• In Terminal, typesudo shutdown -h now
and press Enter. - Disconnect the power supply and everything else connected to the Raspberry Pi.
- Set up the USB audio card and the external speakers. \
- Connect the USB audio card to the Raspberry Pi.
- Connect the external speakers to the USB audio card.
- Connect the external speakers to a power supply.
- Connect the Raspberry Pi to a power supply.
The Raspberry Pi boots. - Open an SSH client.
• On a phone, open Terminus or a similar app.
• On a Windows laptop, in the Windows taskbar, type Command Prompt and press Enter.
(To confirm that OpenSSH Client is installed, Start > Settings > Apps > Apps & features > Optional features > OpenSSH Client.)
• On a Mac or Linux system, open the Terminal.
• For information about using SSH, see SSH (Secure Shell). - At the prompt, type
ssh pi@IPnumber
, using the IP number you wrote down earlier.
Typeyes
and press Enter, if asked to approve the connection. - Type the Raspberry Pi's password and press Enter.
The Raspberry Pi's prompt appears,pi@raspberrypi: ~$
. - Type
python3 stream.py
and press Enter.
Use the file name in the autostart command, if you used a file name other than stream.py.
The music starts after a two minute delay. - To stop the music stream, press Ctrl+C repeatedly until the Python script stops and returns you to the prompt.
Copyright (C) 2021 Jay Martin.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License."
Raspberry Pi is a trademark of the Raspberry Pi Foundation. All other trademarks are the property of their respective owners.