Introduction: Headless Raspberry PI WiFi Setup (for Mobiles)

So you successfully confiured your RaspberryPi in a fully headless fashion, maybe with WiFi too, and you are very happy using it at home in your robotic projects...

But when you go outside, at work or on any foreign network, once powered and connected to some ethernet cable you do always... get it lost on the net :-(, you have no way to know the ip address. Everything works fine at home, where you have access to the router admin, but when you connect it to the ethernet at work or in other complex enterprise nets there is no way to connect to the RPI.

You will find some ready made solutions for the same ip-address problem, from blinking it on the power led to write it on some usb memory after startup, etc, just google for them if you like.

I will suggest here some ways to get connected to your Raspberry with just the help of your smartphone.

What you will need for this instructable is:

  • the already configured RPI
  • a "managed" ethernet/wi-fi router, ie one you can access to get leased ip-addresses
  • internet access to get/update software and search a solution for ongoing problems

For this instructable I will assume a minimal unix understanding, if you have arleady configured an headless RPI you should at least know how to connect to it through a remote console using ssh/rlogin.

I will use vi to edit rpi text files, if you don't feel confortable with vi you can find an editor of your choice, just switch from vi to whatever else in command lines. One widely used editor is mono, if it isn't already installed on your system type:

sudo apt-get install mono

Step 1: Wi-fi Config for Your Home Router

Assuming you already have a working wifi adapter you will work through the ethernet connection to configure the wi-fi connection, don't disconnect the ethernet cable till you are sure the wireless config do work as needed.

Configuring the RPI to connect you home WiFi is not required for the purposes of this instructable but it can be handy for you robotoc projects. By the way you could have already configured it, in this case compare you config with the one described here and decide if you want to make some change.

Start editing the main network config:

sudo vi /etc/network/interfaces

You must ADD the following lines, the config already contains some line that work for the ethernet connection don't change them:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

The main trick is in the 'wpa-roam' line, it tells the system that it can handle more than a single wi-fi sid, otherway we could use 'wpa-config' as suggested in most help sites and use a single sid config. In either case the wpa line delegates wpa config to a second config file: wpa_supplicant.conf. When using wpa-roam the ip address is always 'manual', see the first iface line, this doesn't means the ip is static, it means it's defined in the next lines. The first line after the wpa-roam gives a 'default' config, the most frequent one, with dhcp leased ip-address.

Now execute the following command where you will use your home router sid name, you can type your readable passphrase as second param or type it as input after execution:

wpa_passphrase

The output will be as follows, a network config with the sid name, the passphrase as remainder, and the encoded passphrase. You can remove the plain passphrase for privacy reasons.

network={
        ssid="?????"
        #psk="!!!!!!" (remainder, remove for privacy)
        psk=........
}

Edit /etc/wpa_supplicant/wpa_supplicant.conf and add the new config

sudo vi /etc/wpa_supplicant/wpa_supplicant.conf

If you want you can add the following standard config to access any non secured network:

network={
        key_mgmt=NONE
        priority=1
}

You should now have a wpa_supplicant.conf like:

network={
        key_mgmt=NONE
        priority=1
}
network={
        ssid="your ssid"
        psk=........
}

Now you need to restart the networking service to load new config

sudo service networking restart

The service takes some time to restart, it freezes for some time but you shouldn't break the connection. After the service restarts we can check the connection in two step, iwconfig should now display info about the adapter interface, connected sid, signal level, etc.

iwconfig<br>wlan0     Ralink STA  ESSID:"gtjunior"  Nickname:"xxxxxxx"
          Mode:Managed  Frequency=2.437 GHz  Access Point: xx:xx:xx:xx:xx:xx
          Bit Rate=65 Mb/s
          RTS thr:off   Fragment thr:off
          Encryption key:xxxxxxxxx [2]   Security mode:open
          Link Quality=100/100  Signal level:-44 dBm  Noise level:-44 dBm
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0

ifconfig should display the assigned ip-address

ifconfig<br>wlan0     Link encap:Ethernet  HWaddr 00:36:76:08:97:3e  
          inet addr:192.168.1.18  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::236:76ff:fe08:973e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:45729 errors:0 dropped:0 overruns:0 frame:0
          TX packets:676 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:10845192 (10.3 MiB)  TX bytes:70916 (69.2 KiB)

If all seems ok you can try to stop, remove the ethernet cable and then power again the rpi, you should now connect to your rpi in wireless mode, just wait some time to let the router refresh the dynamic DNS.

Step 2: WiFi Config for Your Smartphone WiFi Tethering

Using wpa-roam you can have you RPI configured for more wifi networks at yhe same time, the system will find the preferred one based on some priority rules.

Start by enabling WiFi tethering on your smartphone, I suggest you to setup a password protected WPA network, the supplicant gives lower priority to unsecured networks so you could never connect to your smartphone while the home network is on.

So you know the SSID and the password to connect to your smartphone tethering, you can then calculate the network config for it using the same command you used in the previous step:

wpa_passphrase

Add the output to /etc/wpa_supplicant/wpa_supplicant.conf, copy it before your home network config, the system will give priority to the first secured network available.

When you are done reboot the system to use the new config:

sudo reboot

After reboot the system should be connected to the phone wifi tether, check this by issuing both ifconfig and iwconfig as before.

Step 3: Find Your RPI Ip Address Using the Smartphone

Now you configured and checked all previous config and you want to go around with your new RPI and connect to it using your smartphone.

To complete this step you need to install two more app to your smartphone, i will suggest here some Android ones, you need to find your preferred if you use other smartphone systems:

  • a terminal emulator, Terminal Emulator for Android is the most used one;
  • an ssh client, I'm using JuiceSSH but there is a lot of choice.

Enable the phone's wifi tethering as before, of course you must ensure that both the SSID and the password has not been changed in the meanwhile, then power on the RPI.

When the RPI connects to the wifi tethering the android system, and linux systems in general I think, logs the leased ip address in a file at the path /proc/net/arp, so issue the cat command in terminal shell on the phone as in the first image, you should magically find the leased ip-address.

Now that you know the tethering ip address you have a lot of power over your RPI connection, you can:

  1. work on the RPI using just the ssh client on the same smartphone;
  2. connect the RPI to some ethernet network and use the smartphone ssh client to know the new ip-address on the wired network using ifconfig;
  3. use the ssh client to configure some other public WiFi access, then restart and continue working using that