How I listen to my favourite radio
Published:
I moved out of my country more than one year ago, and one of the things I miss the most is being able to easily listen to my favourite radio (Radio Popolare). Since the radio has a web streaming, I figured out I could use my Raspberry Pi to play it. Quite a boring story up to now. The cool (at least for me) part of it, is that I managed to plug a rocker switch to the RPi to have an easy and quick ON/OFF button to play the radio.
The RPi runs Volumio, a very cool music player originally developed by Michelangelo Guarise. This means it can be used to play Spotify, music from a USB stick, AirPlay, and maybe other cool things I don’t know about. So here comes a report of what I did exactly, in case somebody wants to build something similar.
Disclaimer
I am not a programmer, nor an electronic engineer. I was a bit lazy in setting up the case. The final result doesn’t work perfectly: sometimes when turning the switch to OFF, it actually sends the ON command. I suspect it might be a matter of loose connections.
Material
- Raspberry Pi 3 Model B+
- HiFiBerry DAC+
- Joy-it rp port-doubler (Link to a Swiss reseller)
- 3 Jumper wires and 2 resistors (1 x 1kΩ, 1 x 10kΩ) from this Arduino kit
- the rocker switch
- a RCA stereo cable ending with your favourite connector (a small jack in my case, like this)
- a power supply for the Raspberry Pi
- a Micro SD card
- the very simple case
Software
- Volumio version 2.452 Volumio
- a python script that deals with the switch
How to put things togheter
Software part
The OS: Volumio
First thing, download and install Volumio on the Micro SD card. You can find the instructions on Volumio’s website, just scroll down to find the guide.
Interacting with Volumio, API requests
The system I use to turn on and off the radio is rather simple; I use one API request to ask Volumio to play a specific playlist and another to stop anything that might be playing. API requests to Volumio interface are documented here.
I use a playlist to be able to play the webradio from API. I had found this idea somewhere on the web, but cannot find the page any longer. Anyway the setup goes as follows:
- Access Volumio interface
- Go to Browse, then WebRadio, and find your WebRadio
- On the far right, click on the three dots and click on “Clear and Play” (beware, this will clean your current queue)
Go to the Queue page, and save the queue as a playlist (top right). I called my playlist “rp_autoplay”.
This allows us to use the API request
http://127.0.01:3000/api/v1/commands/?cmd=playplaylist&name=rp_autoplay
to play the radio that is saved in the playlist “rp_autoplay”. The request for stopping anything from playing is:http://127.0.0.1:3000/api/v1/commands/?cmd=stop
Note that the IP address (127.0.01
) can be replaced bylocalhost
.
Copy and enable rocker.py
To deal with the switch I wrote a python script that checks for changes in the state of the switch and subsequently sends the API requests to Volumio.
The script can be found here.
The module RPi.GPIO is needed and was not installed on my Volumio version so I had to install it manually:
- Enable ssh on Volumio by following the guide in this page.
ssh
to the Raspberry and installgcc
andRPi.GPIO
following this guide. Note that in my case/etc/apt/sources.list
was already set up for jessie.wget
orscp
the python script to the RaspberryEnable the script to run at boot by editing
/etc/rc.local
, by adding the linesudo python /home/volumio/rocker.py &
Note that the path has to be consistent with the location of the script, and the
&
is essential. Michelangelo Guarise says that we should use a different system to have a script run at boot, but I found thatrc.local
works as well.
to be continued…