Here I learnt how to make a reliable scan of the available WiFi network in the nearby.
In my real application, I put the ESP32 in WIFI_AP_STA mode and enable the hotspot:
WiFi.mode(WIFI_AP_STA);
WiFi.softAPConfig(apIP, apIP, netMsk);
WiFi.softAP(ssid, password);
WiFi.softAPsetHostname(HOSTNAME);
in this way the user can navigate to a web page served by AsyncWebServer where the available networks will be shown.
Every time I call WiFi.scanNetworks(true); the client is disconnected from the ESP32'AP:
Starting Network Scan...
....[1314334][W][AsyncTCP.cpp:950] _poll(): rx timeout 4
..[1316616][V][WiFiGeneric.cpp:383] _arduino_event_cb(): SCAN Done: ID: 166, Status: 0, Results: 12
[1316617][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 1 - SCAN_DONE
Found 12 networks
[1323366][V][WiFiGeneric.cpp:414] _arduino_event_cb(): AP Station Disconnected: MAC: 8c:b8:7e:10:39:7f, AID: 1
[1323368][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 13 - AP_STADISCONNECTED
[1330335][V][WiFiGeneric.cpp:407] _arduino_event_cb(): AP Station Connected: MAC: 8c:b8:7e:10:39:7f, AID: 1
[1330336][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 12 - AP_STACONNECTED
[1330421][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.4.2
[1330422][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
[1333679][W][AsyncTCP.cpp:930] _poll(): pcb is NULL
[1333680][W][AsyncTCP.cpp:930] _poll(): pcb is NULL
Starting Network Scan...
......[1346865][V][WiFiGeneric.cpp:383] _arduino_event_cb(): SCAN Done: ID: 167, Status: 0, Results: 14
[1346866][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 1 - SCAN_DONE
Found 14 networks
[1352230][V][WiFiGeneric.cpp:414] _arduino_event_cb(): AP Station Disconnected: MAC: 8c:b8:7e:10:39:7f, AID: 1
[1352231][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 13 - AP_STADISCONNECTED
[1357445][V][WiFiGeneric.cpp:407] _arduino_event_cb(): AP Station Connected: MAC: 8c:b8:7e:10:39:7f, AID: 1
[1357446][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 12 - AP_STACONNECTED
[1357517][V][WiFiGeneric.cpp:421] _arduino_event_cb(): AP Station IP Assigned:192.168.4.2
[1357517][D][WiFiGeneric.cpp:1039] _eventCallback(): Arduino Event: 14 - AP_STAIPASSIGNED
I also have a captive portal to automatically bring the user to the provisioning page when he connects to the AP and the behavior above leads to re-open the captive portal every time I run a scan.
I'm aware that the ESP32 must be in STA mode to perform a scan, but looking at the code of scanNetworks I see:
int16_t WiFiScanClass::scanNetworks(bool async, bool show_hidden, bool passive, uint32_t max_ms_per_chan, uint8_t channel, const char * ssid, const uint8_t * bssid)
{
if(WiFiGenericClass::getStatusBits() & WIFI_SCANNING_BIT) {
return WIFI_SCAN_RUNNING;
}
WiFiScanClass::_scanTimeout = max_ms_per_chan * 20;
WiFiScanClass::_scanAsync = async;
WiFi.enableSTA(true);
So it just enables the STA mode, it does not turns off the AP mode. And I set it to WIFI_AP_STA hence it should keep the AP mode on while scanning.
Bottom line: is the ESP32 capable to perform a WiFi scan while a client remains connected to its AP (and the AsyncWebServer still answers to the AsyncWebServerRequests) ?
If "yes": I need to fix my code...