Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Connect To Multiple Wireless Networks With A Raspberry Pi

TwitterFacebookRedditLinkedInHacker News

So I recently picked up some more Raspberry Pi 3 units. You’re probably thinking I’m building my own T1000 (Terminator reference) with these things. No, actually I got these units for work to play around with some IoT projects around the office at Couchbase. I have a few offices that I visit at Couchbase which brought up my need to have multiple possible wireless networks to connect to as I travel. These are headless units so I’m not connecting to them with a mouse and keyboard.

Now one would imagine that I can just add a bunch of networks to a configuration file in Linux, but that wasn’t the case. There are special configurations that must be done to allow the Raspberry Pi to automatically connect to different wireless networks as they become available.

We’re going to see how to add multiple WiFi network SSIDs to a Raspberry Pi operating with Raspbian so it can automatically connect as they become in range.

My Raspberry Pis are running the lite version of Raspbian. They are being used as headless units which I discussed in one of my previous articles. This means they will never be hooked up to a monitor and never be hooked up to a mouse and keyboard.

Raspbian uses the /etc/wpa_supplicant/wpa_supplicant.conf to manage wireless networks. For example a typical file with a single wireless network might look like the following:

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="network_id_here"
    psk="wpa_password_here"
}

You can add as many networks as you want here, but some extra steps must be taken. For example, connections don’t happen based on the order they appear in this file. You have to name the connections first. Doing this will result in something that looks like the following:

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="network_one_here"
    psk="wpa_password"
    id_str="home"
}

network={
    ssid="network_two_here"
    psk="wpa_password"
    id_str="work"
}

Notice my use of the id_str property?

Simply naming the connections is not enough. We need to make some edits to another file. Some changes need to be made to the /etc/network/interfaces file. Take mine for example:

auto lo
iface lo inet loopback

iface eth0 inet manual

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

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

iface home inet dhcp
iface work inet dhcp

So what did I change versus the original version of this file? Well for starters I add the following lines:

iface home inet dhcp
iface work inet dhcp

The names home and work match those found in the /etc/wpa_supplicant/wpa_supplicant.conf file that we altered earlier. To consider a wireless network for connecting to, it must exist as a new line in this /etc/network/interfaces file.

This is where things get a little strange and where I had the most trouble. Just adding a bunch of new iface lines didn’t work for me. All the tutorials I found on the internet only pointed to those lines. What you actually have to do is change the iface wlan0 and iface wlan1 lines to use the following:

wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

Notice in particular the wpa-roam part. By default these are listed as wpa-conf which would not work.

Restart your Raspberry Pi or network interface for the changes to be picked up and you should be able to connect to whatever is available in your list.

Conclusion

Raspberry Pis are great portable computers. Being so small, they make you want to travel around with them. However, as you travel you’re going to want to connect to different networks. While adding multiple networks to the list of preferred networks is not difficult, there are a few configurations that must be made in order for this to be possible.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in C#, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Unity. Nic writes about his development experiences related to making web and mobile development easier to understand.