Skip to content

WiFi scanNetworks may not find all APs #10281

Closed
@TD-er

Description

@TD-er

Board

ESP32-C3/C6

Device Description

Hardware Configuration

Version

latest master (checkout manually)

IDE Name

PlatformIO

Operating System

Windows 11

Flash frequency

Any

PSRAM enabled

yes

Upload speed

115200

Description

The example WiFi scan code may not show all available APs.
Usually only a very small subset.

In ESP-IDF5.3 the struct wifi_scan_config_t does have a few new members:

    uint8_t home_chan_dwell_time;                      /**< time spent at home channel between scanning consecutive channels. */
    wifi_scan_channel_bitmap_t channel_bitmap;         /**< Channel bitmap for setting specific channels to be scanned. For 2.4ghz channels set ghz_2_channels from BIT(1) to BIT(14) from LSB to MSB order to indicate channels to be scanned. Currently scanning in 5ghz channels is not supported. Please note that the 'channel' parameter above needs to be set to 0 to allow scanning by bitmap. */

link

According to the comments and docs it looks like whenever these members are set to 0, some predefined default is used. See: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_wifi.html#_CPPv428esp_wifi_set_scan_parametersPK26wifi_scan_default_params_t
However for the channel bitmap no default is mentioned, so I guess we should just set those bits ourselves.

Suggested code change for WiFiScanClass::scanNetworks

  if (channel == 0) {
    config.channel_bitmap.ghz_2_channels = 0xFFFF;
  } else {
    config.channel_bitmap.ghz_2_channels = BIT(channel);
  }
  config.home_chan_dwell_time = 30; // The home_chan_dwell_time needs to be a minimum of 30ms and a maximum of 150ms.

N.B. The esp-idf example code does do a calloc to sneakily set all to 0 and thus hiding the fact they do not properly initialize their members.
See: https://github.com/espressif/esp-idf/blob/6673376297b921d438790d195014b860eaf8bb17/examples/wifi/scan/main/scan.c#L174-L182

It is yet unclear how the bitmask for 5GHz will be interpreted as there is not yet any function to set those flags given an array of channel numbers.
So for the ESP32-C5 this needs to be fixed when this chip arrives.

Sketch

https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WiFiScanAsync/WiFiScanAsync.ino

May not fail on each build, but when `async` is set to false, it may fail on an ESP32-C3.
Since the bitmask is not initialized, it may show up some of the used channels and thus masking the bug.

Debug Message

-

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions