Monday, April 8, 2013

Phone (Android) to Car Stereo WIFI Connection using a Raspberry Pi





Phone/Car WIFI Connection with a Raspberry Pi


What is this about?

The original idea behind this project was to be able to play the contents of my Android phone over my car stereo without any wires. As an added bonus I wanted to be able to control the car stereo from the phone with a nice interface. The best way to understand the goal of the project is to watch the following video (subtitles in English and Spanish):





Standing on the shoulders of giants

This project was inspired as an extension of the work by Doug Belshaw published here:

What will I need?

You will need the following elements (estimated cost in parenthesis):

-A Raspberry Pi (25 €), ideally with case
-A 4Gb SD Card (5€), ideally type 10
-A Wifi USB dongle (15€)
-A 12v micro-USB car adapter charger (5€)
-An AUX connection to your car stereo (mini headphone jack). If your car doesn't have a sound input, you may want to consider installing a device that will add that functionality. GROM audio sells a variety of such devices:
http://www.gromaudio.com/
I had to install one in my car, a 2005 Toyota Prius. Here are instructions with pictures of the process:


For the initial setup you will need to have the following elements available:

-Another computer to work on (hereafter referred to as your desktop but it could also be a laptop or any other "normal" computer)
-An SD card reader/writer
-A monitor or TV that you can connect to the Raspberry's HDMI or the Video Composite (yellow RCA)
-A USB keyboard
-A wired (ethernet) connection for the Raspberry initial setup
-A regular wifi connection for your phone and desktop. The wired connection must be in the same subnet (e.g., the ethernet cable is plugged to the same wifi router)

How do I do it?

Setting up your phone (part 1)

These instructions are for an Android phone. It probably requires being root for the SshDroid part. The wifi tethering may or may not require rooting, not sure. It should be possible to do the same with an iPhone if you can install a ssh server that can do sshmount

-Download and install the free App SshDroid from the Google Play Store
-Launch the SshDroid application and push Menu->Options. Check the options for Boot autostart, Lock wifi, Allow Password and set port number to 22
-For convenience download and install a one-tap wifi tether application and place the icon at a convenient location on your Home screen. This will be the button you tap to start the connection between your phone and the car. I use a widget called Tethering Widget
-Download and install an app to control XBMC. I suggest the official XBMC Remote and (my favorite), Spooky XBMC. This app will be your GUI to control the audio system, select files to play, etc
-Go to the wifi tethering (usually under Menu->Settings->Wifi) and enter a unique SSID and a password (example SSID=MichaelKnight, password=amanwhodoesnotexist). Make a note of both, the SSID and password

Setting up your Raspberry Pi

With the tethering activated, find out your phone's IP address. If you don't know how to do this, use your desktop computer. Connect to the phone hotspot (in the example above MichaelKnight). Under the network properties, find your IP address. In Linux you can do this by typing the command:
ifconfig
and look for inet addr. You should find a number that looks like this
192.168.43.36
The phone's IP adress would be obtained by replacing the last number with 1, so in the example above it would be 192.168.43.1. Make a note of this address.

Download the Raspbmc image from this website and follow the instructions on that page to write the image on the SDCard:

Take your Raspberry, put it in its case (if you have one) and connect a monitor or tv (via HDMI or Composite), Ethernet, a USB Keyboard and the wifi dongle. Stick the SDCard in and power the device with a micro-USB cable. You should see the yellow/red LED coming up. Here is a picture of what mine looked like (without the case)

You will see a red R logo on a black screen. Be patient, as it takes a while to boot up. The screen will then change to something like this:


At this point the device is downloading files to complete the installation and make sure you have the latest version of Raspbmc. When it's finished, you will be greeted by the XBMC welcome screen

You will be prompted by some configuration options (language, locale, etc). Using the USB keyboard, navigate to System -> System Info and make a note of the IP address. It will look like 192.168.1.43 (i.e., four numbers separated by dots). Go to your desktop computer and log into the Raspberry Pi using ssh (if you use Windows, you probably need to download a ssh client like Putty). The username is pi and the password is raspberry. In Linux you can do:
ssh pi@192.168.1.43    (use the correct IP address from above)
You will be prompted for the password. Once you are logged in, issue the following commands to install some required packages:

sudo mkdir /phone
sudo apt-get install sshfs
sudo apt-get install wireless-tools
sudo apt-get install xbmc-eventclients-xbmc-send
ssh-keygen     (press enter to accept all the defaults)
sudo cp -r ~/.ssh/* ~root/.ssh/
sudo nano /etc/rc.local
At this point you will be in an editor window. Go down and insert the following just before the last line:
/home/pi/launch_mountd.bash > /dev/null
Download and install my scripts with this command:
wget https://dl.dropbox.com/u/359387/raspbmc_car.tar.gz
tar zxvf raspbmc_car.tar.gz

Stop the tethering in your phone and connect it to the regular wifi. Make sure SshDroid is running. You should see a green message that says something like listening, address: sftp://root@192.168.1.34. This is the IP address of your phone in this network. We will now mount the phone's SDcard on the Raspberry filesystem by typing:
sudo sshfs root@192.168.1.34:/mnt/sdcard/ /phone/ -o allow_other
(but replacing the IP address with whatever you got in SshDroid and assuming that your SDCard is /mnt/sdcard/). It will prompt you for a password. The default password is admin. Once your directory has been mounted, we are going to copy there your public key so that there will be no need to prompt for a password in the future:
cp ~/.ssh/id_rsa.pub   /phone
Unmount the directory:
sudo fusermount -u /phone
Go back to SshDroid in your phone and hit Menu->Options. Uncheck the Password authentication and check the box next to Use Shared Keys below. Tap on Authorized Keys and then on the magnifying glass icon on the top right. Hit Browse to look for the public key file that you just copied over to your phone. Find the id_rsa.pub pub and tap on it. That's it. Your Raspberry will now be authorized to access your phone SDCard at any time. 

Now go back to the Raspberry. With the USB keyboard, navigate to System -> System -> Audio and change the Output Device from HDMI to Analogue. Then under System->Network, check "Web Interface" and "Allow XBMC to be controlled by other systems". If there is a port number, use 8080. Go back to the main menu with Esc and navigate to Programs -> Settings. Go to the network settings and enter the information for your wifi network. Do not use DHCP, it's better to give the Raspberry a fixed IP address. Use an address that is in the subnet as the phone, that means it must have the same first three numbers. Change the last number to something like 75. So, if your phone address was 192.168.43.1, choose the following for XBMC: 192.168.43.75. Set the port to 8080. Check the option to update the network configuration and exit. Disconnect the Ethernet cable, activate the wifi tethering on your phone and reboot the Raspberry Pi (with the USB keyboard, go back to the main menu by pressing Esc and use the button on the bottom left corner of the screen)

Your Raspberry Pi will now boot up, start XBMC and connect to your phone's hotspot. If you have speakers or headphones connected to the analogue audio output you will hear the messages "Connection detected", "Connected to your phone" and then "Mounting filesystem". Fire up your favorite XBMC remote application and make sure you can control XBMC with it. Once you are satisfied with the results, shutdown your Raspberry, disconnect all the cables, make the SDCard read-only (to avoid corruption because you're going to be shutting it down rudely) and you're ready to put it in you car. Congratulations! :)