Spiria logo.
Jan D’Orgeville
December 07, 2015.

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 !

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 !

decorative

decorative

1. The shopping list

  • A rapsberry-PI model A+
  • One SD card
  • A WiFi USB dongle
  • A 12-position circular switch
  • A 10K potentiometer equipped with a switch
  • A 5V 500mA power supply
  • One KIT Low frequency amplifier

2. The schema

decorative

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.

It should be noted that :

  • 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.
  • 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.
  • "Ready Indicator" is an LED simply plugged on a GPIO which will be used to indicate if the WiFI network is available.
  • 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.

decorative

3. Montage

decorative

decorative

4. The software

4.1 Raspberry (wheezy) installation

Image available here: https://www.raspberrypi.org/downloads/raspbian/

Installation instructions here: https://www.raspberrypi.org/documentation/installation/installing-images/

Wifi configuration: https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md

4.2 Installation of mpc/mpc (webradio player)

sudo apt-get install mpd mpc

the list of mpc commands is available via:

mpc help

more info available here: http://www.musicpd.org/doc/user/

Of particular note:

To add a web radio to the playlist:

mpc add 

Examples:

mpc add http://www.radiofeeds.co.uk/bbcradio1.pls # BBC
mpc add http:... # Radio Canada

To add an mp3 (after copying it to the correct directory on the SD card):

mpc update
mpc add 

4.3 Realization of a script for the selection of the radio station

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.

#!/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 INFO

daemon_NAME="radio_ctl.sh"
export PIDFILE=/tmp/radio_ctl
PATH="/sbin:/bin:/usr/sbin:/usr/bin"

#install
#---------
#sudo cp radio_ctl.sh /etc/init.d/. && 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-functions

d_start () {
	V=0
	VOLD=0
	echo 23 > /sys/class/gpio/export
	echo 24 > /sys/class/gpio/export
	echo 25 > /sys/class/gpio/export
	echo 12 > /sys/class/gpio/export
	echo 13 > /sys/class/gpio/export
	echo 26 > /sys/class/gpio/export
	echo 19 > /sys/class/gpio/export
	echo 21 > /sys/class/gpio/export
	echo 20 > /sys/class/gpio/export
	echo 22 > /sys/class/gpio/export
	echo 27 > /sys/class/gpio/export
	echo 17 > /sys/class/gpio/export
	while [ true ]
	do
		[ `cat /sys/class/gpio/gpio23/value` = "1" ] && V=1
		[ `cat /sys/class/gpio/gpio24/value` = "1" ] && V=2
		[ `cat /sys/class/gpio/gpio25/value` = "1" ] && V=3
		[ `cat /sys/class/gpio/gpio12/value` = "1" ] && V=4
		[ `cat /sys/class/gpio/gpio13/value` = "1" ] && V=5
		[ `cat /sys/class/gpio/gpio26/value` = "1" ] && V=6
		[ `cat /sys/class/gpio/gpio19/value` = "1" ] && V=7
		[ `cat /sys/class/gpio/gpio21/value` = "1" ] && V=8
		[ `cat /sys/class/gpio/gpio20/value` = "1" ] && V=9
		[ `cat /sys/class/gpio/gpio22/value` = "1" ] && V=10
		[ `cat /sys/class/gpio/gpio27/value` = "1" ] && V=11
		[ `cat /sys/class/gpio/gpio17/value` = "1" ] && V=12	
		if [ $VOLD != $V ]; then
			VOLD=$V
			mpc play $V
			[ $V = 1 ] && echo "station 1"
			[ $V = 2 ] && echo "station 2"
			[ $V = 3 ] && echo "station 3"
			[ $V = 4 ] && echo "station 4"
			[ $V = 5 ] && echo "station 5"
			[ $V = 6 ] && echo "station 6"
			[ $V = 7 ] && echo "station 7"
			[ $V = 8 ] && echo "station 8"
			[ $V = 9 ] && echo "station 9"			
			[ $V = 10 ] && echo "station 10"			
			[ $V = 11 ] && echo "station 11"			
			[ $V = 12 ] && 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 &
				echo $! > $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" && exit 0 || exit $?
                ;;
        *)
                echo "Usage: /etc/init.d/$daemon_NAME {start|stop|force-stop|restart|reload|force-reload|status}"
                exit 1
                ;;
esac
exit 0

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.

4.4 Realization of a script to light a LED when the WiFi network is available

#!/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 INFO

daemon_NAME="wlan_led.sh"
export PIDFILE=/tmp/wlan_led
PATH="/sbin:/bin:/usr/sbin:/usr/bin"

#install
#---------
#sudo cp wlan_led.sh /etc/init.d/. && 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-functions

d_start () {
	echo 18 > /sys/class/gpio/export
	echo out > /sys/class/gpio/gpio18/direction
	while [ true ]
	do
		ping -t1 -w1 -c1 192.168.1.1 > /dev/null
		if [ $? = "0" ]; then
		   echo 1 > /sys/class/gpio/gpio18/value
		else
		   echo 0 > /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 &
				echo $! > $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" && exit 0 || exit $?
                ;;
        *)
                echo "Usage: /etc/init.d/$daemon_NAME {start|stop|force-stop|restart|reload|force-reload|status}"
                exit 1
                ;;
esac
exit 0

Again, a Quick'n Dirty script, which lights a led when the Wifi router (here 192.168.1.1) responds to a ping.

The test is performed every 10 seconds.

5 Conclusion

Here is a short video that shows the result.

 

 

 

 

 

 

Share this article: