GitXplorerGitXplorer
i

quick-internet-radio

public
1 stars
0 forks
0 issues

Commits

List of commits on branch main.
Verified
f767222611921db3fdd29420453e254a5217ced1

Save a blank file.

iitalicize committed 3 years ago
Verified
20c5b999b11e299f19bbe14c20ac0505e9d0fe26

Save a override URL.

iitalicize committed 3 years ago
Verified
4378a94a9127aa9ffa3477f1ae705ed284f557cd

Update README.md

iitalicize committed 3 years ago
Verified
3e9af21140caed7746c29351967495f22de0e18a

Start to split into versions 1 and 2

iitalicize committed 3 years ago
Verified
2d8ce7770aa3b64f5eb44fc421fb33dd082de98c

Saved a blank file.

iitalicize committed 3 years ago
Verified
bcc5b3808e5657b6273a808b88ec3d9dd106ca36

Saved an override URL.

iitalicize committed 3 years ago

README

The README file for this repository.

Quick One-Station Internet Radio

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.

  1. Gather the parts
  2. Find the URLs
  3. Set up the Raspberry Pi 4
  4. Change the display settings
  5. Add Omxplayer
  6. Add an autostart command
  7. Check the card number of the USB audio card
  8. Save and edit the Python script
  9. Start the radio

Gather the parts

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

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.

(Example) URLs for SomaFM channels are easy to find

The URLs in the Python script—before you customize it—are streams at SomaFM.

  1. Open https://somafm.com/.
  2. Click a channel, such as ThistleRadio.
  3. Click Direct Stream Links.
  4. Copy the URL of a main PLS, such as http://somafm.com/thistle.pls.
  5. Paste the URL in the browser bar and press enter.
    The file downloads.
  6. Open the downloads folder.
  7. Rename the PLS file with the extension .txt, such as thistle.pls.txt.
  8. Open the file with a text editor.
  9. 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

(Example) URLs for other stations might be harder to find

To give another example, I found URLs for a classical music stream.

  1. In Chrome or Chromium, open a radio station's site and open its online player.
    For example, I opened capradio.org and clicked ClassicalStream.
  2. In the brower's address bar, click . . . > More tools > Developer tools.
  3. In the tools window, click the Network tab.
  4. Repeat the following steps until you find three or more URLs.
    1. In the browser's menu bar, click Reload this page (↻).
    2. In the tools pane, click Clear (🚫).
    3. In the site pane, click Play (▶).
    4. In the tools pane, right-click the line where the file size is increasing.
    5. Select Copy > Copy link address.
    6. Open a text editor and paste the link.
    7. 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

Version 1, a Rapsberry Pi 4 with speakers

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.)

Set up the Raspberry Pi 4

Install Raspberry PI OS and connect to Wi-Fi.

  1. Place the Raspberry Pi in its case, according to the case instructions.
  2. 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.
  3. Connect a monitor, keyboard, and mouse.
    Connect the USB audio card, if using.
  4. Connect a power supply.
    The Raspberry Pi boots.
  5. If the microSD card has NOOBs, select Rapsberry Pi OS and install it.
  6. When prompted, select settings, including country, password, and Wi-Fi.
  7. When prompted, update the system.
  8. When prompted, restart.

Change the display settings

Specify the monitor resolution in the settings.

  1. Open Terminal.
  2. 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.
  3. Type sudo raspi-config and press Enter.
  4. Select Display Options.
  5. Select Resolution
  6. 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.
  7. 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.

Add OMXPlayer

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.

Add an autostart command

Edit the autostart file.

  1. In Terminal, type sudo nano /etc/xdg/lxsession/LXDE-pi/autostart and press Enter.
  2. Move down to the line beginning @xscreensaver.
  3. 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.
  4. 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.

Check the card number of the USB audio card

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.

Save and edit the Python script

The Python script is ready to download and play ThistleRadio on a Raspberry Pi Zero W. Change it to play another stream.

  1. Open Text Editor or Thonny.
  2. 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.
  3. Copy and paste the code for stream.py.
  4. 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 to output = 'alsa:hw:2,0'.
    (If using a Raspberry Pi Zero W, leave the 1 in the output parameter, output = 'alsa:hw:1,0'.)
  5. Change the URLs to the URLs you found.
  6. 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 beginning url4 =.
    • Delete eight lines from Tries url4 to except:.
    See stream-classical.py.
  7. Save the file.

(Optional) Test the Python script on a Raspberry Pi 4

To test a Raspberry Pi 4, run the Python script in Terminal.

  1. Set up the external speakers.
    1. Connect the external speakers to the USB audio card.
    2. Connect the external speakers to a power supply.
    3. Turn on the external speakers and adjust the volume to about 50 percent.
  2. 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.
  3. To stop the music stream, press Ctrl+C twice in Terminal.
    If Terminal is closed, open Terminal, type killall omxplayer.bin && killall python3 and press Enter.

Start the radio

  1. Shut down the Raspberry Pi.
    • On the desktop, click the raspberry menu and select Shutdown.
    • In Terminal, type sudo shutdown -h now and press Enter.
  2. Disconnect the power supply.
    Disconect the monitor, keyboard, and mouse.
  3. Connect the USB audio card and external speakers. \
    1. Connect the USB audio card to the Raspberry Pi.
    2. Connect the external speakers to the USB audio card.
    3. Connect the external speakers to a power supply.
  4. 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.

Move the radio to a new location

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.

Version 2, a Raspberry Pi Zero W with a USB audio card and speakers

Set up the Raspberry Pi Zero W

Install Raspberry PI OS and connect to Wi-Fi.

  1. 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.
  2. 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.
  3. Connect a monitor, keyboard, and mouse.
    If using a Raspberry Pi 4, connect the USB audio card too.
  4. Connect a power supply.
    The Raspberry Pi boots.
  5. If the microSD card has NOOBs, select Rapsberry Pi OS and install it.
  6. When prompted, select settings, including country, password, and Wi-Fi.
  7. When prompted, update the system.
  8. 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.

(Optional) Enable SSH

Access through SSH isn't required for this project, but might be convenient after the monitor and keyboard are disconnected.

  1. In Terminal, type sudo raspi-config and press Enter.
  2. Select Interface Options.
  3. Select SSH.
  4. Select Yes and OK and Finish.
  5. Type hostname -I and press Enter.
    The Raspberry Pi's IP number is displayed.
  6. Write down the IP number to use later.

Change the display settings

Specify the monitor resolution in the settings.

  1. Open Terminal
  2. 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.
  3. Type sudo raspi-config and press Enter.
  4. Select Display Options.
  5. Select Resolution
  6. 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.
  7. 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.

Add OMXPlayer

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.

Add an autostart command

Edit the autostart file.

  1. In Terminal, type sudo nano /etc/xdg/lxsession/LXDE-pi/autostart and press Enter.
  2. Move down to the line beginning @xscreensaver.
  3. 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.
  4. 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.

Check the card number of the USB audio card

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.

Save and edit the Python script

The Python script is ready to download and play ThistleRadio on a Raspberry Pi Zero W. Change it to play another stream.

  1. Open Text Editor or Thonny.
  2. 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.
  3. Copy and paste the code for stream.py.
  4. 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 to output = 'alsa:hw:2,0'.
    (If using a Raspberry Pi Zero W, leave the 1 in the output parameter, output = 'alsa:hw:1,0'.)
  5. Change the URLs to the URLs you found.
  6. 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 beginning url4 =.
    • Delete eight lines from Tries url4 to except:.
    See stream-classical.py.
  7. Save the file.

(Optional) Test the Python script on a Raspberry Pi 4

To test a Raspberry Pi 4, run the Python script in Terminal.

  1. Set up the external speakers.
    1. Connect the external speakers to the USB audio card.
    2. Connect the external speakers to a power supply.
    3. Turn on the external speakers and adjust the volume to about 50 percent.
  2. 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.
  3. To stop the music stream, press Ctrl+C twice in Terminal.
    If Terminal is closed, open Terminal, type killall omxplayer.bin && killall python3 and press Enter.

Start the radio

  1. Shut down the Raspberry Pi.
    • On the desktop, click the raspberry menu and select Shutdown.
    • In Terminal, type sudo shutdown -h now and press Enter.
  2. Disconnect the power supply.
    Disconect the monitor, keyboard, and mouse.
  3. Connect the USB audio card and external speakers. \
    1. Connect the USB audio card to the Raspberry Pi.
    2. Connect the external speakers to the USB audio card.
    3. Connect the external speakers to a power supply.
  4. 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.

Move the radio to a new location

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.

(Optional) Add an override URL to the Python script

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.

  1. Save and edit the Python script, as above, but copy and paste the code for stream-override.py.
  2. 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.
  3. Add the URL of the lookup file to the code.
    The example code says lookup = 'https://raw.githubusercontent.com/italicize/quick-internet-radio/main/override-url.txt'.
    The URL in the code opens the raw file.
  4. Test or start the radio.
  5. 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.
  6. 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.

(Optional) Test the Python script on a Raspberry Pi Zero W

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.

  1. Shut down the Raspberry Pi.
    • On the desktop, click the raspberry menu and select Shutdown.
    • In Terminal, type sudo shutdown -h now and press Enter.
  2. Disconnect the power supply and everything else connected to the Raspberry Pi.
  3. Set up the USB audio card and the external speakers. \
    1. Connect the USB audio card to the Raspberry Pi.
    2. Connect the external speakers to the USB audio card.
    3. Connect the external speakers to a power supply.
  4. Connect the Raspberry Pi to a power supply.
    The Raspberry Pi boots.
  5. 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).
  6. At the prompt, type ssh pi@IPnumber, using the IP number you wrote down earlier.
    Type yes and press Enter, if asked to approve the connection.
  7. Type the Raspberry Pi's password and press Enter.
    The Raspberry Pi's prompt appears, pi@raspberrypi: ~$.
  8. 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.
  9. To stop the music stream, press Ctrl+C repeatedly until the Python script stops and returns you to the prompt.

Legal

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.