Our final project in this chapter is the ultimate in mischievous mayhem. We're going to use our Pi Zero to take over the airwaves by transmitting whatever audio we choose to a standard FM radio by using a rather nifty piece of software called PiFm. PiFm was originally written by Oliver Mattos and Oskar Weigl, and updated by a guy called Ryan Grassel.
The Pi Zero is perfect for this, because of its small size, and can be integrated with our wearable audio recorder built in Chapter 2 , Audio Antics, so you can interfere with FM radio reception out in the field.
In order to get range out of your radio jammer you'll need to solder a 20cm piece of wire to pin 4 of your Pi Zero's GPIO connector to act as an antenna. This should give a surprising range—perhaps up to 20 or 30 meters.
Compatibility Note: Owing to the way this software works, it's dependent on the operating clock speed of your Raspberry Pi. Having tested this, PiFm currently doesn't work on the Pi Model 2 or Pi Model 3 because they operate at a higher operating speed than it was originally designed for. Therefore, this project will only work with the Pi Zero and also earlier models such as the Model 1 B+ and B.
Let's get the code and compile it:
pi@raspberrypi ~ $ sudo git clone https://github.com/rm-
hull/pifm
pi@raspberrypi ~ $ cd pifm
pi@raspberrypi ~ $ sudo make
Copy across a few of your favorite MP3 files to the Pi and put them in the default /home/pi/Music
directory.
PiFm currently works only with 16-bit 22050 Hzfiles in the WAV format. But this is OK, as we can use the SoX sound exchange processor we installed in Chapter 2, Audio Antics, to take your MP3 file and convert it into the required WAV file on-the-fly and direct it to PiFm using the following command:
@raspberrypi ~ $ sox -t mp3 /home/pi/Music/somesong.mp3
-t wav -r 22050 -c 1 - | sudo /home/pi/pifm/pifm - 88.5
The above command works by converting the MP3 file into a WAV file using SoX, changing its audio sampling rate to 22050 Hz (-r 22050
), and down-mixing the track to a single mono channel (-c 1
). The converted track is then sent to the standard output, denoted by the hyphen sign (-) and is then piped (|) into the input of the pifm
command.
Now we have our favorite tune broadcasting, grab a radio and tune your dial to 88.5 MHz to listen to the audio output of your Pi Zero.
You can also do other clever things such as broadcasting your voice by piping the output of the arecord
command, as we did in Chapter 2
, Audio Antics,to send to an audio device, or by using SoX to access a live Internet stream and broadcasting that over FM.
3.142.42.176