3

I am trying to use a RPI Zero W2 to solve a problem I have with collecting files from an IoT device. Specifically, I don't know how to setup the Wi-Fi network(s). Any inputs to my questions would be appreciated.

Existing Setup:

  1. IoT device acting as a web server and hosting a Wi-Fi access point (say 10.10.x.x subnet).
  2. Wi-Fi access point providing internet access (say 192.168.x.x subnet).

My Requirement / Need a RPI Zero W2 to do the below:

  1. Connect to the IoT device (1) and download some log files using a REST API. (could be a few GB)
  2. Connect to the Wi-Fi access point (2) and upload the log files to some cloud storage.

Note 1: I don’t need to provide internet access to the IoT device. It is acting as a web server only and does not generate outgoing traffic.

Note 2: I can take care of the code to do the download and upload mentioned in point 1. & 2. Need guidance on how to setup the networks at this point.

Questions:

  1. How do I connect to two Wi-Fi access points simultaneously? I was planning to add a second Wi-Fi NIC using a USB dongle & OTG cable. Alternatively can these connections be made simultaneously on the single built-in Wi-Fi NIC?

  2. Would it be better to alternate connecting to the two access points? For example connect to the IoT device Wi-Fi AP, download files, disconnect from that AP, connect to the internet Wi-Fi AP, upload to internet, disconnect from the internet Wi-Fi AP, and then repeat.

Fred
  • 4,552
  • 18
  • 29
Sachin
  • 31
  • 1
  • 3
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Apr 09 '22 at 15:44

2 Answers2

2

Interesting questions.

N.B.: I can't replicate your network setup. Consequently, the following answers have not been fully verified. I feel the alternate/sequential connections approach will work for you assuming both APs (IoT & WiFi) are on functional networks.

Summary:

A "simultaneous" solution is possible only with hardware & drivers that support it. AFAIK, there is no RPi hardware that does.

An "alternate/sequential" solution is possible, and illustrated below. This solution employs the wpa_cli app (part of wpa_supplicant) to rotate AP priorities using set network <id> priority <n> followed by a reassociate to switch to the higher priority network id/AP. This rather obtuse approach is necessary to avoid the select_network option that disables the other networks.

WRT Q1: connect to 2 Wi-Fi access points simultaneously ?

As I understand it, this requires both an interface (hardware) and a driver that supports dual-channel management. You should run the iw list command on your Pi 0 W2 to determine if your system currently supports "simultaneous".

For my RPi (a 3B+ running bullseye), I get the result below where <=1 effectively means that the "simultaneous" option is not available for my 3B+. If you get <=1 result, then "simultaneous" is likely not available for you either. This may guide your selection of a suitable USB NIC if you want "simultaneous".

$ iw list | grep -A 4 'valid interface combinations'
    valid interface combinations:
         * #{ managed } <= 1, #{ P2P-device } <= 1, #{ P2P-client, P2P-GO } <= 1,
           total <= 3, #channels <= 2
         * #{ managed } <= 1, #{ AP } <= 1, #{ P2P-client } <= 1, #{ P2P-device } <= 1,
           total <= 4, #channels <= 1

WRT Q2: alternate/sequential wifi connections

I believe this will do what you need using the single wlan0 interface built into your Pi 0 2W. You may wish to do some reading (see REFERENCES below) before you begin, and get some background on this process. Based on my reading of this & other research, and under the assumptions stated above, this option will work. The procedure is outlined below in two steps:

  1. Declare both networks in /etc/wpa_supplicant/wpa_supplicant.conf
    You didn't show your wpa_supplicant.conf file, so I've created this one to serve as an example:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

country=GB

network={ ssid="MyWiFiAP" psk="mypasswd" }

network={ ssid="MyIoTAP" psk="mypasswd2" }

  1. We use wpa_cli in its interactive mode below, but all of these commands may be written as stand-alone commands, suitable for scripting:
$ wpa_cli

... # preliminaries...

> list_networks network id / ssid / bssid / flags 0 MyWiFiAP any [CURRENT] 1 MyIoTAP any

The above result shows the two SSIDs configured in wpa_supplicant.conf

> get_network 1 priority 0 > get_network 0 priority 0

The above results show the priority of the two SSID/network ids (0 & 1)

> set_network 1 priority 2 OK

Assign a higher priority to network id 1 (MyIoTAP)

> reassociate OK
... a series of CTRL_EVENTS are listed ...

Connect to the higher priority network (MyIoTAP)

> list_networks network id / ssid / bssid / flags 0 MyWiFiAP any 1 MyIoTAP any [CURRENT]

after reassociate, connection has moved to higher priority network/SSID

to restore the original connection to MyWiFiAP:

use the set_network priority commands above,

follow that with another reassociate

Note: when priorities of all networks are equal,

wpa_supplicant defaults to the one with the strongest signal

> quit

terminates the wpa_cli interactive session

$

And that's it... the alternate/sequential wifi connections process. You should verify this works with your network configuration manually as I've shown above. Once verified, you may use the equivalent stand-alone commands to automate the process in a script - or experiment with wpa_cli running in daemon mode.


REFERENCES:

  1. RE: wlan0 vs. p2p-dev-wlan0
  2. ArchWiki wpa_supplicant documentation
  3. man wpa_cli on your RPi
  4. Linux Find Wireless WiFi Driver Chipset Information
Seamus
  • 21,900
  • 3
  • 33
  • 70
  • unfortunately my RPI zero w2 returns <=1. so will have to go with the alternating connections to each wifi AP. – Sachin Apr 14 '22 at 05:16
  • @Sachin: "my RPI zero w2 returns <=1" - not surprising considering RPi priority is on *low cost. On the bright side, administration of such a beast might be x-painful? I just skimmed through this recent, interesting post on Vivek Gite's Linux blog; if dual simultaneous is what you want, there may* be some information there to help you find it. But in any case, please do let us know how this works for you. – Seamus Apr 24 '22 at 21:56
0

I am now able to switch between Wi-Fi networks with the following commands:

wpa_cli -i wlan0 set_network 0 priority 2
wpa_cli -i wlan0 set_network 1 priority 0
wpa_cli -i wlan0 reassociate

and able to switch back to the first Wi-Fi with:

wpa_cli -i wlan0 set_network 0 priority 0
wpa_cli -i wlan0 set_network 1 priority 2
wpa_cli -i wlan0 reassociate

If I didn't use the -i switch it would default to p2p-dev-wlan0 instead of wlan0.

Reference: Switching to a different wireless network when it is available

Fred
  • 4,552
  • 18
  • 29
Sachin
  • 31
  • 1
  • 3
  • Add the following on its own line (I put it at the top so its easy to find again) to disable the p2p-dev-wlan0 interface: p2p_disabled=1 in the file /etc/wpa_supplicant/wpa_supplicant.conf. I rebooted, but a daemon-reload or some such may also work. With the p2p interface disabled, wpa_cli defaults to the wlan0 interface. – Chris Oct 11 '22 at 21:21