Introduction: Raspberry Pi Wireless Bluetooth Audio FM Radio Transmitter

Use your raspberry pi to wirelessly stream music directly from your phone to your radio! Fantastic to get music to your car stereo.

This instructable draws on information from two other instructables, and fills in the gaps to make them work together. All of the steps to complete your Wireless Bluetooth Audio FM Radio Transmitter will be listed here, but more (specific) information about each piece can be found in these instructables:

Also many thanks to the Imperial College Robotics Society, for the pifm software.

Step 1: The Prerequisites

The parts (to make it work):

  1. Raspberry Pi with power supply (any model, but A+ or B+ are recommended)
  2. At least 2GB micro SD card with Raspian on it
    • You'll need to burn the image to the SD card
    • For windows, use a handy utility such as Win32DiskImager
    • For mac, there's instructions here
  3. Bluetooth A2DP dongle (something like this)
  4. Antenna
    • Just a length of wire will do (30cm - 75cm), attached to GPIO 4

The extra parts (needed only to complete the installation)

  1. Ethernet cable (and internet access)
  2. USB keyboard (or ssh)
  3. HDMI display and cable (or ssh)

Before you plug the SD card into your pi, plug it into your computer. Open up the drive, and find the config.txt file. Uncomment the following lines. This will allow us to force audio to be played through the HDMI output, rather than the stereo jack, which is necessary for the pi to stream music from bluetooth to the fm transmitter (without which you'd just get a loud, screechy noise on both the audio jack and on your radio).

hdmi_force_hotplug=1
hdmi_drive=2

Step 2: The Software

Plug the SD card (with OS image) into your pi, plug in the USB keyboard, the ethernet cable, connect it to the display, and power it up.

If it asks for login credentials, the default username is pi and the default password is raspberry.

Proceed with downloading the necessary software by executing the following commands:

sudo apt-get upgrade
sudo apt-get install bluez pulseaudio-module-bluetooth python-gobject python-gobject-2 bluez-tools sudo apt-get install sox libsox-fmt-all cd /home/pi; mkdir fm; cd fm wget http://www.icrobotics.co.uk/wiki/images/c/c3/Pifm.tar.gz

tar -zxvf Pifm.tar.gz

Step 3: The Configuration

Make the following modifications

Add our user pi to the Pulse audio group

sudo usermod –a –G lp pi

Audio configuration settings

sudo nano /etc/bluetooth/audio.conf

Add the following text under

[General]:
Enable=Source,Sink,Media,Socket

Pulse settings

sudo nano /etc/pulse/daemon.conf

Comment this line by adding a semicolon

; speex-float-3

Add these lines

resample-method = trivial
exit-idle-time = -1

Bluetooth configuration

sudo nano /etc/bluetooth/main.conf

Modify these lines to customize your bluetooth device name and class

Name = nameyourbluetoothdevicehere
Class = 0x20041C

Bluetooth configuration of your device

sudo nano /var/lib/bluetooth/<bluetooth mac address>/config

Modify these lines to customize your bluetooth device name and class

(Note, the class may reset on reboot. Don't worry too much about it)

name nameyourbluetoothdevicehere
class 0x20041C

Input rules

sudo nano /etc/udev/rules.d/99-input.rules

Add this line

KERNEL=="input[0-9]*", RUN+="/usr/lib/udev/bluetooth"

Device initialization (auto-login)

sudo nano /etc/inittab

Modify the following line to look like this

(was something like this) 1:2345:respawn:/sbin/getty --noclear 38400 tty1

1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1

== OPTIONAL ==

Increase the audio quality of the fm transmitter

sudo nano /home/pi/fm/pifm.c

Find and modify the following lines

QUALITY=15 SQUALITY=20

Then recompile

cd /home/pi/fm; mv pifm pifm_bak;

g++ -O3 -o pifm pifm.c

Step 4: The Scripts

Create the following scripts

sudo nano /usr/lib/udev/bluetooth

Type this all into the empty editor, then save

Note and adjust the radio frequency and volume to your preference 

#!/bin/bash
AUDIOSINK="alsa_output.platform-bcm2835_AUD0.0.analog-stereo"
ACTION=$(expr "$ACTION" : "\([a-zA-Z]\+\).*")
echo "Executing bluetooth script...|$ACTION|" >> /var/log/bluetooth_dev
if [ "$ACTION" = "add" ]
then
  # Turn off BT discover mode before connecting existing BT device to audio
  hciconfig hci0 noscan
  # Turn off BT auto connect if it is still running
  sudo killall bluetooth-auto

  # set the audio output to the hdmi
  amixer cset numid=3 2
  # Set volume level to 100 percent
  amixer set Master 100%
  # Set sink volume to 125%
  pacmd set-sink-volume 0 0x12500

  for dev in $(find /sys/devices/virtual/input/ -name input*)
    do
	if [ -f "$dev/name" ]
	then
	
	  mac=$(cat "$dev/name")
	  # Add this mac address to list of trusted addresses
	  TRUST=$(grep "$mac" /usr/lib/udev/bluetooth-trust)
	  if [ -z "$TRUST" ]
	  then
		echo "Adding $mac to trusted addresses" >> /var/log/bluetooth_dev
		echo $mac >> /usr/lib/udev/bluetooth-trust
	  fi
	  
	  mac_underscore=$(cat "$dev/name" | sed 's/:/_/g')
	  bluez_dev=bluez_source.$mac_underscore
	  
	  # Set source volume to 125%
	  pacmd set-source-volume $bluez_dev 0x12500

	  sleep 1
	  CONFIRM=`sudo -u pi pactl list short | grep $bluez_dev`
	  if [ ! -z "$CONFIRM" ]
	  then
		echo "Setting bluez_source to:  $bluez_dev" >> /var/log/bluetooth_dev
		echo pactl load-module module-loopback source=$bluez_dev sink=$AUDIOSINK rate=44100 adjust_time=0 >> /var/log/bluetooth_dev
		sudo -u pi pactl load-module module-loopback source=$bluez_dev sink=$AUDIOSINK rate=44100 adjust_time=0 >> /var/log/bluetooth_dev
		echo "Killing any existing radio connections" >> /var/log/bluetooth_dev
		sudo killall pifm >> /var/log/bluetooth_dev
		echo "Connecting bluetooth output to radio input, playing on 87.7" >> /var/log/bluetooth_dev
		# Using $AUDIOSINK instead of 0 here doesn't seem to work, not sure why
		echo pacat -r -d 0 --latency-msec=50 | sox -t raw -r 44100 -e signed-integer -b 16 -c 2 - -t wav - gain -l 10 | sudo /home/pi/fm/pifm - 87.9 44100 stereo >> /var/log/bluetooth_dev
		sudo -u pi pacat -r -d 0 --latency-msec=50 | sudo -u pi sox -t raw -r 44100 -e signed-integer -b 16 -c 2 - -t wav - gain -l 10 | sudo /home/pi/fm/pifm - 87.7 44100 stereo >> /var/log/bluetooth_dev
	  fi
	fi
  done
fi

if [ "$ACTION" = "remove" ]
then
  # Turn on bluetooth discovery if device disconnects
  sudo hciconfig hci0 piscan
  # Turn on bluetooth auto discovery
  sudo /usr/lib/udev/bluetooth-auto &
fi

Finally, grant appropriate permissions to the script
sudo chmod 774 /usr/lib/udev/bluetooth
sudo nano /usr/lib/udev/bluetooth-auto

Type this all into the empty editor, then save


#!/bin/bash
while [ true ]
do
  sleep 1
  echo "Scanning for trusted devices" >> /var/log/bluetooth_dev
  for mac in $(sudo hcitool scan | grep ":" | awk '{print $1}')
  do
	trust=$(grep "$mac" /usr/lib/udev/bluetooth-trust)
	if [ ! -z "$trust" ]
	then
	  _BT_ADAPTER=`dbus-send --system --print-reply --dest=org.bluez / org.bluez.Manager.DefaultAdapter|awk '/object path/ {print $3}'`
	  BT_ADAPTER=${_BT_ADAPTER//\"/}
	  mac_underscore=$(cat "$mac" | sed 's/:/_/g')
	  echo "Connecting to device at: $mac" >> /var/log/bluetooth_dev
	  sudo dbus-send --print-reply --system --dest=org.bluez $BT_ADAPTER/dev_$mac_underscore org.bluez.AudioSource.Connect >> /var/log/bluetooth_dev
	  exit 0
	fi
  done
done


Finally, grant appropriate permissions to the script, and create the trust file
sudo chmod 774 /usr/lib/udev/bluetooth-auto
sudo touch /usr/lib/udev/bluetooth-trust
sudo nano /etc/init.d/bluetooth-agent

Type this all into the empty editor, then save

#!/bin/sh
#/etc/init.d/bluetooth-agent
### BEGIN INIT INFO
# Provides: bluetooth-agent
# Required-Start: $remote_fs $syslog bluetooth pulseaudio
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Makes Bluetooth discoverable and connectable to 0000
# Description: Start Bluetooth-Agent at boot time.
### END INIT INFO
USER=root
HOME=/root
export USER HOME
case "$1" in
  start)
	echo "initializing pulseaudio"
	sudo pactl info
	echo "setting bluetooth discoverable"
	sudo hciconfig hci0 piscan
	start-stop-daemon -S -x /usr/bin/bluetooth-agent -c pi -b -- 0000
	echo "bluetooth-agent started pw: 0000"
	sudo /usr/lib/udev/bluetooth-auto &
        echo "bluetooth-auto-discovery started"
        sudo /home/pi/fm/pifm /home/pi/fm/silence 87.7 44100 stereo
	echo "pifm started at 87.7, playing silence"
	;;
  stop)
	echo "Stopping bluetooth-agent"
	start-stop-daemon -K -x /usr/bin/bluetooth-agent
	;;
  *)
	echo "Usage: /etc/init.d/bluetooth-agent {start|stop}"
	exit 1
	;;
  esac
exit 0

Finally, create the silence file, grant appropriate permissions to the script, and add it to the list of programs that run on startup


sudo touch /home/pi/fm/silence
sudo chmod 755 /etc/init.d/bluetooth-agent
sudo update-rc.d bluetooth-agent defaults

Step 5: Enjoy

That's it. Restart your pi - either by just disconnecting and reconnecting the power supply, or by running the command:

sudo reboot

Disconnect the keyboard, display, and ethernet cable. You no longer need these.

You should see the pi as a bluetooth device to which you can connect. After connecting the first time, the pi will add your phone to its list of trusted devices. Each time the pi powers up, it will attempt to automatically connect to nearby trusted devices. Likewise, if you move out of bluetooth range, the pi will attempt to reconnect when you're back in range.

Begin playing music, send it to the pi, and tune your radio to your chosen frequency (default 87.7).