Failed to check if wifi was connected
Posted: Tue Jan 09, 2024 10:09 pm
Hello friends, how are you? I can't check if the wifi is connected, even though it is connected. My main routine is basically:
However, even though the Wi-Fi is connecting successfully and the bit is set successfully, somehow the xEventGroupWaitBits function is not returning and the code does not move forward.
Another curious thing is that it was necessary to create the event (wifi_event_group = xEventGroupCreate();) in both the wifi.c and main.c components. In the github examples, the event is only created in main.c. But in my case, if the event is not created in both files, I get the error:
I tried another alternative to check if the wifi was connected successfully using semaphores:
Even after releasing the semaphore, it is not possible to acquire it in the main loop with the function:
I also used the function:
and when analyzing the value of the semaphore I realized that it has different values in the scope of the wifi component and in the scope of the app_main() function, as if they were not the same thing.
This is the structure of my project:
dir - .vscode
- build
- components - http_client
- wifi_station
- sdcard
- main - CMakeLists.txt
- Kconfig.projbuild
- main.c
- sdkconfig
The code has no logic errors, it was copied from examples and checked several times. I even made HTTPS requests and it worked fine.
In short, I need to ensure that the code only progresses if the Wi-Fi is connected and if the connection is lost, wait until it resumes.
I tried to be as clear and objective as possible. Any suggestions on how to solve this "problem"?
Code: Select all
void app_main(void)
{
// initialization SPIFFS
// initialization NVS
wifi_event_group = xEventGroupCreate();
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta(wifi_ssid, wifi_password);
while (1)
{
vTaskDelay(10 / portTICK_PERIOD_MS);
EventBits_t bits = xEventGroupWaitBits(wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT)
{
// if wifi is connected, the code should run normally
} else If (bits & WIFI_FAIL_BITS) {
//reconnection attempt
}
}
}
However, even though the Wi-Fi is connecting successfully and the bit is set successfully, somehow the xEventGroupWaitBits function is not returning and the code does not move forward.
Another curious thing is that it was necessary to create the event (wifi_event_group = xEventGroupCreate();) in both the wifi.c and main.c components. In the github examples, the event is only created in main.c. But in my case, if the event is not created in both files, I get the error:
Code: Select all
assert failed: xEventGroupWaitBits event_groups.c:344 (xEventGroup)
Backtrace: 0x40081afa:0x3ffbb230 0x400899bd:0x3ffbb250 0x40090f49:0x3ffbb270 0x4008d19f:0x3ffbb390 0x400d75a1:0x3ffbb3c0 0x4016ec28:0x3ffbb420 0x4008c3e5:0x3ffbb440
0x40081afa: panic_abort at C:/Users/morei/esp/esp-idf/components/esp_system/panic.c:452
0x400899bd: esp_system_abort at C:/Users/morei/esp/esp-idf/components/esp_system/port/esp_system_chip.c:84
0x40090f49: __assert_func at C:/Users/morei/esp/esp-idf/components/newlib/assert.c:81
0x4008d19f: xEventGroupWaitBits at C:/Users/morei/esp/esp-idf/components/freertos/FreeRTOS-Kernel/event_groups.c:344 (discriminator 1)
0x400d75a1: app_main at C:/esprojects/uart_events_mod2/main/uart_events_example_main.c:497 (discriminator 13)
0x4016ec28: main_task at C:/Users/morei/esp/esp-idf/components/freertos/app_startup.c:208
0x4008c3e5: vPortTaskWrapper at C:/Users/morei/esp/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:162
ELF file SHA256: ae410852a7760135
Rebooting...
Code: Select all
SemaphoreHandle_t conexaoWifiSemaphore = NULL;
void app_main(void)
{
// initialization SPIFFS
// initialization NVS
conexaoWifiSemaphore = xSemaphoreCreateBinary();
if (conexaoWifiSemaphore == NULL)
{
// Lidar com erro na criação do semáforo
while (1)
{
ESP_LOGI(TAG, "Erro na inicialização do semaphoro.");
}
}
wifi_event_group = xEventGroupCreate();
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta(wifi_ssid, wifi_password);
while (1)
{
vTaskDelay(10 / portTICK_PERIOD_MS);
if (xSemaphoreTake(conexaoWifiSemaphore, pdMS_TO_TICKS(10000)) == pdTRUE));
{
// if wifi is connected, the code should run normally
} else {
//reconnection attempt
}
}
}
/* In the wifi.c file:
*the semaphore was declared: extern SemaphoreHandle_t conexaoWifiSemaphore;
**after successful connection, the semaphore was released: xSemaphoreGive(conexaoWifiSemaphore);
Code: Select all
xSemaphoreTake(conexaoWifiSemaphore, pdMS_TO_TICKS(10000)) == pdTRUE)
Code: Select all
int semaphore value = uxSemaphoreGetCount(conexaoWifiSemaphore);
ESP_LOGI(TAG, "Semaphore value in main: %d", semaphore value);
This is the structure of my project:
dir - .vscode
- build
- components - http_client
- wifi_station
- sdcard
- main - CMakeLists.txt
- Kconfig.projbuild
- main.c
- sdkconfig
The code has no logic errors, it was copied from examples and checked several times. I even made HTTPS requests and it worked fine.
In short, I need to ensure that the code only progresses if the Wi-Fi is connected and if the connection is lost, wait until it resumes.
I tried to be as clear and objective as possible. Any suggestions on how to solve this "problem"?