Can ESP32 execute a scan while a client is connected to its AP?

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...

My guess (never tried) is that it can do a scan and maintain the AP live but you should expect disruption for AsyncWebServerRequest as I think both activities would run on the same core

1 Like

scan goes thru all channels so it will turn of the SoftAP and disconnect the stations from it.

2 Likes

From a user-experience point of view, what you would suggest then?

I mean, when the user opens the provisioning page he would expect periodic scans to update the available networks. But since this leads to a disconnection, how to make a usable UI?

you can't update the visible networks. scan them before starting the SoftAP.
btw: why not use the WiFiManager library?

1 Like

Most IoT devices allow a refresh of the available networks while provisioning. I'm trying to mimic that behavior.

I tried the WiFiManager library in the past, but now I want to use my own code to better understand how it works under the hood and provide a custom page for provisioning (along with the credentials I need to set few others options).

ESP32 has only one radio which can run only at one channel at time.
you could try if the connected station can stay connected with AP lost for a moment.

1 Like

you can dot that with WiFiManager

but that's a good reason :slight_smile:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.