Use a Raspberry Pi with multiple WiFi networks

I have recently been carrying my Raspberry Pi back and forth between my home and my local hack space - BuildBrighton.

Both places have WiFi and with a Raspberry Pi A+ missing an ethernet port, you need to ensure you can access the Pio to do any sort of development (and I can't always guarantee a HDMI compatible display will be available).

It's fairly easy and isn't restricted to just two - you can add as many WiFi networks as you wish. All you need to know is the SSID and password.

With access to the Pi, edit the following file with sudo access:

$ sudo nano /etc/network/interfaces

It should look something like this:

auto lo
iface lo inet loopback
auto eth0
allow-hotplug eth0
iface eth0 inet dhcp
auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface location1 inet dhcp
iface location2 inet dhcp

If your internet is working, there is no need to tweak too much. The most important lines are the last few

wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface location1 inet dhcp
iface location2 inet dhcp

If your WiFi credentials are in the this file, save them somewhere and delete them, replacing with the above.

For every location, add the iface line with a unique identifier in the example above i've used location1 and location2. This can be anything (keep it lowercase and one word for consistency).

Now edit the wpa_supplicant.conf file - make sure you have your WiFi details to hand for this bit!

Edit the file:

$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

It should already have the first couple of lines, but then after add a network array for each location

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    ssid="WIFI SSID"
    ssid="WIFI SSID"

Replace the WIFI SSID and WIFI PASSWORD with the correct credentials (leaving the " in place).

The id_str needs to match exactly the keyword you used in the interfaces file.

Save the file and reboot the Pi. You should now be connected to your current network and when you head to your second location, it should connect there as well.

If you have any problems, don't hesitate to drop me a tweet.

Blog Posts in the Series

View this post on Github

You might also enjoy…

Mike Street

Written by Mike Street

Mike is a CTO and Lead Developer from Brighton, UK. He spends his time writing, cycling and coding. You can find Mike on Mastodon.