In this article I will share my experience with turning the Raspberry Pi and a pair of cheap speakers into an automatic radio player. I provide some tips on configuring the software, as well as code that you will need.
I like to wake up to the radio playing local news station. Unfortunately, in the place where I live, the reception of the FM radio is rather weak, so – until few months ago – I had to set my desktop computer to automatically wake up every morning at 6:30 and run the audio stream in foobar2000. This solution has many disadvantages:
- I had to remember to sleep (not shut down) my desktop computer every evening.
- I had to remember to unplug headphones and turn the speakers on, set the proper volume.
- My desktop computer is rather noisy.
- When I wanted to turn the volume of radio stream up or down, I had to get up from my bed.
- When leaving home, I had to turn on the display, log into the computer and turn it off (no need for the computer to stay on all day when I’m out).
When long awaited Raspberry Pi was finally out, I decided to use it as a remedy to the problems mentioned above. In case you don’t know – Raspberry Pi is “an ARM GNU/Linux box for $25″. It’s a small but wonderful piece of hardware with ARM chip, 256MB RAM, HDMI, audio, USB and LAN ports, as well as a SD card slot. It can run some customized versions of Linux (debian, Arch Linux) or RISC OS.
After receiving my unit, I did some research and it turned out, that there is multimedia player – omxplayer – designed specifically for Raspberry Pi. It’s documentation is not perfect, but there are some resources, that can be googled. I made a plan to replace my current setup with Raspberry Pi powered system.
To accomplish this task, I needed some basic components to install Raspbian – an SD card, USB keyboard and mouse and HDMI cable with HDMI->;DVI adapter, so I could connect it temporarily to my display. Installation of both the system and omxplayer is straightforward. To enable control panel, you should also install some http server (I use lighttpd, because of its, well, lightness) and PHP.
On the hardware side, I bought cheap speakers for around $10. After installation you no longer need keyboard or display connected to the computer, so I put the unit into the small cardboard box, plugged in the speakers, LAN cable and USB charger and put the whole setup in the corner of my room.
On the software side, the setup is relatively simple. As omxplayer can receive text commands from the named pipe, it is sufficient to create one and tell the omxplater to read from it. Experimentally, I came into conclusion, that the player won’t start if it didn’t receive anything from the pipe.
So, the code to create the pipe looks as follows:
#!/bin/bash PIPE="/tmp/omx.pipe" if [ ! -p "$PIPE" ] ; then mkfifo $PIPE chmod a+w $PIPE fi
Then, you only need to put following commands to your crontab file (crontab -e):
28 6 * * 1-5 /home/pi/mkmypipe.sh 29 6 * * 1-5 timeout 4h omxplayer http://mainstream.radioagora.pl/tuba10-1.mp3 < /tmp/omx.pipe 30 6 * * 1-5 echo -n . > /tmp/omx.pipe
You can easily deduce, what are the paths and the URL. As I wanted the player to turn off after 4h (because of my laziness: I don’t want to bother turning off the radio every time I leave home), I used timeout 4h, which says the player to turn itself off after defined time. Commands are run in the one minute intervals. I tried to run them all at once, but omxplayer had some problems with starting the playback in such situation, so I gave up. Because of cron’s flexibility, you can specify many different situations, i.e. different radio stations played at different times, depending on the day of the week.
The final touch is to add small PHP application to send commands to the omxplayer (via named pipe). Main part of such application is:
$f = fopen("/tmp/omx.pipe", 'w+'); fwrite($f, $_GET['cmd']); fclose($f);
The commands sent to the omxplayer are taken from the cmd parameter. Here you can find a list of valid commands.
If you don’t want to create your own HTML code, you can find my control panel, as well as other files and instructions, in this Bitbucket mercurial repository. (I used some open source HTML template).
Of course my project isn’t the only one solution of this problem. Similar projects appear once a while, i.e. this Node.js omxcontrol package.
And here are two photos of my setup. They’re taken by phone, so please forgive the poor quality
Update (28.05.2013): Sometimes I want to turn on the radio other time, that the predefined hour. Thus, I added an option to the panel, that just turns on the radio for 2 hours. Please check the code repository.