Recording FM radio with RetroPie

Note: This works on just about any Linux install. I just wanted the excuse to set up a RetroPie.

Installing and Configuring RetroPie

RetroPie recommends at least a Pi 3. I went with a Pi 4 8GB in order to future proof my setup. Since I plan on leaving it constantly on and hooked up to my home theater I wanted a passively cooled case. The Flirc Pi 4 case fit my needs and looks good as well. Installing the Pi in the case was easy and straightforward. Attach the thermal pad, place the Pi in the case and screw it together. That’s it.

Installing RetroPie is just as easy. Download the RetroPie image, extract it and install it onto a MicroSD card. By default the pi runs samba which allows you to copy your roms over the network to the pi. This is great for ease of use but I don’t want to have to worry about losing my roms if the MicroSD card fails. Instead we’ll be serving the roms over the network.

I originally configured my rom share on my TrueNAS (formerly FreeNAS) server for NFS. That way I could set it to read only with mapall permissions for easy connection. However, NFS doesn’t use a set port and I have my home theater firewalled off because it’s connected via Powerline. NFS can be configured to run on a specific port but that has to happen per client and it’s much easier to just switch to SMB.

Once you configure the network share and open the ports then you just need to set up RetroPie to use the share. First you need to enable SSH on the RetroPie. Then you can copy the existing folder structure from the /home/pi directory to the network share. This allows it to properly pick up the roms. Once that’s done you need to run raspi-config and tell it to wait for the network to boot. After that edit your autostart.sh and add the following line, edited to your share settings.

sudo mount -t cifs -o username=something,password=something,nounix,noserverinouid=userid,forceuid,gid=usergroup,forcegid //REMOTEHOST/path/to/roms /home/pi/RetroPie/roms

I created a user just for the pi on the NAS so that I don’t have to worry about someone getting access to the pi and seeing the password. If they do they only have readonly rom access and can’t hurt anything. If you elect to save games on the NAS as well, I recommend making a separate share owned by the same user which has read and write permissions. I did have to add the uid and gid options in order for the permissions to properly line up.

One thing to note is that you will need to enable write permissions for the roms share in order to install new emulators. Once they’re installed you can change the share back to read only.

Recording FM Radio

Now that RetroPie is all configured and running we move on to recording the radio. We’ll be using a rtl usb dongle to receive the radio signals. Once the radio show has been recorded we’ll then email to ourselves.

First we need to install rtl in order to receive the signal and ffmpeg to convert it to an mp3. We also need ssmtp to email it and mpack to include the mp3 as an attachment.

sudo apt install rtl-sdr ffmpeg mpack ssmtp

Once you’ve installed all of those packages we need to configure ssmtp to know what smtp server to use. Open up /etc/ssmtp/ssmtp.conf and add the following lines. Edit for your email and server.

root=your_email@gmail.com
mailhub=smtp.gmail.com:465
FromLineOverride=YES
AuthUser=your_email@gmail.com
AuthPass=your_password
UseTLS=YES

If you are not receiving emails you can add Debug=YES as well and then look in /var/log/syslog to see what the problem is.

Now create a radio.sh file in the pi home directory and add the following lines.

now=date
rtl_fm -M wbfm -f RADIO_FREQUENCY | ffmpeg -f s16le -ac 1 -ar 32k -i pipe:0 -acodec libmp3lame -ab 32k -f mp3 -y -t 01:00:00 radio.mp3
mpack -s "SHOW_TITLE ${now}" radio.mp3 EMAIL_ADDRESS

The first line saves the current date. If you just used date in the last line the time would be the end of the show instead of the beginning. Originally I was going to use timeout to set the recording time and provide a file for ffmpeg to convert. But ffmpeg accepts a -t option which is the length of the recording. Once that is reached it will automatically stop rtl_fm. Make sure you adjust it appropriately. Also set RADIO_FREQUENCY to the correct station. On the last line configure the title for your email subject and provide the appropriate email address to recieve the recording. Keep in mind that the sender of the email will be whatever email address you have configured in your ssmtp.conf file.

Lastly, you need to configure the recording script to run at the appropriate time. As the regular pi user run the following.

crontab -e

This will bring you into the crontab editor. Add the following line to the end of the file.

0 HOUR * * WEEKDAY /home/pi/radio.sh

If the recording is a weekly show that starts on the hour simply replace the HOUR and WEEKDAY values with the appropriate numbers. Make sure you go through the raspi-config and set the appropriate date, time and time zone. Additionally, you will have to reboot once you’ve set the time zone as it doesn’t take effect until then. I found that out the hard way.

You can test the radio script separately if you don’t want to wait for the appropriate time. Simply run the following command.

/home/pi/radio.sh

This will record whatever happens to be on the radio at the moment but will let you know everything is working.

If you want to record multiple different shows then you can configure the radio.sh to accept variables and call it multiple times with the appropriate settings from the crontab, but I leave that as an exercise for the reader.

Leave a Reply

Your email address will not be published. Required fields are marked *