Control Raspberry Pi via USB connected Android device

Vitali V. picture Vitali V. · Mar 2, 2014 · Viewed 25.6k times · Source

I have a Raspberry Pi with running custom server not connected to the internet (offline system).

I would like to connect Android device via USB cable to rPi and communicate with it.

As Raspberry Pi is not connected to the internet, it doesn't have IP address (given via DHCP).

How can I communicate with rPi using Android device connected via USB?

Maybe local DHCP server?

Thanks

Answer

AL̲̳I picture AL̲̳I · Apr 1, 2014

Follow this link. You just have to configure the RPi for it once and you are good to go.

What this does:

  1. Allows you to use your Android device as keyboard input and terminal display for your Raspberry Pi

  2. Allows you to tether your Raspberry PI to your Android device's WiFi (3G untested, but should work).

Background:

I love the idea of a truly portable Raspberry Pi box. I wanted to make one, but I don't have a compatible WiFi dongle, nor do I have a wireless keyboard and mouse. Even if I did all of these things, I'd like to save myself the extra USB port by having both my network and input go over the same port. I guess the next thing to do after this would be to set up some sort of VNC server and connect to the Pi's GUI via the Droid.

My Build:

  • Raspberry Pi v2 (Linux raspberrypi 3.1.9+ #125 PREEMPT)
  • Asus Transformer Prime (Android 4.1.1. Jelly Bean)
  • Yoobao "Long March" -11200mA 1A / 2A battery (good for 10+ hours!)
  • VX ConnectBot terminal emulator (Free in the Google Play store)
  • Hackers Keyboard - because I like tab complete (Free in the Google Play store)

Assumptions:

  • Your PI is configured to run SSHd on startup
  • You are using the default Pi username / password
  • Your Android device has internet connectivity via WiFi/3G and it is enabled

How to do it:

  1. Add the following to /etc/network/interfaces

    iface usb0 inet static
    address 192.168.42.42
    netmask 255.255.255.0
    network 192.168.42.0
    broadcast 192.168.42.255
    
  2. Power down your Pi

  3. Connect your Android device to your Pi via it's USB data cable

  4. Power up your Pi... wait about a minute.

  5. On your Android device, go to Settings / Wireless & Networks / More / Tethering & Portable Hotspot... click "USB tethering"

  6. Start up VX ConnectBot and SSH "[email protected]"

  7. Keep hitting return until it asks you for your password. Enter it.

    Congratulations - you should now have terminal access to your Pi with a software keyboard... kinda. Mine seems to disconnect the session every minute or so - very annoying.

    Now to set up routing so that your Pi can see the world via your Android device.

  8. Root yourself up

    sudo su -
    
  9. Check to see which IP address that your Android device is using

    arp -a
    

    Mine is 192.168.42.129

  10. Add a default route pointing all traffic from your Pi out to your Android device. It's worth noting that this static route is temporary and will not survive rebooting your Pi. I've left it like this because I don't know if Android likes to change it's tether gateway address very often. If you've gotten this far, I'll assume you can figure out how to make a static route permanent if need be :)

    route add default gw 192.168.42.129 usb0
    
  11. Test it!

    ping http://www.google.com