Maker.io main logo

Configure WiFi Settings on a Headless Raspberry Pi and Connect Multiple Wireless Networks

2023-05-24 | By Maker.io Staff

Wireless Wifi Raspberry Pi

The Raspberry Pi 3 Model B introduced native WiFi support to the immensely popular line of single-board computers, which opened the doors to a whole new range of connected project ideas that we could tap into. Configuring multiple wireless networks is also possible, allowing the Pi to switch between networks when needed. However, setting up that functionality might not be evident to those who don’t have experience in configuring networks in Linux, so this article explains how to set up multiple WiFi configurations and assign them static IP addresses so that finding the Pi on every network becomes much more manageable.

Configure WiFi Settings on a Headless Raspberry Pi and Connect Multiple Wireless Networks

Setting up the First Wireless Connection

We need to set up a primary WiFi configuration before adding any additional WiFi connections. The simplest way to accomplish this on a headless setup is to utilize the Raspberry Pi imager’s built-in WiFi configuration options menu to enter the SSID and password of the primary wireless network. This configuration option also lets users enter the WiFi country code. This article explains all the necessary steps in more detail.

Configure WiFi Settings on a Headless Raspberry Pi and Connect Multiple Wireless Networks This screenshot shows the initial WiFi setup as generated by the Raspberry Pi imager tool when setting up a new SD card image.

Alternatively, you can also utilize the Pi’s desktop environment by accessing it through the built-in VNC server if you installed the full Raspbian image earlier. Finally, manually storing the SSID and passphrase in Linux’s networking configuration file (wpa_supplicant.conf) is also an option.

Adding Multiple WiFi Networks

Next comes the more tricky part. As shown above, the desktop UI doesn’t allow adding multiple wireless interfaces in any obvious way without disconnecting the currently active interface. You can, however, always add a new wireless network by selecting it from the list of available wireless networks and then entering the correct passphrase. However, doing so will result in the Pi immediately disconnecting itself from the currently active wireless network, which means accessing it wirelessly will not be possible until the other network is removed, which may not be possible or easy to do in a headless setup.

Configure WiFi Settings on a Headless Raspberry Pi and Connect Multiple Wireless Networks Selecting a second wireless network from the UI adds it to the list of known networks.

The last screenshot shows that the new network also appears in the wpa_supplicants configuration file. Therefore, we can use that file to define multiple wireless networks without making the Raspberry Pi disconnect itself from the currently active WiFi network. Typing the following command in a terminal allows accessing the file:

Copy Code
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Next, a new networking configuration can be added by supplying the SSID and password for each new network to add:

Copy Code
network={‎
‎    ssid="NETWORK_SSID"‎
‎    psk="NETWORK_PASSPHRASE"‎‎}‎ 

Once done, save the file using Ctrl+O and then exit the editor using Ctrl+X. It may then be necessary to reconfigure the wireless networking interface by typing:

Copy Code
wpa_cli -i wlan0 reconfigure 

Doing so will result in the Pi disconnecting itself from the active wireless network for a few seconds. However, it should re-establish the connection after a short while and display the following success message:

Configure WiFi Settings on a Headless Raspberry Pi and Connect Multiple Wireless Networks After a few seconds, the networking interface should come back online.

Setting Static IP Addresses for Multiple Wireless Interfaces

Using static IP addresses is typical in headless setups since doing so makes finding the Raspberry Pi on the network easier (as the computer will always have the same IP address). Further, this approach also allows for a more straightforward configuration of other devices that want to communicate with the headless SBC (single-board computer).

Before setting any IP addresses, you must know every network's router IP address and DNS server addresses. Another recent article discusses how to find these values in more detail, so this article will only outline the steps. Therefore, start by writing down the necessary values for all networks. In my case, those are the following:

Copy Code
IP Router 1: 192.168.1.1‎
IP Router 2: 192.168.0.1‎
DNS Network 1: 192.168.1.1 and 192.168.1.230‎
DNS Network 2: 8.8.8.8‎ 

Finally, choose a single static IP address for every network according to that network’s subnet mask, which will be 255.255.255.0 in most cases, meaning that you can freely assign the last number of the IP address (excluding 1 and 255). So, in my case, I decided to give the Pi the following IP addresses:

Copy Code
Raspberry IP Network 1: 192.168.1.108/24‎
Raspberry IP Network 2: 192.168.0.108/24‎ 

The /24 indicates the network’s subnet mask, which is 24-bit in the case of 255.255.255.0. Note that the Pi will try to get this IP address from the router. If the address is already in use, the router (or DHCP server) will assign the Pi another valid address in the network.

Either way, edit the following file once you have obtained these values:

Copy Code
sudo nano /etc/dhcpcd.conf 

Then, fill in the following template using the previously obtained values and append the result to the dhcpcd.conf file:

Copy Code
interface <Network> ‎
arping <IP_Router_1> ‎
arping <IP_Router_2>‎
arping <IP_Router_x>‎

profile <IP_Router_1>‎
static ip_address=<Static_IP_1>/<Subnet_Mask_1>‎
static routers=<IP_Router_1>‎
static domain_name_servers=<Name_Server_1>‎

profile <IP_Router_2>‎
static ip_address=<Static_IP_2>/<Subnet_Mask_2>‎
static routers=<IP_Router_2>‎
static domain_name_servers=<Name_Server_2>‎

profile <IP_Router_x>‎
static ip_address=<Static_IP_x>/<Subnet_Mask_x>‎
static routers=<IP_Router_x>‎
static domain_name_servers=<Name_Server_x>‎ 

You can add as many profiles as needed. In my case, I only want to configure two static IPs for two networks. Therefore, my dhcpcd.conf file looks as follows:

Configure WiFi Settings on a Headless Raspberry Pi and Connect Multiple Wireless Networks This screenshot shows the new static configuration lines added below the green cursor

Again, use Ctrl+O to save the changes and Ctrl+X to exit nano. Finally, reboot the Raspberry Pi using the following command:

Copy Code
sudo reboot now 

Upon the next boot, the Pi will automatically connect to the first WiFi network it finds and then use the new static IP address defined in the dhcpcd.conf file.

Summary

Adding multiple WiFi networks in a headless setup without disconnecting the Pi from the currently active network might be tricky. However, adding multiple wireless networks upfront is vital in a headless configuration, as there might be no way to configure a new network without a keyboard and a display.

Luckily, we can add wireless networks to the wpa_supplicant.conf file before moving the Pi between locations, and they can even assign each configured WiFi network a unique static IP address so they can always find the Pi in each network.

TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum