Here's the hassle free way to make your raspberry pi play your music files over FM-radio!
Skip to the next step if you are in a hurry ;)
For a couple of days I have been messing around with PiFM, and I absolutely love having my own radio station.
So far I've gotten about 50m range outside, not much, but certainly enough to broadcast at and around home. It is possible to improve the range drastically by adding filters and signal amplifiers , but broadcasting with that kind of range is illegal without a license -.-
Yup, pifm is awesome!!! But its a little impractical to use, you have to convert every different format and its troublesome to write the long one-liners over and over. It's just not practical to use if you're outside controlling it from your phone via ssh.
I wanted it to be as easy to use as the programs aplay and play, so I made PiFMPlay :D
PiFMPlay is here to make it easier to control your PiFM-radio broadcast.
I DIDN'T make PiFM! Kudos and thanks to icrobotics :D
I DID make the PiFMPlay script, and it's on github. Feel free to fork it from there: https://github.com/Mikael-Jakhelln/PiFMPlay
Now, lets get to it!
Step 1: Tl;dr (Too Long Didn't Read)
This is the quickest way to set up pifmplay.
Open a terminal on your Raspberry Pi.
Move the 'pifmplay' folder to /home/pi:
mv PiFMPlay-master/pifmplay ~/pifmplay
Go into the pifmplay folder:
Then add execution permissions:
sudo chmod +x pifm pifmplay
Download & Install media converters:
sudo apt-get install ffmpeg sox libsox-fmt-all
Now edit you users bash config file:
and type this at the bottom of the file:
restart the pi:
Attach a 30cm wire to to GPIO4 (That's Pin-7)
Turn your FM-radio to frequency "91.3"
Start FM-Broadcast with:
pifmplay ~/pifmplay/starwars.wav 91.3
If you cannot hear anything on the radio, try adjusting your antenna
Step 2: Ingredients:
What you will need:
- A raspberry pi up and running with internet
- a way to type commands into you pi :P (ssh or a screen and a keyboard)
- a 30cm wire to use as antenna (any antenna/wire will do)
- a brain
Software(download links comes later in the instructable):
- pifm and pifmplay
- sound/music files
Step 3: Add Antenna to GPIO4
- Connect your antenna (the 30 cm wire) to GPIO4 on your raspberry pi.
And that's all you have to with the hardware.
Step 4: Install Sound Converters
PiFMPlay uses sox and ffmpeg to automatically convert audio files and play them with pifm.
So to use it we need to install sox and ffmpeg. Type this into a terminal on your pi:
sudo apt-get install ffmpeg sox libsox-fmt-all
Step 5: Install PiFMPlay
Do this in a terminal on your pi:
go to your home directory:
download pifmplay from github
Move pifmplay to where you want it, e.g:
mv PiFMPlay-master/pifmplay /home/pi/pifmplay
remove empty folder:
rm -r PiFMPlay-master
Now go into the pifmplay folder, e.g:
change permissions so we can run it as a program:
sudo chmod +x pifm
sudo chmod +x pifmplay
Step 6: Change Default Frequency
change the default frequency in the pifmplay script.
find this line, it should be at the top somewhere..
and change 91.3 to your preffered frequency
Step 7: PiFMPlay Is Ready for Your Orders, Now Play Some Music
Go into the pifmplay folder via a terminal, and type:
sudo sh pifmplay .
(this plays some music on the default frequency. Default is 91.3 if you didn't change it )
sudo sh pifmplay "/path/to/folder/or/sound.file" 91.3
(change 91.3 to your desired broadcast frequency.)
sudo sh $HOME/pifmplay/pifmplay "/home/pi/music/Daft Punk" 91.3
sudo sh /home/pi/pifmplay/pifmplay "/home/pi/music/Daft Punk/Daft Punk - Technologic.mp3" 91.3
Step 8: How to Use Pifmplay
Basically you just send what you want to play as the first parameter to the script, and the frequency to broadcast on as the second.
But read on, because there is more:
to play a file:
sudo sh $HOME/pifmplay/pifmplay "Name of - song.mp3"
to play a song on another frequency:
sudo sh $HOME/pifmplay/pifmplay "Name of - song.m4a" 91.3
To play a folder:
sudo sh $HOME/pifmplay/pifmplay .
sudo sh $HOME/pifmplay/pifmplay "/path/to/music/folder" 91.3
to pause the broadcast:
sudo sh $HOME/pifmplay/pifmplay pause
sudo sh $HOME/pifmplay/pifmplay resume
sudo sh $HOME/pifmplay/pifmplay stop
to play the next file in the folder:
sudo sh $HOME/pifmplay/pifmplay next
If you want to run pifmplay in the background, type:
sudo sh pifmplay $HOME/pifmplay/pifmplay "/path/to/folder/or/file" &>/dev/null &
Now you are done installing pifmplay and you can enjoy using your fm-radio transmitter ;)
However; there's still some more ingredients you might want to add to your pie to make it even more awesome ;)
Step 9: (Optional)Now That PiFMPlay Is Up, Add More Music
To play music from a usb hdd or flashstick do this:
First things first:
Preheat the oven to 50°c, fold in a usb-drive into your pie, put the pie in the oven, let it cook for 50µs.
(DON'T PUT YOUR PI IT IN THE OVEN!!!! Just insert a usb drive into your pi)
Now you must find your usb media, so type in:
ls -l /dev/disk/by-uuid
That gives you a list of storage devices connected to the pi.
It should look like this:
pi@pi ~ $ ls -l /dev/disk/by-uuid
lrwxrwxrwx 1 root root 15 Jan 1 1970 676B-0317 -> ../../mmcblk0p5
lrwxrwxrwx 1 root root 10 Jan 1 1970 1B23-1234 -> ../../sda1
lrwxrwxrwx 1 root root 10 Jan 1 1970 B321-4321 -> ../../sda2
lrwxrwxrwx 1 root root 15 Jan 1 1970 A1B2-3C4D -> ../../mmcblk0p1
this is the disk uuid & this is the drivename. remember these, we will use them later
Your usb media drive should be called something like /dev/sda1 or /dev/sdb1, where the number at the end is the partition number.
sda is the first drive while sdb is the second, the third and fourth drive would be sdc and sdd respectively
However, its NOT /dev/mmcblk0px, that is the sdcard your OS runs from.
Now, make up a name for your drive.
To mount your media, type:
sudo mkdir /media/yourmadeupname
sudo mount -t vfat /dev/harddrivename /media/yourmadeupname
(if your drive is not formatted as FAT16 or 32, you have to change "vfat" to something else)
To make it mount it automatically at boot, we need to edit /etc/fstab
make a backup of /etc/fstab with:
sudo cp /etc/fstab/ /etc/fstab.backup
edit /etc/fstab with:
Remember your uuid and your made up drivename?
Add this line the the end of the file:
UUID=youruuid /media/yourmadeupname vfat rw,defaults 0 0
Replace "youruuid" with your drives actual uuid, and "yourmadeupname" with the name you made up :P
If you didn't understand my instructions, or something was unclear check out:
or google it ;)
Step 10: (optional) Add Pifmplay to Your $PATH
If you want to type even less to play your music, you must add the pifmplay folder to your $PATH
This can be done in so many ways, and i'm not sure if this is the correct way. But it works :P
on your pie, type this:
now type this into the bottom of the file
now you should be able to type
sudo pifmplay "file/or/folder/to/play"
from anywhere, Yey! ¦-D