The Raspberry Pi has the ability to be a FM radio transmitter, with the addition of an antenna – you don’t need to buy or make anything else! Plug a 20cm ish long wire to GPIO04 (pin 7) and that’s all the hardware you need – note it ONLY works on this pin. This opens up lots of interesting possibilities, and I’ll name three (instructions below). PLEASE NOTE: transmitting FM is illegal in certain countries on certain frequencies. Check this is legal where you are before you start broadcasting. Although the UK has made small radio transmitters legal (such as those you plug into an iPod to use in your car radio), these first have to be approved – and the Pi has not been for this purpose, hence it is technically not legal)
1. Radio Relay
At the moment we’re living outside of the UK, and my wife likes to listen to our local (UK) radio station, which she can do via the internet stream on a tablet. But, its not that convenient – you’ve got to start the tablet, open the app, find the station … and you don’t want to take your tablet it into the shower! But with the raspberry pi, we can get the stream from the internet, and send it out over FM, so it can be listened to just like a normal radio station!
A) Download the icrobotics Pifm code, extract it, and make it executable:
tar -xvf Pifm.tar.gz
sudo chmod +x pifm
B) Install sound exchange (sox), which will convert the stream into something Pifm can transmit:
sudo apt-get install sox libsox-fmt-all
C) Find the radio station of your choice – you’ll need an mp3 stream (sox cannot handle aac). The following website contains a list of UK internet radio stations:
D) Pipe the stream from sox to Pifm, and tune your radio in! This command tells sox to convert an mp3 of the Rutland Radio stream to wav; uses an input buffer to prevent gaps; uses the sampling frequency 22050Hz (you must get this correct for the stream you choose, otherwise it will be played too slowly or too quickly. Some stations use 48000Hz, or if not it will be a standard sampling rate). This is then piped (|) to pifm, and transmitted on 107Mhz.
(If you wanted to play a simple wav file on your SD card do: sudo ./pifm wavefile.wav 107)
sox -v .9 -t mp3 http://icy-e-04.sharp-stream.com:80/lincsfmrutland.mp3 -t wav –input-buffer 80000 -r 22050 -c 1 – | sudo /home/pi/pifm/pifm – 107
To stop the stream, simple press Ctrl-C. You will get a warning message on the Pi whilst this program is running, but you can ignore it. If you hear silence (not static), then the radio part is working but the file or stream is not correct. Sometimes this locks the audio, and even if you try multiple times and you may only hear the switch between silence and static – but a simple reboot should clear things up.
2. Remote Monitoring with Radio Feedback
The Raspberry Pi is perfect for gathering all kinds of data, using a particular kind of sensor attached to the GPIO port (with the correct hardware and software of course!). Often you might be monitoring something close by, such as the moisture your plant pots inside your house – so you can transmit the data back to another device via wifi pretty easily. But there are plenty of other uses for using a Raspberry Pi to monitor something outside of the reach of your wifi network – e.g. a weather / environment monitor at the bottom of your garden – but how can you get hold of the data remotely? There’s a couple of ways, firstly you could add a 3G dongle (if you have mobile reception) via USB, and send data over the internet. Secondly, there’s a very interesting radio transmitter add on for the Raspberry Pi the RPI900 , which can send data upto 500kb/s and up to a range of 40 miles. Both of these options are pretty good, except for the cost (3G dongle and sim card around £10 + approx £10 per month fees, RPI900 over £100 with the total kit). Now that the Raspberry Pi can transmit FM radio, this gives us another option – albeit limited. You could read any sensor via GPIO, convert this into meaningful information, use a text to speech module to read out the data audibly, and pipe this to the FM transmitter – then all you need is an FM radio to hear your data! Obviously, you can’t then manipulate the data on another device, nor can you save it. But for free, it’s not bad!
We’ll use espeak for our text to speech, since this does not require an online connection (although it sounds more robotic that google’s version – see here for options http://elinux.org/RPi_Text_to_Speech_(Speech_Synthesis) ). Install espeak with:
sudo apt-get install espeak
Then, as long as you’ve installed pifm as in part 1, you just need to generate a wav file with espeak and then play it with pifm. I’ve used a simple python script to do this, which you can then use to get the text you want to transmit – e.g. set it to transmit a temperature reading every few seconds. The script simply reads a string of characters. I’ve copied out the relevant parts below, or you can just download it (it needs to be in the same directory as pifm):
sudo wget http://download.andypi.co.uk/text2fm.py
subprocess.call([“espeak”, “-ven+”+voice, “-s”+speed,text,”-w”+file_name, “2>/dev/null”])
subprocess.call([“./pifm”, file_name, freq])#file_name, freq])
textToFM(‘Welcome to AndyPi World’, ‘f1’, ‘120’, ‘107’)
3. Transferring data via FM?
It is possible to actually transmit and receive digital data over FM with a couple of Raspberry Pi’s. Chirpio has already shown this concept is possible. It app transmits an URL using a chirping sound between mobile phones (note it allows you share pictures, but you need an internet connection to upload it first, the chirp only transmits the URL. A Raspberry Pi forum member has shown how something similar can work on the pi (FM radio telemetry hack). I’ve got this working after finding a way to install the CPAN modules, but not being a Perl expert means I’m not quite sure how it works. I get a lot of errors with it due to the radio not being tuned in very well – so I reckon its not viable without some error correction – and probably better to use 433MHz radio with a built in modem, which could transmit faster, would do all the modem error correction and is actually legal (in Europe). So that’s another project!