I wanted to set up a radio alarm in my son’s room but didn’t have a clock radio. That’s no issue, clearly this is a software problem!

I thought I might be able to hack something together with the raspberry pi, and indeed I was right.

Setup

The only thing you need to install on the Pi is mplayer, which you can get by:

sudo apt-get install mplayer

I found the URL to play Radio 1 from here: http://www.suppertime.co.uk/blogmywiki/2015/04/updated-list-of-bbc-network-radio-urls/

Script

I then created this script:

#!/bin/sh

/usr/bin/mplayer -msglevel all=-1 http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio1_mf_p &
TASK_PID=$!
sleep 2400
kill $TASK_PID

This will start mplayer playing Radio 1 and then kill it after 40 minutes (2400 seconds). I saved this in /usr/bin/radio1.sh, making sure to:

sudo chmod +x /usr/bin/radio1.sh

Timing

The last part is to make sure it plays at 7am every day. This is where cron comes in. This will let you run commands on a schedule. If you run crontab -eit will open an editor to let you enter the rather arcane cron syntax. I entered:

0 7 * * * /usr/bin/radio1.sh

which means 7am every day.

Job done, just in time for bed.

PS

I originally used the timeout command to run the radio for 40 minutes. This hung mplayer, but only when running in a script. I’ve shaved enough yaks in my time to know a knotty problem when I see it, so I cheated and did something else instead.