Raspberry Pi Wireless Networking for Multiple Networks

I’ve recently started trying to create something useful with my Raspberry Pi. My first challenge has been trying to find some way to easily access the Pi without being dependent on access to an HDMI display, ethernet cable and keyboard. I also did not want to be tied to a physical location or network.

For this project, I am using a Raspberry Pi model B, revision 1, running Raspbian.

Since the Raspberry Pi does not include wifi capabilities out of the box, I purchased a USB wifi dongle (http://www.adafruit.com/product/814) for wifi functionality.

To setup the Raspberry Pi for wireless, I connected the wifi dongle to one USB port and my keyboard to the other. I used an HDMI cable to connect the Pi to my TV.

Now, I wanted to setup my Pi to connect to either my phone or my home wireless access point, depending on what is available. I ended up using WPA supplicant to achieve the desired functionality.

Start by opening the wpa_supplicant.conf file in your favorite text editor. For this example, I will use nano.

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf


This file had some text in it already, which I left untouched. Below that, add:



Brief explanation:
The wpa_supplicant.conf file provides configuration information to the WPA supplicant. Each section provides information for how to connect to a specific network.

ssid: This is name of one of the networks you want connect to.
psk: This is your network password.
id_str: This is the id you will use to refer to the network interface in the next section.

The rest of the options worked correctly for my network setup, but these may change based on your network configuration.

Once you have edited and saved your wpa_supplicant.conf file, you can go about editing the /etc/network/interfaces file. Again, open this in your favorite text editor.

sudo nano /etc/network/interfaces


Some text was already in here, but I modified the file to look like:

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0

iface wlan0 inet dhcp

pre-up wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B
post-down killall -q wpa_supplicant
post-down rm /var/run/wpa_supplicant/wlan0

iface home inet static
address [ip address for home network]
netmask [mask for home network]
gateway [gateway address for home network]

iface phone inet static
address [ip address for phone network]
netmask [mask for phone network]
gateway [gateway address for phone network]

Brief Explanation:
The /etc/network/interfaces file specifies information about the network interfaces your pi can connect to.

Make sure that the line below includes the correct path to your wpa_supplicant.conf file.

pre-up wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B


The post-down commands are used to reload the wpa_supplicant information if you restart the network service.

Once you have edited and saved /etc/network/interfaces, you need to restart the network service for the networking configuration to take effect.

One way to do this is:

sudo service networking restart


If that fails for some reason, rebooting the Pi may work.

sudo reboot


Now, the Pi should be able to connect to either the phone or home network on the Pi, if they are available. This allows you to use SSH to connect to the Pi from any computer that’s on the same network.

ssh pi@ip-address-for-network

For more information about using SSH to connect to a Pi, visit: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh

If for some reason, you do not have access to a computer to SSH into the Pi when you need to, you can also use a smart phone. I am using an SSH client app called VX ConnectBot for this. Simply connect your Raspberry Pi to your mobile hotspot and then use VXConnectBot to SSH to its assigned IP address.

Update: I had some difficulty finding out how to operate VX ConnectBot for tasks such as typing Ctrl-X in nano. Instead, I’m using JuiceSSH, which works pretty well and offers a popup keyboard for harder to find keys.

Now, I can connect to my Raspberry Pi from any computer or phone on the networks it has been configured to use. Hopefully this greatly reduces the amount of time I spend hunting for an HDMI cable and keyboard.

Future Work

There are still many ways to improve this process. Some things I still want to work on:

  • Generate and store a WPA passphrase instead of storing the network password in plain text
  • Find out if there is a better way to restart the wpa_supplicant configuration when restarting the network service. This throws some warnings as it is now.