I'm trying to programmatically change which wifi network my PI is connected to, on the fly without a reboot. When I run the script using this USB wifi device it won't switch. However, it works when when using the built-in wifi card on the PI.
The external USB wifi is wlan0, and I'm forcing the onboard wifi to be wlan1 so they will always be consistent at boot-time (see this link)
cat > /etc/udev/rules.d/72-static-name.rules <<EOF ACTION=="add", SUBSYSTEM=="net", DRIVERS=="brcmfmac", NAME="wlan2" EOF
Here's the script. Note that for the onboard wifi, everything is the same except use
wlan1 instead of
#set primary network settings wpa_cli -p /var/run/wpa_supplicant -i wlan0 add_network wpa_cli -p /var/run/wpa_supplicant -i wlan0 set_network 0 ssid '"PrimaryAccessPoint"' wpa_cli -p /var/run/wpa_supplicant -i wlan0 set_network 0 scan_ssid 1 wpa_cli -p /var/run/wpa_supplicant -i wlan0 set_network 0 key_mgmt WPA-PSK wpa_cli -p /var/run/wpa_supplicant -i wlan0 set_network 0 psk '"password"' wpa_cli -p /var/run/wpa_supplicant -i wlan0 enable_network 0 #set secondary network settings wpa_cli -p /var/run/wpa_supplicant -iwlan0 add_network wpa_cli -p /var/run/wpa_supplicant -iwlan0 set_network 1 ssid '"SecondaryAccessPoint"' wpa_cli -p /var/run/wpa_supplicant -iwlan0 set_network 1 scan_ssid 1 wpa_cli -p /var/run/wpa_supplicant -iwlan0 set_network 1 key_mgmt WPA-PSK wpa_cli -p /var/run/wpa_supplicant -iwlan0 set_network 1 psk '"password"' wpa_cli -p /var/run/wpa_supplicant -iwlan0 enable_network 1
After running this script, network 1 (SecondaryAccessPoint) is the active network. When I want to to use network 0 (PrimaryAccessPoint) I run:
wpa_cli -p /var/run/wpa_supplicant -i wlan0 enable_network 0
When using wlan1, this works, and within 15 seconds when I run
ifconfig I see the new ip address from PirmaryAccessPoint instead of from SecondaryAccessPoint.
However, for wlan0, it just does nothing. To debug, I did the following:
$ wpa_cli Selected interface 'wlan0' Interactive mode > select_network 0 OK <3>CTRL-EVENT-SSID-REENABLED id=0 ssid="PrimaryAccessPoint" <3>CTRL-EVENT-SCAN-STARTED <3>CTRL-EVENT-SCAN-RESULTS <3>WPS-AP-AVAILABLE <3>Trying to associate with 80:37:73:cb:b0:fa (SSID='PrimaryAccessPoint' freq=2437 MHz) <3>Association request to the driver failed >
I've tried running
sudo systemctl restart dhcpcd according to this post but that didn't solve anything either. The only way I can get this to work is to reboot, and run the script for the access point I want first.
Association request to the driver failed mean, and how can I fix it? Why does it work on first connect, but fails afterwords? Is there a way around this?
Just for clarity's sake, I'll explain a little bit more of my objective here. Here is a link to my blog where I outline what I'm trying to do. In a nutshell, I'm using the onboard wifi to create an access point, and I'm using TWO external wifi cards. One external card (call it external1) will constantly be testing which open wifi access points are strongest (and fastest using speed test), and the other external card (call it external2) will connect to the "best" open network that external1 found and share that internet connection with the onboard card. Whenever external1 finds a better connection, it will reconfigure external2 to use that better connection.
Device version: RaspberryPI 3B+
Kernel version: 4.14.69-v7+ #1141 SMP armv7l GNU/Linux