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! :)


23 comments:

  1. The YouTube video is marked as "private" and won't play. Do you have a link to the video somewhere else or can you change the settings so we can view it?

    ReplyDelete
  2. Wouldn't it been easier to use one of those mp3-player -> c-cassette adapter thingies?

    ReplyDelete
    Replies
    1. Depends what you are trying to do
      This is wireless and allows you to use the power of the XBMC remote

      Delete
  3. This is awesome. Pretty ingenious use of the Pi. I just wish there was a way to stream all audio from phone to car (i.e. phone calls, pandora, etc..)

    ReplyDelete
    Replies
    1. Hmmm... maybe via Bluetooth. The easiest way would be to install a BT adapter to the audio system. Or one could use a BT dongle for the Raspberry :)

      Delete
  4. While I'm an Android guy myself, an easier way to accomplish this via iOS is to install RaspBMC on the Pi as above, set up the Tethering as above, enable Airplay in XBMC then when the Pi is connected via wifi to your phone you can simply play from your phone and turn on airplay in the media player.

    ReplyDelete
    Replies
    1. Yes, that's interesting and I've been meaning to try that (in fact, you may have noticed that I included airplay as one of the keywords for this blog entry). However, I was under the impression that it doesn't work with tethering, that somehow you need a proper network with a router (no, I don't understand why that would be the case either :) )

      Delete
    2. I've been trying to do use this technique with my iphone 5 but for some reason the raspberry pi isn't picking up on my iOS hotspot.
      Any other network things that need to be changed do you think?

      Delete
    3. Ryangodammit lpswich, I don't see any reason why the r-pi wouldn't connect to your iphone hotspot. But I've never had an iOS device so I can't really help, maybe somebody else here has any ideas?

      Delete
    4. I have now managed to get the rpi to connect to my hotspot but only very seldom does the xbmc remote notice an xbmc instance. Even then if it does manage to connect it only stays connected for a few seconds. Not sure whats going on, feels like an iOS thing.
      Might have to try and make a rpi based adhoc network

      Delete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. what a great article, you have shared a very helpful ideas..i have gain some information which i haven't knew..

    car audio pittsburgh

    ReplyDelete
  7. Hey nice post,

    I like the valuable info, you have provides in your article.


    Thanks for sharing
    Android Car Audio

    ReplyDelete
  8. To produce high quality sounds it is always necessary that your car is equipped with the best speaker system.

    Car entertainment unit & Toyota car DVD player

    ReplyDelete
  9. This In Car DVD Player really works to be entertained while on the road. Such a cool gadget to have!

    In dash DVD GPS player & Nissan GPS DVD player

    ReplyDelete
  10. Wow Nice post, i think its a very nice product. I really like this product and its features are also very good. Thanks for sharing with us.


    4ch DVR kit & Holden DVD player

    ReplyDelete
  11. Thank you for posting the great content…I was looking for something like this…I found it quiet interesting, hopefully you will keep posting such blogs….Keep sharing
    Window tinting West Vally City

    ReplyDelete
  12. with new information. I have found it extremely useful.

    soundboss car stereo player

    ReplyDelete
  13. Thanx, this is very Informational Post to Read now I am Waiting for your next post. Visit : Professional audio dvd

    ReplyDelete
  14. It is very useful information. Thank you. For more information related to this Click here

    ReplyDelete