espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
12.57k stars 7.02k forks source link

Wi-Fi Scanning Fails (IDFGH-12671) #13663

Open michaelboeding opened 3 weeks ago

michaelboeding commented 3 weeks ago

Answers checklist.

IDF version.

5.2.1

Espressif SoC revision.

ESP32-D0WDR2-V3

Operating System used.

macOS

How did you build your project?

VS Code IDE

If you are using Windows, please specify command line type.

None

Development Kit.

ESP32-D0WDR2-V3 custom board

Power Supply used.

USB

What is the expected behavior?

I expect the wifi scan to work after I init the wifi station.

What is the actual behavior?

Fails to scan.

Steps to reproduce.


esp_err_t WifiNetworkManager::initialize_wifi_sta() {

    wifi_mode_t mode;
    if (esp_wifi_get_mode(&mode) != ESP_OK || mode != WIFI_MODE_STA) {
        printf("Initializing WiFi in STA mode...\n");
        // Initialize the TCP/IP stack
        ESP_ERROR_CHECK(esp_netif_init());
        // Create the event loop
        ESP_ERROR_CHECK(esp_event_loop_create_default());

        // Initialize the WiFi with default configuration
        wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
        ESP_ERROR_CHECK(esp_wifi_init(&cfg));

        // Set WiFi mode to STA (Station mode) if not already set
        if (mode != WIFI_MODE_STA) {

            ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
        }

    }else{
        printf("WiFi already initialized in STA mode.\n");
    }

    return ESP_OK;
}

std::vector<wifi_ap_record_t> WifiNetworkManager::loadAvailableNetworks() {
    // Ensure WiFi STA mode is initialized and started
    ESP_ERROR_CHECK(initialize_wifi_sta());

    // Start WiFi scan
    ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true));

    // Get the number of scanned networks
    uint16_t number_of_networks = 0;
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&number_of_networks));

    // Allocate memory for scan results
    wifi_ap_record_t *ap_records = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * number_of_networks);
    assert(ap_records != nullptr); // Ensure memory allocation was successful

    // Get the scan results
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number_of_networks, ap_records));

    // Create a vector to hold the WiFi network records
    std::vector<wifi_ap_record_t> foundNetworks;

    // Process the scan results
    for (int i = 0; i < number_of_networks; ++i) {
        foundNetworks.push_back(ap_records[i]);
    }

    // Free memory
    free(ap_records);

    return foundNetworks;
}

Debug Logs.

No response

More Information.

Get avaliable networks request fired!!!! Initializing WiFi in STA mode... I (18152) wifi:wifi driver task: 3ffea3d8, prio:23, stack:6656, core=0 I (18172) wifi:wifi firmware version: a9f5b59 I (18172) wifi:wifi certification version: v7.0 I (18172) wifi:config NVS flash: enabled I (18172) wifi:config nano formating: disabled I (18182) wifi:Init data frame dynamic rx buffer num: 32 I (18182) wifi:Init static rx mgmt buffer num: 5 I (18192) wifi:Init management short buffer num: 32 I (18192) wifi:Init static tx buffer num: 16 I (18192) wifi:Init tx cache buffer num: 32 I (18202) wifi:Init static rx buffer size: 1600 I (18202) wifi:Init static rx buffer num: 10 I (18212) wifi:Init dynamic rx buffer num: 32 I (18212) wifi_init: rx ba win: 6 I (18222) wifi_init: tcpip mbox: 32 I (18222) wifi_init: udp mbox: 6 I (18222) wifi_init: tcp mbox: 6 I (18232) wifi_init: tcp tx win: 5760 I (18232) wifi_init: tcp rx win: 5760 I (18232) wifi_init: tcp mss: 1440 I (18242) wifi_init: WiFi/LWIP prefer SPIRAM I (18242) wifi_init: WiFi IRAM OP enabled I (18252) wifi_init: WiFi RX IRAM OP enabled Free heap: 2019404 Total free memory: 2019404 I (22582) HEAP: Free Heap: 33323 bytes MALLOC_CAP_8BIT 2019411 bytes MALLOC_CAP_DMA 33323 bytes MALLOC_CAP_SPIRAM 1986088 bytes MALLOC_CAP_INTERNAL 33323 bytes MALLOC_CAP_DEFAULT 2019404 bytes MALLOC_CAP_IRAM_8BIT 0 bytes MALLOC_CAP_RETENTION 0 bytes

ESP_ERROR_CHECK failed: esp_err_t 0x3002 (ESP_ERR_WIFI_NOT_STARTED) at 0x400eeb39

The error occurs on ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true));

This previously worked on idf v4.4. I upgraded to 5.2.1 and im now also using SPIRAM - not sure if that has something to do with it.

ShyamalKhachane commented 2 weeks ago

Hi @michaelboeding , According to the attached code snippet and logs, it seems that WiFi is not started. Please call esp_wifi_start() after initialising WiFi. For more details relating to the return values of esp_wifi_scan_start(), please refer esp_wifi_scan_start() API description