Page 1 of 1

WiFi station mode connect error due to assert failed: xEventGroupWaitBits event_groups.c:369 (xEventGroup)

Posted: Sun Jul 31, 2022 3:10 pm
by sureace
Hi,
i am new to ESP IDF framework. I am trying to run wifi station mode example. But i got struck with the below error. Please check the logs below. Could you please help how to resolve this issue.

Code
-------
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_mac.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"

#define WIFI_AP_SSID "xxxxxx"
#define WIFI_AP_PASS "xxxxxxxxxx"
#define WIFI_AP_CHANNEL 1
#define MAX_WIFI_CONNECT_RETRY 5

#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1

static int s_retry_num = 0;

static EventGroupHandle_t s_wifi_event_group;

static const char *TAG = "wifi softAP";

//Handling wifi events
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 < MAX_WIFI_CONNECT_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:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}


void app_main(){
//Initialize NVS
//(Is partition in flash memory which stores key-value pairs)
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);

//Initialise lwIP
//ESP-IDF uses lwIP(), a light weight TCP/IP libray stack. lwIP implement protocols such as TCP, UDP, IP, DHCP, etc.
//ESP-NETIF is another library that encapsulates lwIP and provies another set of APIs
ESP_ERROR_CHECK(esp_netif_init());//Creating task for lwIP and send all events to the event task and event task notifies applicaiton task about those events

//Create default event loop
ESP_ERROR_CHECK(esp_event_loop_create_default());

//Listening WiFi and IP events
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(
WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id
));
ESP_ERROR_CHECK(esp_event_handler_instance_register(
IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip
));

//Binding WiFi driver and esp-netif
esp_netif_create_default_wifi_sta();

//Create wifi driver task
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
printf("Initialization done!\n");

//Configure WiFi
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_AP_SSID,
.password = WIFI_AP_PASS
}
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA,&wifi_config));

//Start wif
ESP_ERROR_CHECK(esp_wifi_start());

EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);

if(bits & WIFI_CONNECTED_BIT){
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",WIFI_AP_SSID, WIFI_AP_PASS);
}else if(bits & WIFI_FAIL_BIT){
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",WIFI_AP_SSID, WIFI_AP_PASS);
}else{
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}


}

Error Logs
-------------
I (28) boot: ESP-IDF v4.4.1-dirty 2nd stage bootloader
I (28) boot: compile time 19:29:36
I (28) boot: chip revision: 1
I (31) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (38) boot.esp32: SPI Speed : 40MHz
I (43) boot.esp32: SPI Mode : DIO
I (47) boot.esp32: SPI Flash Size : 2MB
I (52) boot: Enabling RNG early entropy source...
I (57) boot: Partition Table:
I (61) boot: ## Label Usage Type ST Offset Length
I (68) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (76) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (83) boot: 2 factory factory app 00 00 00010000 00100000
I (91) boot: End of partition table
I (95) boot_comm: chip revision: 1, min. application chip revision: 0
I (102) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=13438h ( 78904) map
I (139) esp_image: segment 1: paddr=00023460 vaddr=3ffb0000 size=03808h ( 14344) load
I (145) esp_image: segment 2: paddr=00026c70 vaddr=40080000 size=093a8h ( 37800) load
I (161) esp_image: segment 3: paddr=00030020 vaddr=400d0020 size=6d398h (447384) map
I (323) esp_image: segment 4: paddr=0009d3c0 vaddr=400893a8 size=0af7ch ( 44924) load
I (341) esp_image: segment 5: paddr=000a8344 vaddr=50000000 size=00010h ( 16) load
I (352) boot: Loaded app from partition at offset 0x10000
I (352) boot: Disabling RNG early entropy source...
I (364) cpu_start: Pro cpu up.
I (364) cpu_start: Starting app cpu, entry point is 0x40081184
0x40081184: call_start_cpu1 at /Users/suresh/esp/esp-idf/components/esp_system/port/cpu_start.c:160

I (0) cpu_start: App cpu up.
I (378) cpu_start: Pro cpu start user code
I (378) cpu_start: cpu freq: 160000000
I (378) cpu_start: Application information:
I (383) cpu_start: Project name: helloworld
I (388) cpu_start: App version: v4.4.1-dirty
I (393) cpu_start: Compile time: Jun 7 2022 19:29:24
I (399) cpu_start: ELF file SHA256: 2637d1611d03d44d...
I (405) cpu_start: ESP-IDF: v4.4.1-dirty
I (411) heap_init: Initializing. RAM available for dynamic allocation:
I (418) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (424) heap_init: At 3FFB74A0 len 00028B60 (162 KiB): DRAM
I (430) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (437) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (443) heap_init: At 40094324 len 0000BCDC (47 KiB): IRAM
I (450) spi_flash: detected chip: generic
I (454) spi_flash: flash io: dio
W (458) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (472) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (544) wifi:wifi driver task: 3ffc0094, prio:23, stack:6656, core=0
I (544) system_api: Base MAC address is not set
I (544) system_api: read default base MAC address from EFUSE
I (564) wifi:wifi firmware version: 63017e0
I (564) wifi:wifi certification version: v7.0
I (564) wifi:config NVS flash: enabled
I (564) wifi:config nano formating: disabled
I (564) wifi:Init data frame dynamic rx buffer num: 32
I (574) wifi:Init management frame dynamic rx buffer num: 32
I (574) wifi:Init management short buffer num: 32
I (584) wifi:Init dynamic tx buffer num: 32
I (584) wifi:Init static rx buffer size: 1600
I (584) wifi:Init static rx buffer num: 10
I (594) wifi:Init dynamic rx buffer num: 32
I (594) wifi_init: rx ba win: 6
I (604) wifi_init: tcpip mbox: 32
I (604) wifi_init: udp mbox: 6
I (604) wifi_init: tcp mbox: 6
I (614) wifi_init: tcp tx win: 5744
I (614) wifi_init: tcp rx win: 5744
I (624) wifi_init: tcp mss: 1440
I (624) wifi_init: WiFi IRAM OP enabled
I (624) wifi_init: WiFi RX IRAM OP enabled
Initialization done!
I (634) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (744) wifi:mode : sta (58:bf:25:33:38:3c)
I (744) wifi:enable tsf

assert failed: xEventGroupWaitBits event_groups.c:369 (xEventGroup)


Backtrace:0x400819f2:0x3ffb9e900x400883b9:0x3ffb9eb0 0x4008ef25:0x3ffb9ed0 0x4008b206:0x3ffb9ff0 0x400d5ed3:0x3ffba020 0x4013bbcd:0x3ffba1b0 0x4008b545:0x3ffba1d0
0x400819f2: panic_abort at /Users/suresh/esp/esp-idf/components/esp_system/panic.c:402

0x400883b9: esp_system_abort at /Users/suresh/esp/esp-idf/components/esp_system/esp_system.c:128

0x4008ef25: __assert_func at /Users/suresh/esp/esp-idf/components/newlib/assert.c:85

0x4008b206: xEventGroupWaitBits at /Users/suresh/esp/esp-idf/components/freertos/event_groups.c:369 (discriminator 1)

0x400d5ed3: app_main at /Users/suresh/esp/esp-idf/projects/helloworld/build/../main/wifi_ap_mode.c:237 (discriminator 2)

0x4013bbcd: main_task at /Users/suresh/esp/esp-idf/components/freertos/port/port_common.c:129 (discriminator 2)

0x4008b545: vPortTaskWrapper at /Users/suresh/esp/esp-idf/components/freertos/port/xtensa/port.c:131

Re: WiFi station mode connect error due to assert failed: xEventGroupWaitBits event_groups.c:369 (xEventGroup)

Posted: Mon Aug 01, 2022 11:09 am
by sanketwadekar
Hi @sureace,
In your code, you have declared a variable s_wifi_event_group of type EventGroupHandle_t but haven't created a event group.
You need to add the following line of code at the start of your app_main() to actually create an event group.

Code: Select all

s_wifi_event_group = xEventGroupCreate();