Unable to do wifi scan in STA+SoftAP mode after updating to esp-idf-v5.2.1

rohand87
Posts: 1
Joined: Thu May 02, 2024 10:34 am

Unable to do wifi scan in STA+SoftAP mode after updating to esp-idf-v5.2.1

Postby rohand87 » Thu May 02, 2024 10:54 am

Hello,

I have an ESP IDF application which I had developed using esp-idf-v4.3, It started in Wi-Fi STA+SoftAP mode and hosted webpage on an http server. The user could connect to the ESP SoftAP and view the webpage, from the webpage scan for APs to connect, select an AP and connect to it in STA mode. All of these functionalities worked fine in v4.3.

Now I updated to v5.2.1 and the scan functionality stopped working. I modified the softAP_sta example and found the same problem as I am getting in my application. Below is the relevant part of the modified softAP_sta example. There are no errors, but the number of scanned APs is 0 (zero). I added a couple of delays in the code to see if things work but it didn't. Also see the log below.


Can someone please help me understand:
1. What am I doing wrong?
2. Is there something extra to be done in the updated version v5.2.1?
3. How can I debug the problem further?

Code: Select all

/* Initialize wifi station */
esp_netif_t *wifi_init_sta(void)
{
    esp_netif_t *esp_netif_sta = esp_netif_create_default_wifi_sta();
#if 0	// disabling this part so wifi does not connect to an AP. Deon to check wifi scanning
    wifi_config_t wifi_sta_config = {
        .sta = {
            .ssid = EXAMPLE_ESP_WIFI_STA_SSID,
            .password = EXAMPLE_ESP_WIFI_STA_PASSWD,
            .scan_method = WIFI_ALL_CHANNEL_SCAN,
            .failure_retry_cnt = EXAMPLE_ESP_MAXIMUM_RETRY,
            /* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8).
             * If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value
             * to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to
            * WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
             */
            .threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
            .sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
        },
    };

    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_sta_config) );
#endif
    ESP_LOGI(TAG_STA, "wifi_init_sta finished.");

    return esp_netif_sta;
}

...
...

/* Scan for access points, wifi should be in STA or AP+STA mode */
#define DEFAULT_SCAN_LIST_SIZE 10
uint16_t comm_wifi_scan(wifi_ap_record_t *ap_info)
{
    uint16_t number = DEFAULT_SCAN_LIST_SIZE;
    uint16_t ap_count = 0;
    memset(ap_info, 0, sizeof(wifi_ap_record_t));

    int ret = esp_wifi_scan_start(NULL, true);
    if (ret != 0) {
    	ESP_LOGE(TAG_STA, "esp_wifi_scan_start failed with code = %d", ret);
    }

    ESP_LOGI(TAG_STA, "Max AP number ap_info can hold = %u", number);

    vTaskDelay(2000 / portTICK_PERIOD_MS);

//    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
    ret = esp_wifi_scan_get_ap_records(&number, ap_info);
    if (ret != 0) {
    	ESP_LOGE(TAG_STA, "esp_wifi_scan_get_ap_records failed with code = %d", ret);
    }

//    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
    ret = esp_wifi_scan_get_ap_num(&ap_count);
    if (ret != 0) {
    	ESP_LOGE(TAG_STA, "esp_wifi_scan_get_ap_num failed with code = %d", ret);
    }

    ESP_LOGI(TAG_STA, "Total APs scanned = %u", ap_count);
    for (int i = 0; (i < DEFAULT_SCAN_LIST_SIZE) && (i < ap_count); i++) {
        ESP_LOGI(TAG_STA, "SSID \t\t%s", ap_info[i].ssid);
        ESP_LOGI(TAG_STA, "RSSI \t\t%d", ap_info[i].rssi);
        print_auth_mode(ap_info[i].authmode);
        if (ap_info[i].authmode != WIFI_AUTH_WEP) {
            print_cipher_type(ap_info[i].pairwise_cipher, ap_info[i].group_cipher);
        }
        ESP_LOGI(TAG_STA, "Channel \t\t%d\n", ap_info[i].primary);
    }
    return ap_count;
}


void app_main(void)
{
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    //Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    /* Initialize event group */
    s_wifi_event_group = xEventGroupCreate();

    /* Register Event handler */
    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                    ESP_EVENT_ANY_ID,
                    &wifi_event_handler,
                    NULL,
                    NULL));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
                    IP_EVENT_STA_GOT_IP,
                    &wifi_event_handler,
                    NULL,
                    NULL));

    /*Initialize WiFi */
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));

    /* Initialize AP */
    ESP_LOGI(TAG_AP, "ESP_WIFI_MODE_AP");
    esp_netif_t *esp_netif_ap = wifi_init_softap();

    /* Initialize STA */
    ESP_LOGI(TAG_STA, "ESP_WIFI_MODE_STA");
    esp_netif_t *esp_netif_sta = wifi_init_sta();

    /* Start WiFi */
    ESP_ERROR_CHECK(esp_wifi_start() );

    vTaskDelay(2000 / portTICK_PERIOD_MS);

    /* scan */
    wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
    uint16_t ap_count = comm_wifi_scan(ap_info);
    ESP_LOGI(TAG_STA, "scanned %d APs", ap_count);

    /*
     * Wait until either the connection is established (WIFI_CONNECTED_BIT) or
     * connection failed for the maximum number of re-tries (WIFI_FAIL_BIT).
     * The bits are set by event_handler() (see above)
     */
    EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
                                           WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
                                           pdFALSE,
                                           pdFALSE,
                                           portMAX_DELAY);

    /* xEventGroupWaitBits() returns the bits before the call returned,
     * hence we can test which event actually happened. */
    if (bits & WIFI_CONNECTED_BIT) {
        ESP_LOGI(TAG_STA, "connected to ap SSID:%s password:%s",
                 EXAMPLE_ESP_WIFI_STA_SSID, EXAMPLE_ESP_WIFI_STA_PASSWD);
    } else if (bits & WIFI_FAIL_BIT) {
        ESP_LOGI(TAG_STA, "Failed to connect to SSID:%s, password:%s",
                 EXAMPLE_ESP_WIFI_STA_SSID, EXAMPLE_ESP_WIFI_STA_PASSWD);
    } else {
        ESP_LOGE(TAG_STA, "UNEXPECTED EVENT");
        return;
    }

    /* Set sta as the default interface */
    esp_netif_set_default_netif(esp_netif_sta);

    /* Enable napt on the AP netif */
    if (esp_netif_napt_enable(esp_netif_ap) != ESP_OK) {
        ESP_LOGE(TAG_STA, "NAPT not enabled on the netif: %p", esp_netif_ap);
    }
}
Log of softap_sta example

Code: Select all

ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 271414342, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:7172
load:0x40078000,len:15540
load:0x40080400,len:4
0x40080400: _init at ??:?

ho 8 tail 4 room 4
load:0x40080404,len:3904
entry 0x40080640
I (32) boot: ESP-IDF v5.2.1 2nd stage bootloader
I (32) boot: compile time May  2 2024 13:36:43
I (32) boot: Multicore bootloader
I (36) boot: chip revision: v3.0
I (40) boot.esp32: SPI Speed      : 40MHz
I (44) boot.esp32: SPI Mode       : DIO
I (49) boot.esp32: SPI Flash Size : 2MB
I (54) boot: Enabling RNG early entropy source...
I (59) boot: Partition Table:
I (63) boot: ## Label            Usage          Type ST Offset   Length
I (70) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (77) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (85) boot:  2 factory          factory app      00 00 00010000 00100000
I (92) boot: End of partition table
I (96) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=22664h (140900) map
I (153) esp_image: segment 1: paddr=0003268c vaddr=3ffb0000 size=03bb4h ( 15284) load
I (159) esp_image: segment 2: paddr=00036248 vaddr=40080000 size=09dd0h ( 40400) load
I (175) esp_image: segment 3: paddr=00040020 vaddr=400d0020 size=83558h (537944) map
I (360) esp_image: segment 4: paddr=000c3580 vaddr=40089dd0 size=0c8ach ( 51372) load
I (391) boot: Loaded app from partition at offset 0x10000
I (391) boot: Disabling RNG early entropy source...
I (403) cpu_start: Multicore app
I (412) cpu_start: Pro cpu start user code
I (412) cpu_start: cpu freq: 160000000 Hz
I (412) cpu_start: Application information:
I (415) cpu_start: Project name:     softap_sta
I (420) cpu_start: App version:      1
I (424) cpu_start: Compile time:     May  2 2024 13:36:28
I (430) cpu_start: ELF file SHA256:  55a9fa1b7...
I (436) cpu_start: ESP-IDF:          v5.2.1
I (441) cpu_start: Min chip rev:     v0.0
I (445) cpu_start: Max chip rev:     v3.99 
I (450) cpu_start: Chip rev:         v3.0
I (455) heap_init: Initializing. RAM available for dynamic allocation:
I (462) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (468) heap_init: At 3FFB7CF0 len 00028310 (160 KiB): DRAM
I (474) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (481) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (487) heap_init: At 4009667C len 00009984 (38 KiB): IRAM
I (495) spi_flash: detected chip: generic
I (498) spi_flash: flash io: dio
W (502) spi_flash: Detected size(8192k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (516) main_task: Started on CPU0
I (526) main_task: Calling app_main()
I (556) wifi:wifi driver task: 3ffbf880, prio:23, stack:6656, core=0
I (556) wifi:wifi firmware version: a9f5b59
I (556) wifi:wifi certification version: v7.0
I (556) wifi:config NVS flash: enabled
I (556) wifi:config nano formating: disabled
I (566) wifi:Init data frame dynamic rx buffer num: 32
I (566) wifi:Init static rx mgmt buffer num: 5
I (576) wifi:Init management short buffer num: 32
I (576) wifi:Init dynamic tx buffer num: 32
I (576) wifi:Init static rx buffer size: 1600
I (586) wifi:Init static rx buffer num: 10
I (586) wifi:Init dynamic rx buffer num: 32
I (596) wifi_init: rx ba win: 6
I (596) wifi_init: tcpip mbox: 32
I (596) wifi_init: udp mbox: 6
I (606) wifi_init: tcp mbox: 6
I (606) wifi_init: tcp tx win: 5760
I (606) wifi_init: tcp rx win: 5760
I (616) wifi_init: tcp mss: 1440
I (616) wifi_init: WiFi IRAM OP enabled
I (626) wifi_init: WiFi RX IRAM OP enabled
I (626) WiFi SoftAP: ESP_WIFI_MODE_AP
I (896) WiFi SoftAP: wifi_init_softap finished. SSID:myssid password:mypassword channel:1
I (896) WiFi Sta: ESP_WIFI_MODE_STA
I (906) WiFi Sta: wifi_init_sta finished.
I (906) phy_init: phy_version 4791,2c4672b,Dec 20 2023,16:06:06
W (916) phy_init: failed to load RF calibration data (0x1102), falling back to full calibration
I (996) wifi:mode : sta (0c:8b:95:96:eb:5c) + softAP (0c:8b:95:96:eb:5d)
I (1006) wifi:enable tsf
I (1006) wifi:Total power save buffer number: 16
I (1006) wifi:Init max length of beacon: 752/752
I (1006) wifi:Init max length of beacon: 752/752
I (1016) WiFi Sta: Station started
I (1016) esp_netif_lwip: DHCP server started on interface WIFI_AP_DEF with IP: 192.168.4.1
I (5916) WiFi Sta: Max AP number ap_info can hold = 10
I (7916) WiFi Sta: Total APs scanned = 0
I (7916) WiFi Sta: scanned 0 APs


Who is online

Users browsing this forum: **txf** and 98 guests