Building a web radio with a Raspberry-PI

A mix of past and modern technology, this project was born out of the purchase, for a handful of dollars at a yard sale, of a superb cabinet (completely empty) of a 1933 Emerson radio set. So, after some small restoration works, we are now ready for the installation of an ultra-modern Raspberry-pi !
Dev's Corner
Spiria's Dev Team
2018-10-27 15:45
5 minute read
<div><div><p>A mix of past and modern technology, this project was born out of the purchase, for a handful of dollars at a yard sale, of a superb cabinet (completely empty) of a 1933 Emerson radio set.</p><p>So, after some small restoration works, we are now ready for the installation of an ultra-modern Raspberry-pi !</p><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/20151011_200847.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/20151011_200847.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/20151011_200847.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/2672/20151011_200847.jpg" alt="decorative"></source></source></source></picture></p><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/20151011_200855.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/20151011_200855.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/20151011_200855.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/2672/20151011_200855.jpg" alt="decorative"></source></source></source></picture></p><h2>1. The shopping list</h2><ul> <li><a href="https://www.raspberrypi.org/products/model-a-plus/">A rapsberry-PI model A+</a></li> <li>One SD card</li> <li>A WiFi USB dongle</li> <li>A 12-position circular switch</li> <li>A 10K potentiometer equipped with a switch</li> <li>A 5V 500mA power supply</li> <li>One KIT Low frequency amplifier</li></ul><h2>2. The schema</h2><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/radio_schamatic.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/radio_schamatic.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/radio_schamatic.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/2672/radio_schamatic.webp" alt="decorative"></source></source></source></picture></p><p>The audio output of the Raspberry-Pi is connected to the input of a small amplifier tolerating a power supply of 3 to 16V and the network is carried by a WiFi dongle.</p><p>It should be noted that :</p><ul> <li>The amplifier is not connected directly to the 5V (otherwise you will hear noise from the CPU). A small regulator (LM7805 adjusted to 4V) and a 250uF capacitor at the output will filter the line cleanly.</li> <li>The potentiometer (P2) allowing to adjust the gain, is also equipped with a switch that will allow us to mechanically cut the power supply when we turn the knob to its minimum.</li> <li>"Ready Indicator" is an LED simply plugged on a GPIO which will be used to indicate if the WiFI network is available.</li> <li>The rotary switch (P1) is connected to 12 GPIOs which have the particularity of being configured by default as PULL-DOWN on the Raspberry-PI.</li></ul><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/untitled.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/untitled.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/untitled.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/2672/untitled.webp" alt="decorative"></source></source></source></picture></p><h2>3. Montage</h2><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/20151011_200635.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/20151011_200635.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/20151011_200635.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/2672/20151011_200635.jpg" alt="decorative"></source></source></source></picture></p><p><picture><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/20151011_200832.400x0.webp" media="(max-width: 599px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/20151011_200832.760x0.webp" media="(max-width: 999px)"><source type="image/webp" srcset="https://mirror.spiria.com/site/assets/files/2672/20151011_200832.1039x0.webp" media="(min-width: 1000px)"><img src="https://mirror.spiria.com/site/assets/files/2672/20151011_200832.webp" alt="decorative"></source></source></source></picture></p><h2>4. The software</h2><h3>4.1 Raspberry (wheezy) installation</h3><p>Image available here: <a href="https://www.raspberrypi.org/downloads/raspbian/" target="_blank">https://www.raspberrypi.org/downloads/raspbian/</a></p><p>Installation instructions here:<a href="http://https://www.raspberrypi.org/documentation/installation/installing-images/" target="_blank"> https://www.raspberrypi.org/documentation/installation/installing-images/</a></p><p>Wifi configuration: <a href="https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md" target="_blank">https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md</a></p><h3>4.2 Installation of mpc/mpc (webradio player)</h3><pre><code>sudo apt-get install mpd mpc</code></pre><p>the list of mpc commands is available via:</p><pre><code>mpc help</code></pre><p>more info available here: <a href="http://www.musicpd.org/doc/user/">http://www.musicpd.org/doc/user/</a></p><p>Of particular note:</p><p>To add a web radio to the playlist:</p><pre><code>mpc add </code></pre><p>Examples:</p><pre><code>mpc add http://www.radiofeeds.co.uk/bbcradio1.pls # BBCmpc add http:... # Radio Canada</code></pre><p>To add an mp3 (after copying it to the correct directory on the SD card):</p><pre><code>mpc updatempc add </code></pre><h3>4.3 Realization of a script for the selection of the radio station</h3><p>The principle consists in realizing a "unix deamon" that probes the gpios, in order to associate the positions of the station selection button (P1) to a playback position in the playlist.</p><pre><code>#!/bin/sh### BEGIN INIT INFO# Provides: radio_ctl.sh# Required-Start:# Required-Stop:# Default-Start: S# Default-Stop:# Short-Description: Connect GPIO to mpc playlist# Description: Connect GPIO to mpc playlist### END INIT INFOdaemon_NAME="radio_ctl.sh"export PIDFILE=/tmp/radio_ctlPATH="/sbin:/bin:/usr/sbin:/usr/bin"#install#---------#sudo cp radio_ctl.sh /etc/init.d/. &amp;&amp; sudo chmod a+x /etc/init.d/radio_ctl.sh#sudo update-rc.d radio_ctl.sh defaults#to uninstall#---------#sudo update-rc.d -f radio_ctl.sh remove. /lib/lsb/init-functionsd_start () { V=0 VOLD=0 echo 23 &gt; /sys/class/gpio/export echo 24 &gt; /sys/class/gpio/export echo 25 &gt; /sys/class/gpio/export echo 12 &gt; /sys/class/gpio/export echo 13 &gt; /sys/class/gpio/export echo 26 &gt; /sys/class/gpio/export echo 19 &gt; /sys/class/gpio/export echo 21 &gt; /sys/class/gpio/export echo 20 &gt; /sys/class/gpio/export echo 22 &gt; /sys/class/gpio/export echo 27 &gt; /sys/class/gpio/export echo 17 &gt; /sys/class/gpio/export while [ true ] do [ `cat /sys/class/gpio/gpio23/value` = "1" ] &amp;&amp; V=1 [ `cat /sys/class/gpio/gpio24/value` = "1" ] &amp;&amp; V=2 [ `cat /sys/class/gpio/gpio25/value` = "1" ] &amp;&amp; V=3 [ `cat /sys/class/gpio/gpio12/value` = "1" ] &amp;&amp; V=4 [ `cat /sys/class/gpio/gpio13/value` = "1" ] &amp;&amp; V=5 [ `cat /sys/class/gpio/gpio26/value` = "1" ] &amp;&amp; V=6 [ `cat /sys/class/gpio/gpio19/value` = "1" ] &amp;&amp; V=7 [ `cat /sys/class/gpio/gpio21/value` = "1" ] &amp;&amp; V=8 [ `cat /sys/class/gpio/gpio20/value` = "1" ] &amp;&amp; V=9 [ `cat /sys/class/gpio/gpio22/value` = "1" ] &amp;&amp; V=10 [ `cat /sys/class/gpio/gpio27/value` = "1" ] &amp;&amp; V=11 [ `cat /sys/class/gpio/gpio17/value` = "1" ] &amp;&amp; V=12 if [ $VOLD != $V ]; then VOLD=$V mpc play $V [ $V = 1 ] &amp;&amp; echo "station 1" [ $V = 2 ] &amp;&amp; echo "station 2" [ $V = 3 ] &amp;&amp; echo "station 3" [ $V = 4 ] &amp;&amp; echo "station 4" [ $V = 5 ] &amp;&amp; echo "station 5" [ $V = 6 ] &amp;&amp; echo "station 6" [ $V = 7 ] &amp;&amp; echo "station 7" [ $V = 8 ] &amp;&amp; echo "station 8" [ $V = 9 ] &amp;&amp; echo "station 9" [ $V = 10 ] &amp;&amp; echo "station 10" [ $V = 11 ] &amp;&amp; echo "station 11" [ $V = 12 ] &amp;&amp; echo "station 12" fi sleep 0.1 done}d_stop () { if [ -e $PIDFILE ]; then kill `cat $PIDFILE` rm -f $PIDFILE fi}case "$1" in start) if [ -e $PIDFILE ]; then log_daemon_msg "Daemon $daemon_NAME already running" log_end_msg $? else log_daemon_msg "Starting system $daemon_NAME Daemon" log_end_msg $? d_start &amp; echo $! &gt; $PIDFILE fi ;; stop) if [ -e $PIDFILE ]; then log_daemon_msg "Stopping system $daemon_NAME Daemon" log_end_msg $? d_stop fi ;; restart|reload|force-reload) d_stop d_start ;; force-stop) d_stop killall -q $daemon_NAME || true sleep 2 killall -q -9 $daemon_NAME || true ;; status) status_of_proc "$daemon_NAME" &amp;&amp; exit 0 || exit $? ;; *) echo "Usage: /etc/init.d/$daemon_NAME {start|stop|force-stop|restart|reload|force-reload|status}" exit 1 ;;esacexit 0</code></pre><p>Mea-culpa: This bash script is clearly not the most efficient (pooling at the 10ms of the GPIO) but it has the merit to be simple and to have been written very quickly.</p><h3>4.4 Realization of a script to light a LED when the WiFi network is available</h3><pre><code>#!/bin/sh### BEGIN INIT INFO# Provides: wlan_led.sh# Required-Start:# Required-Stop:# Default-Start: S# Default-Stop:# Short-Description: led on if router answer to ping# Description: led on if router answer to ping### END INIT INFOdaemon_NAME="wlan_led.sh"export PIDFILE=/tmp/wlan_ledPATH="/sbin:/bin:/usr/sbin:/usr/bin"#install#---------#sudo cp wlan_led.sh /etc/init.d/. &amp;&amp; sudo chmod a+x /etc/init.d/wlan_led.sh#sudo update-rc.d wlan_led.sh defaults#to uninstall#---------#sudo update-rc.d -f wlan_led.sh remove. /lib/lsb/init-functionsd_start () { echo 18 &gt; /sys/class/gpio/export echo out &gt; /sys/class/gpio/gpio18/direction while [ true ] do ping -t1 -w1 -c1 192.168.1.1 &gt; /dev/null if [ $? = "0" ]; then echo 1 &gt; /sys/class/gpio/gpio18/value else echo 0 &gt; /sys/class/gpio/gpio18/value fi sleep 10 done}d_stop () { if [ -e $PIDFILE ]; then kill `cat $PIDFILE` rm -f $PIDFILE fi}case "$1" in start) if [ -e $PIDFILE ]; then log_daemon_msg "Daemon $daemon_NAME already running" log_end_msg $? else log_daemon_msg "Starting system $daemon_NAME Daemon" log_end_msg $? d_start &amp; echo $! &gt; $PIDFILE fi ;; stop) if [ -e $PIDFILE ]; then log_daemon_msg "Stopping system $daemon_NAME Daemon" log_end_msg $? d_stop fi ;; restart|reload|force-reload) d_stop d_start ;; force-stop) d_stop killall -q $daemon_NAME || true sleep 2 killall -q -9 $daemon_NAME || true ;; status) status_of_proc "$daemon_NAME" &amp;&amp; exit 0 || exit $? ;; *) echo "Usage: /etc/init.d/$daemon_NAME {start|stop|force-stop|restart|reload|force-reload|status}" exit 1 ;;esacexit 0</code></pre><p>Again, a Quick'n Dirty script, which lights a led when the Wifi router (here 192.168.1.1) responds to a ping.</p><p>The test is performed every 10 seconds.</p><h2>5 Conclusion</h2><p>Here is a short video that shows the result.</p><div height="315px" width="560px"></div>

Want to Work Together?

Every great project starts with a conversation.