I have a Raspberry Pi 3B running Raspbian Lite Stretch (2018-03-13).
I have an IoT device that broadcasts an AP. I want my RPi to join this AP whenever it is available (i.e., the IoT device is on). But when the AP is not available, I want it to fall back to connecting to my home infrastructure network.
I've configured the networks in the wpa_supplicant config, and assigned a priority of 1000 to the IoT network; if I turn the Raspberry Pi on while the IoT network is broadcasting, it correctly connects to it. When the IoT device goes off, the Raspberry Pi switches to the home network.
However, when the IoT device turns on after the RPi, the RPi does not automatically leave the home network and associate to the IoT device's network.
I'm OK with having to kick off the switch through a cron job or on some other trigger.
Doesn't work: wpa_cli select_network
The first technique I've tried is simply using wpa_cli select_network
to switch manually. After running it, list_networks
output changes to have this [DISABLED]
/[CURRENT]
indicator. However, it does not actually change networks.
Selected interface 'p2p-dev-wlan0'
network id / ssid / bssid / flags
0 HomeNetwork any [DISABLED]
1 IoTDevice any [CURRENT]
I've also tried using wpa_cli reconnect
and wpa_cli reassociate
but these don't do anything. wpa_cli disconnect
also does nothing, and wpa_cli disassociate [BSSID]
just reports failure.
Doesn't work: iwconfig
$ sudo iwconfig wlan0 essid "IoTDevice"
Error for wireless request "Set ESSID" (8B1A) :
SET failed on device wlan0 ; Operation already in progress.
Doesn't work: iw
$ sudo iw wlan0 connect IoTDevice
command failed: Operation already in progress (-114)
Not supported anymore, presumably: wpa-roam
Many guides suggest putting the device into wpa-roam
mode, but the information on how to do so is out of date; all the guides reference /etc/network/interfaces
, which seems to be virtually blank since the switch to dhcpcd
.
Does work, but sucks: Nuking dhcpcd
Other people also express frustration with difficulty restarting wpa_supplicant
short of blowing away dhcpcd
each time. But when I do this, it does switch to the higher priority network if available.
I've written an answer that periodically scans for the desired network and kills dhcpcd
when it's found; this works, but it is a very heavy-handed solution.
Since I can change networks just fine when the associated AP goes down without killing any processes, I shouldn't need to do this.
I'm trying to avoid cargo cult answers that involve flipping interfaces on and off, killing processes and anointing the board with sacred oils. How is switching networks really supposed to work?
dhcpcd
includes the commands to killdhcpcd
but I have also added it to my post.nmcli
- It's what worked for me to manually switch between available networks on an Ubuntu system. (wpa_cli
was only showing one inlist_networks
, even after ascan
producedscan_results
showing various others -- so I couldn't figure out how to switch withwpa_cli
, butnmcli con up <networkname>
worked for me.)