espressif / esp-idf

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

esp32 stuck in loop of "xEventGroupWaitBits" (IDFGH-4480) #6309

Closed MarkEvens closed 3 years ago

MarkEvens commented 3 years ago

Environment

Problem Description

I am using esp32 in sta+ap mode both mode start running but my code not go forward after "xEventGroupWaitBits" function called and stuck in there.

Code to reproduce this issue

wifi_config
wifi_config.sta.ssid = "xxxx" 
wifi_config.sta.password = "xxxxxxxxxx";
wifi_config.sta.pmf_cfg.capable = true;
wifi_config.sta.pmf_cfg.required = false;

wifi_config_t wifi_ap_config = {};
wifi_ap_config.ap.ssid = "ESP-32"
wifi_config.ap.ssid_len = 32;
wifi_ap_config.ap.password = "123456789"
wifi_ap_config.ap.max_connection = wifiCred.maxConnection;
wifi_ap_config.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;

if (strlen(wifiCred.apPWD) == 0)
{
    wifi_ap_config.ap.authmode = WIFI_AUTH_OPEN;
}

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_ap_config));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());

ESP_LOGI(TAG, "wifi_init_sta finished.");
start_webserver();
    /* Waiting 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) */
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. */
    ESP_LOGI(TAG, "\nfinished");
    if (bits & WIFI_CONNECTED_BIT)
    {
        ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
                    wifiCred.routerSSID, wifiCred.routerPWD);
}
else if (bits & WIFI_FAIL_BIT)
{
    ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
                wifiCred.routerSSID, wifiCred.routerPWD);
}
else
{
    ESP_LOGE(TAG, "UNEXPECTED EVENT");
}

"wifi_init_sta finished" this message in printing on but terminal but after that next mesaage of "finished" in not.

MarkEvens commented 3 years ago

this is my debug log

I (1191) wifi:wifi firmware version: 6e1f9e4
I (1191) wifi:config NVS flash: enabled
I (1191) wifi:config nano formating: disabled
I (1191) wifi:Init data frame dynamic rx buffer num: 32
I (1201) wifi:Init management frame dynamic rx buffer num: 32
I (1201) wifi:Init management short buffer num: 32
I (1211) wifi:Init dynamic tx buffer num: 32
I (1211) wifi:Init static rx buffer size: 1600
I (1211) wifi:Init static rx buffer num: 10
I (1221) wifi:Init dynamic rx buffer num: 32
I (1321) phy: phy_version: 4390, 6b3c1f2, Sep 10 2020, 15:09:07, 0, 0
I (1331) wifi:mode : sta (4c:11:ae:7b:b9:88) + softAP (4c:11:ae:7b:b9:89)
I (1331) wifi:Total power save buffer number: 16
I (1331) wifi:Init max length of beacon: 752/752
I (1341) wifi:Init max length of beacon: 752/752
I (1341) Wifi: wifi_init_sta finished.
I (3791) wifi:ap channel adjust o:1,1 n:7,2
I (3791) wifi:new:<7,2>, old:<1,1>, ap:<7,2>, sta:<7,2>, prof:1
I (4841) wifi:state: init -> auth (b0)
I (4851) wifi:state: auth -> assoc (0)
I (4861) wifi:state: assoc -> run (10)
I (4871) wifi:connected with TP-LINK_1032, aid = 1, channel 7, 40D, bssid = f4:f2:6d:8f:10:32
I (4871) wifi:security: WPA2-PSK, phy: bgn, rssi: -39
I (4881) wifi:pm start, type: 1

I (4931) wifi:AP's beacon interval = 102400 us, DTIM period = 1
I (13741) wifi:new:<7,0>, old:<7,2>, ap:<7,2>, sta:<7,0>, prof:1
I (15691) wifi:new:<7,2>, old:<7,0>, ap:<7,2>, sta:<7,2>, prof:1
I (18761) wifi:new:<7,0>, old:<7,2>, ap:<7,2>, sta:<7,0>, prof:1
I (19681) wifi:new:<7,2>, old:<7,0>, ap:<7,2>, sta:<7,2>, prof:1
I (60741) wifi:new:<7,0>, old:<7,2>, ap:<7,2>, sta:<7,0>, prof:1
I (61671) wifi:new:<7,2>, old:<7,0>, ap:<7,2>, sta:<7,2>, prof:1
I (67711) wifi:new:<7,0>, old:<7,2>, ap:<7,2>, sta:<7,0>, prof:1
I (69761) wifi:new:<7,2>, old:<7,0>, ap:<7,2>, sta:<7,2>, prof:1
I (89721) wifi:new:<7,0>, old:<7,2>, ap:<7,2>, sta:<7,0>, prof:1
I (90751) wifi:new:<7,2>, old:<7,0>, ap:<7,2>, sta:<7,2>, prof:1
I (129761) wifi:new:<7,0>, old:<7,2>, ap:<7,2>, sta:<7,0>, prof:1
I (130691) wifi:new:<7,2>, old:<7,0>, ap:<7,2>, sta:<7,2>, prof:1
I (167751) wifi:new:<7,0>, old:<7,2>, ap:<7,2>, sta:<7,0>, prof:1
I (168671) wifi:new:<7,2>, old:<7,0>, ap:<7,2>, sta:<7,2>, prof:1

as we can see esp never got ip

MarkEvens commented 3 years ago

I am tried example to change in c++ code and run it

/* WiFi station Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"

/* The examples use WiFi configuration that you can set via project configuration menu

   If you'd rather not, just change the below entries to strings with
   the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_ESP_WIFI_SSID      CONFIG_ESP_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS      CONFIG_ESP_WIFI_PASSWORD
#define EXAMPLE_ESP_MAXIMUM_RETRY  CONFIG_ESP_MAXIMUM_RETRY

/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;

/* The event group allows multiple bits for each event, but we only care about two events:
 * - we are connected to the AP with an IP
 * - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT      BIT1

static const char *TAG = "wifi station";

static int s_retry_num = 0;

static void event_handler(void* arg, esp_event_base_t event_base,
                                int32_t event_id, void* event_data)
{
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
        esp_wifi_connect();
    } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
        if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
            esp_wifi_connect();
            s_retry_num++;
            ESP_LOGI(TAG, "retry to connect to the AP");
        } else {
            xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
        }
        ESP_LOGI(TAG,"connect to the AP fail");
    } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
        ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
        ESP_LOGI(TAG, "got ip:%s",
                 ip4addr_ntoa(&event->ip_info.ip));
        s_retry_num = 0;
        xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
    }
}

void wifi_init_sta()
{
    s_wifi_event_group = xEventGroupCreate();

    tcpip_adapter_init();

    ESP_ERROR_CHECK(esp_event_loop_create_default());

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));

    wifi_config_t wifi_config = {};
    strcpy((char*)wifi_config.sta.ssid,"TP-LINK_1032");
    strcpy((char*)wifi_config.sta.password, "63728460");
    /* Setting a password implies station will connect to all security modes including WEP/WPA.
             * However these modes are deprecated and not advisable to be used. Incase your Access point
             * doesn't support WPA2, these mode can be enabled by commenting below line */
    wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;

    wifi_config.sta.pmf_cfg = {};
    wifi_config.sta.pmf_cfg.capable = true;
    wifi_config.sta.pmf_cfg.required = false;
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
    ESP_ERROR_CHECK(esp_wifi_start() );

    ESP_LOGI(TAG, "wifi_init_sta finished.");

    /* Waiting 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, "connected to ap SSID:%s password:%s",
                 EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
    } else if (bits & WIFI_FAIL_BIT) {
        ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
                 EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
    } else {
        ESP_LOGE(TAG, "UNEXPECTED EVENT");
    }

    ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
    ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
    vEventGroupDelete(s_wifi_event_group);
}

extern "C" void app_main()
{
    //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);

    ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
    wifi_init_sta();
}

and still it is not assigning IP or device not getting IP got event debug log

I (72) boot: Chip Revision: 1
I (72) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (39) boot: ESP-IDF v4.0.2-206-g5630b17e1 2nd stage bootloader
I (39) boot: compile time 13:10:21
I (40) boot: Enabling RNG early entropy source...
I (45) boot: SPI Speed      : 40MHz
I (49) boot: SPI Mode       : DIO
I (53) boot: SPI Flash Size : 4MB
I (57) boot: Partition Table:
I (61) boot: ## Label            Usage          Type ST Offset   Length
I (68) boot:  0 nvs              WiFi data        01 02 00009000 00005000
I (76) boot:  1 otadata          OTA data         01 00 0000e000 00002000
I (83) boot:  2 app0             OTA app          00 10 00010000 00140000
I (91) boot:  3 app1             OTA app          00 11 00150000 00140000
I (98) boot:  4 spiffs           Unknown data     01 82 00290000 00170000
I (106) boot: End of partition table
I (110) boot_comm: chip revision: 1, min. application chip revision: 0
I (117) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x170fc ( 94460) map
I (160) esp_image: segment 1: paddr=0x00027124 vaddr=0x3ffb0000 size=0x03728 ( 14120) load
I (166) esp_image: segment 2: paddr=0x0002a854 vaddr=0x40080000 size=0x00400 (  1024) load
I (167) esp_image: segment 3: paddr=0x0002ac5c vaddr=0x40080400 size=0x053b4 ( 21428) load
I (184) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x708bc (460988) map
I (348) esp_image: segment 5: paddr=0x000a08dc vaddr=0x400857b4 size=0x10250 ( 66128) load
I (389) boot: Loaded app from partition at offset 0x10000
I (389) boot: Disabling RNG early entropy source...
I (390) cpu_start: Pro cpu up.
I (393) cpu_start: Application information:
I (398) cpu_start: Project name:     wifi_station
I (403) cpu_start: App version:      1
I (408) cpu_start: Compile time:     Dec 23 2020 13:10:14
I (414) cpu_start: ELF file SHA256:  6170bdc763c596cc...
I (420) cpu_start: ESP-IDF:          v4.0.2-206-g5630b17e1
I (426) cpu_start: Starting app cpu, entry point is 0x40081370
I (0) cpu_start: App cpu up.
I (437) heap_init: Initializing. RAM available for dynamic allocation:
I (443) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (449) heap_init: At 3FFB96F0 len 00026910 (154 KiB): DRAM
I (456) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (462) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (468) heap_init: At 40095A04 len 0000A5FC (41 KiB): IRAM
I (475) cpu_start: Pro cpu start user code
I (493) spi_flash: detected chip: generic
I (494) spi_flash: flash io: dio
I (494) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (563) wifi station: ESP_WIFI_MODE_STA
I (583) wifi:wifi driver task: 3ffc1d74, prio:23, stack:6656, core=0
I (583) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (583) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (613) wifi:wifi firmware version: 6e1f9e4
I (613) wifi:config NVS flash: enabled
I (613) wifi:config nano formating: disabled
I (613) wifi:Init data frame dynamic rx buffer num: 32
I (613) wifi:Init management frame dynamic rx buffer num: 32
I (623) wifi:Init management short buffer num: 32
I (623) wifi:Init dynamic tx buffer num: 32
I (633) wifi:Init static rx buffer size: 1600
I (633) wifi:Init static rx buffer num: 10
I (643) wifi:Init dynamic rx buffer num: 32
I (733) phy: phy_version: 4390, 6b3c1f2, Sep 10 2020, 15:09:07, 0, 0
I (733) wifi:mode : sta (4c:11:ae:7b:b9:88)
I (743) wifi station: wifi_init_sta finished.
I (863) wifi:new:<7,2>, old:<1,0>, ap:<255,255>, sta:<7,2>, prof:1
I (863) wifi:state: init -> auth (b0)
I (873) wifi:state: auth -> assoc (0)
I (873) wifi:state: assoc -> run (10)
I (893) wifi:connected with TP-LINK_1032, aid = 1, channel 7, 40D, bssid = f4:f2:6d:8f:10:32
I (893) wifi:security: WPA2-PSK, phy: bgn, rssi: -40
I (893) wifi:pm start, type: 1

I (973) wifi:AP's beacon interval = 102400 us, DTIM period = 1
I (96513) wifi:new:<7,0>, old:<7,2>, ap:<255,255>, sta:<7,0>, prof:1
I (97533) wifi:new:<7,2>, old:<7,0>, ap:<255,255>, sta:<7,2>, prof:1
I (107473) wifi:new:<7,0>, old:<7,2>, ap:<255,255>, sta:<7,0>, prof:1
I (121503) wifi:new:<7,2>, old:<7,0>, ap:<255,255>, sta:<7,2>, prof:1
I (134503) wifi:new:<7,0>, old:<7,2>, ap:<255,255>, sta:<7,0>, prof:1
I (136453) wifi:new:<7,2>, old:<7,0>, ap:<255,255>, sta:<7,2>, prof:1
YouDONG-ESP commented 3 years ago

Can you fix the channel of your router? Seem like your router keep switching channel.

I (96513) wifi:new:<7,0>, old:<7,2>, ap:<255,255>, sta:<7,0>, prof:1
I (97533) wifi:new:<7,2>, old:<7,0>, ap:<255,255>, sta:<7,2>, prof:1

I run your second code and there is no problem for getting IP address.(except i run it in C and it need to remove line wifi_config.sta.pmf_cfg = {};to compile), dose this issue happen with others router?

MarkEvens commented 3 years ago

In 'C' it also work with my setup but in "C++" not

MarkEvens commented 3 years ago

my TP-LINK router has some issue worked with other router second code is working.

MarkEvens commented 3 years ago

need help my esp32 is in AP+STA mode, and esp32 is not connected with any router, my mobile connect to my esp32 AP now i want to scan wifi network, without losing my mobile connection.

give me something it took my lot's of time.

YouDONG-ESP commented 3 years ago

Could you show me the complete code for the AP+STA mode?

MarkEvens commented 3 years ago

sample_code.zip this my code in which i have two rest api

  1. scanSSIDs
  2. getSSIDs

first i call scanSSIDs which start scanning fill ssid list to get_sstd_list variable and after that i called getSSIDs for getting list of ssid but, my mobile disconnect from esp32 hotspot i want to do this without disconnect

MarkEvens commented 3 years ago

only first time I have above issue after that mobile is not disconnecting

but don't how esp32 is watch dog resting.

E (145695) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (145695) task_wdt:  - IDLE0 (CPU 0)
E (145695) task_wdt: Tasks currently running:
E (145695) task_wdt: CPU 0: tiT
E (145695) task_wdt: CPU 1: wifi_app_task
E (152695) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (152695) task_wdt:  - IDLE0 (CPU 0)
E (152695) task_wdt: Tasks currently running:
E (152695) task_wdt: CPU 0: tiT
E (152695) task_wdt: CPU 1: wifi_app_task
YouDONG-ESP commented 3 years ago

@MarkEvens in funciton wifi_app_task(),vTaskDelay(1/portTICK_PERIOD_MS); means delay 1ms which is too short that may lead task watchdog. Set it too 1000/portTICK_PERIOD_MS(1 second) will be ok. Others wifi part of your code run correctly in my side.

MarkEvens commented 3 years ago

@YouDONG-ESP Thanks i got my answer.