Pull to refresh

Wi-Fi internet radio from a router with station switching capability

Level of difficultyMedium
Reading time8 min
Views1.8K
Wi-Fi интернет-радиоприёмник.
ChatGPT was not used in writing this article.
The animated image uses the webp file format instead of gif.
In this article, you will find a complete description of how to make a Wi-Fi internet radio receiver from a router that can play mp3 streams from internet radio stations.
It is also possible to switch between two internet radio stations. We will use OpenWRT firmware installed on the router to create a Wi-Fi internet radio. It is possible to complete this project without using a soldering iron. All the components can be placed inside the router to create a finished device — a Wi-Fi internet radio.
To make this, you will need:

Connecting a router to the Internet

We need to connect a router that will act as a Wi-Fi internet radio receiver to the home network router using a patch cord. Connect one end of the patch cord to the router that will act as the Wi-Fi internet radio receiver, specifically to the «Internet» port. Connect the other end of the patch cord to any available LAN port on the home network router. Note that the last (rightmost) port on the router, when viewed from the side where the ports are located, is often reserved by the Internet service provider for IPTV and is not intended for internet access.

Connect a personal computer to the router that will act as the Wi-Fi internet radio using a second patch cord. Insert the plug into any available LAN port on the router.


Reflash a router

Check if there is an OpenWRT firmware version available for your router: https://openwrt.org/toh/start. You can do this by visiting the OpenWRT wiki page for your router model. For example, the TP-LINK TL-WR842N router has an OpenWRT firmware available.

The TP-LINK TL-WR842N router is listed as a supported device on the OpenWRT website.
Follow the instructions on the OpenWRT firmware page or the general instructions on the OpenWRT website to reflash your router: https://openwrt.org/docs/guide-user/installation/generic.flashing.

Setting up OpenWRT

1) To connect to a router via ssh, type the following command in the console: (Windows users can use the ssh command or putty)

ssh root@192.168.1.1
Then, update the information about the packages in the OpenWRT by typing
opkg update
If the command is executed with errors, you need to set up DNS. This can be done in the web interface of OpenWRT. To access the web interface, open a browser and go to the page:
⠀https://192.168.1.1
⠀http://192.168.1.1, for an older version of OpenWRT
Ignore the browser warning about the self-signed certificate and proceed to the login page of the web interface. Log in without a password.
To set up DNS, go to the
Network -> DHCP and DNS section of the top menu. On this page, delete the DNS forwardings entries and add the addresses of the Google and OpenDNS DNS servers, for example: 8.8.8.8, 208.67.222.222, 208.67.220.220, please look at the image:

How to configure DNS in OpenWRT.
To configure DNS settings in older versions of OpenWRT (e.g., 19.07) and in some cases, newer versions, follow these steps:
  1. Access the web interface of OpenWRT.
  2. Go to the top menu and navigate to Network -> Interfaces.
  3. Click on the «Edit» button next to the WAN interface.
  4. In the dialog box that appears, select the «Advanced Setting» tab.
  5. Uncheck the box next to «Use DNS servers advertised by peer».
  6. Enter the addresses of the Google and OpenDNS DNS servers.
You can also perform the same steps for the LAN interface if you want devices connected to the router to receive DNS server addresses via DHCP.
Please note that these steps may vary slightly depending on the specific version of OpenWRT you are using.

2) Install all the packages needed for the USB sound card to work, including madplay, which, along with wget, will play the MP3 stream of an internet radio station. Use the following command:

opkg install kmod-sound-core kmod-usb-audio kmod-input-core kmod-input-evdev madplay alsa-utils triggerhappy kmod-hid kmod-usb-hid

We will be using a simple USB sound card for $1-$2, for example, this one from Aliexpress: USB sound card.
Also, let's update wget. This is necessary for older versions of OpenWRT (for example, versions prior to 19.07) if you want to listen to audio streams transmitted over the https:
opkg upgrade wget


3) If we want our router to connect to the Internet through the Wi-Fi home network instead of using Ethernet, we need to switch the Wi-Fi module of the router to client mode. This can be done in the web interface of OpenWRT, in the top menu section:

Switch the Wi-Fi module to Client by selecting client mode from the dropdown list of the Mode section
Wi-Fi module in client mode.
To connect to a home Wi-Fi network, enter the name of the network in the ESSID field and select the encryption type and password for the network in the «Wireless security» tab.
To save the changes made to the Wi-Fi settings, click the «Save» button in the bottom right corner of the page, and after the Device Configuration dialog disappears, click the «Save & Apply» button in the bottom right corner of the page.
To connect to the home Wi-Fi network, enter the network name in the ESSID field and select the encryption type and password for the network in the «Wireless security» tab, then click the «Save» button in the bottom right corner of the page, and after the «Device Configuration» dialog disappears, click the «Save & Apply» button in the bottom right corner of the page; finally, disconnect the patch cord that connects the router that will be used as a Wi-Fi internet radio receiver to the home network router, and check the Wi-Fi connection by executing a command:
ping altavista.com

Connect USB sound card

Now, connect the USB sound card to the USB port of the router. Connect speakers to the output of the sound card. If necessary, if the speakers are active and powered by USB, connect them to the USB port to provide power to the speakers audio-frequency amplifier. You now have a Wi-Fi router with a connected USB sound card and active speakers.

A Wi-Fi router with a connected USB sound card and active speakers.
In this case, a USB hub was used to connect the USB sound card and active speakers to the single USB port of the router. Some blue electrical tape was also used.

Reboot the router

To reboot the router, enter the command

reboot
or restart the router through the web interface on the page:
⠀https://192.168.1.1/cgi-bin/luci/admin/system/reboot
⠀http://192.168.1.1/cgi-bin/luci/admin/system/reboot, for old OpenWRT version


First time audio stream playback

To connect to the router via ssh, as we have done before, we can use the following command:

wget --no-check-certificate -q -O - https://mpc1.mediacp.eu:8404/stream | madplay - -Q --no-tty-control
Here,⠀https://mpc1.mediacp.eu:8404/stream is the URL of an mp3 audio stream from an internet radio station.
To test, here are two more audio streams:
⠀http://radio.m-1.fm/raduga/high⠀http://i6.streams.ovh:16012/stream

wget connects to the internet radio station server, without checking the SSL certificate (--no-check-certificate), does not display its status in the console (-q), and sends the downloaded audio stream output to STDOUT (-O -). Then, via a pipe (|), the audio stream goes to madplay, which decodes the mp3 stream without displaying its status in the console (-Q) and without responding to button presses (--no-tty-control). The data is then sent to the USB sound card.
To stop playback, press twice
Ctrl+c
If we want to play an audio stream with special characters in the URL such as '? &! # $ ' ( ) * +, /:; = @ [ ]' (RFC 3986), then we need to enclose the URL in quotes ' '.


Automatic connection to a stream when the router is turned on

In Linux, there is a file where you can add commands that will be executed during system startup. In OpenWRT and some other distributions, this file is called rc.local and it is located in the directory /etc:

/etc/rc.local
You can also access it through the web interface of OpenWRT:
System -> Startup, Local Startup tab

Editing the /etc/rc.local file in the OpenWRT web interface.
To add a command that will be executed during the OpenWRT boot process, you need to edit the file before the «exit 0» line. Once you have added the command, save the file by clicking the «Save» button.

Reconnecting to audio stream after connection loss and switching between two stations

Switching between two stations is possible thanks to the presence of a file in OpenWRT that stores the brightness value of the LED responsible for indicating the connection of client devices to the LAN network. When we connect a computer to the router using an Ethernet cable, for example, to the LAN4 port, the fourth LED on the router panel will light up.
To utilize this feature, we can connect to the router via the console and navigate to the directory

cd /sys/devices/platform/leds/leds/router_model_name:lan/
In this directory, we can find the «brightness» file, which stores the brightness value of the LAN LED, ranging from 0 to 255.

brightness file in the :lan directory in OpenWRT.
We can determine whether this LED is on or off by using the command
cat /sys/devices/platform/leds/leds/router_model_name:lan/brightness
To view the directories in the /sys/devices/platform/leds/leds/ directory, navigate to that directory using the command
cd /sys/devices/platform/leds/leds/
Then, enter the command
ls
Station switch. To connect the wires to the four LAN4 pins (two twisted pairs), we will connect these four wires to a switch (station switch in the «Wi-Fi Internet Radio Receiver in open view» image). The switch will toggle the TX (Transmit) pair with the RX (Receive) pair of the Wi-Fi router. The Wi-Fi router's LAN port will remain functional and can be used to connect external devices if the switch is open.
We can now control the state of the LED and the value stored in the brightness file in OpenWRT.

Let's write a bash script that we will place in the file /etc/rc.local, which will handle connecting to the audio stream, playback, reconnection in case of connection loss, and switching between two internet radio stations.


# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.

echo "#!/bin/sh" > /tmp/checkOnline
echo "if ( ! ping -c 8 8.8.8.8 > /dev/null 2>&1 ) ; then echo 'network down'; sh /tmp/killPlayer; else echo 'network up'; fi;" >> /tmp/checkOnline
echo "if ( ! pgrep -x madplay > /dev/null 2>&1 ) ; then echo 'player down'; sh /tmp/killPlayer; else echo 'player up'; fi" >> /tmp/checkOnline
chmod +x /tmp/checkOnline

echo "#!/bin/sh" > /tmp/killPlayer
echo "/usr/bin/killall -9 wget madplay;
sleep 1;
echo 'start playRadio';
sh /tmp/playRadio &" >> /tmp/killPlayer
chmod +x /tmp/killPlayer

echo "#!/bin/sh" > /tmp/switchRadioTimer
echo "while true
do
 STATE=\`cat /sys/devices/platform/leds/leds/tl-wr842n-v5:green:lan/brightness\`
 OLDSTATE=\`cat /etc/oldStationSwitchState\`
 if [ "\$STATE" != "\$OLDSTATE" ] ; then echo \$STATE > /etc/oldStationSwitchState; sh /tmp/killPlayer; fi;
 sleep 1
done" >> /tmp/switchRadioTimer
chmod +x /tmp/switchRadioTimer

echo "#!/bin/sh" > /tmp/playRadio
echo "OLDSTATE=\`cat /etc/oldStationSwitchState\`
URL='http://radio.m-1.fm/raduga/high'
if [[ \$OLDSTATE -gt 0 ]] ; then URL='http://i6.streams.ovh:16012/stream'; fi;
wget --no-check-certificate -q -O - \$URL | madplay - -Q --no-tty-control" >> /tmp/playRadio
chmod +x /tmp/playRadio

echo "#!/bin/sh" > /tmp/checkOnlineTimer
echo "while true
do
 sh /tmp/checkOnline
 sleep 20
done" >> /tmp/checkOnlineTimer
chmod +x /tmp/checkOnlineTimer

sleep 5; /tmp/playRadio &
sleep 5; /tmp/switchRadioTimer &
sleep 5; /tmp/checkOnlineTimer &

exit 0

In the directory /tmp, we create executable script files: checkOnline, killPlayer, switchRadioTimer, playRadio, and checkOnlineTimer.


checkOnline is a script that checks whether the Google DNS server is pingable and whether madplay is running. If at least one of these conditions is not met, killPlayer is executed.


killPlayer terminates the execution of madplay and runs the playRadio script.


playRadio reads the station number to be played from the text file /etc/oldStationSwitchState and plays either stream #1 or stream #2 depending on the value.


switchRadioTimer is a script that, in an infinite loop, checks the state of the LAN4 LED, which lights up when our station switch is closed. Then, the script compares the read switch state with the value stored in the /etc/oldStationSwitchState file. If they are different, the switch state value is saved in the /etc/oldStationSwitchState file, and killPlayer is called.


checkOnlineTimer checks whether madplay is running every 20 seconds using the checkOnline script.


All script files have been created. We will now run playRadio, switchRadioTimer, and checkOnlineTimer.


After saving /etc/rc.local, we need to create a file /etc/oldStationSwitchState with a zero value. This can be done in the OpenWRT console using the command

echo 0 > /etc/oldStationSwitchState

Wi-Fi internet radio receiver inside and outside

We will place the USB sound card, audio-frequency amplifier, volume controller, and switches inside the Wi-Fi router case. Then, we will mount the 3.5mm audio jack on the back panel:

Wi-Fi Internet Radio Receiver in open view.Wi-Fi Internet Radio Receiver in open view.

Here is a Wi-Fi internet radio receiver in its assembled form:

Assembled Wi-Fi internet radio.

Let's test how a Wi-Fi internet radio receiver works:

Please watch the video of the Wi-Fi internet radio receiver (mp4 file). (I am using an old Habr editor and cannot embed a video file. The oembed tag does not support simple video file insertion.)
This article is on my website: Wi-Fi internet radio from a router with station switching capability. (With the permission of the administrators of Habrahabr.)
Tags:
Hubs:
Total votes 4: ↑4 and ↓0+4
Comments1

Articles