I'd like to have the wifi try to connect on startup and if it does, great - but if not to continue running other tasks and keep trying to connect.
The way I approcated this was to remove the xEventGroupWaitBits section in the example. https://github.com/espressif/esp-idf/bl ... #L107-L130, and also to modify the event handler to continue attempting to connect on a WIFI_EVENT_STA_DISCONNECTED event.
If I compile and run this, then it works as expected, the event handler runs and when the AP becomes available, it connects. If I turn off the AP I see a disconnected message. I'm sure that there's additional events I will need to handle properly, but so far, so good.
My problems start when I try to add a task[2] to monitor the bits set in `s_wifi_event_group`, namely `WIFI_CONNECTED_BIT (BIT0)` and `WIFI_FAIL_BIT (BIT1)`. I start this task after the `wifi_init_sta()` call in `app_main`[3].
Even though I've tried delaying the task startup by several seconds to allow time for the event handler to be created, I seem to get an immediate panic along the lines of:
Code: Select all
I (751) wifi station: wifi_init_sta finished.
I (751) wifi stati
***ERROR*** A stack overflow in task task_poll_wifi_ has been detected.
What is the reason for this? Is there a way to check that the event group has been created, checking for a NULL value seems to give me an exception also.
Any advice appreciated, thanks.
[1] Modified event handler.
Code: Select all
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();
}
// If we are disconnected, try to connect
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
ESP_LOGI(TAG, "Try to connect to the AP");
esp_wifi_connect();
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);
}
}
Code: Select all
void task_poll_wifi_status(void *parameters)
{
ESP_LOGI(TAG, "Delaying for 10 seconds to allow wifi to start.");
// Delaying to allow wifi event group to get created.
vTaskDelay(10000 / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "Starting polling task.");
EventBits_t bits;
// Tasks should not return.
while(1)
{
printf("\nPolling wifi status.\n");
bits = xEventGroupGetBits(s_wifi_event_group);
printf("\nDo we ever get here?\n");
if (bits & WIFI_CONNECTED_BIT)
{
ESP_LOGI(TAG, "connected to ap");
}
else if (bits & WIFI_FAIL_BIT)
{
ESP_LOGI(TAG, "Failed to connect to ap");
}
else
{
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}
Code: Select all
xTaskCreate(
task_poll_wifi_status, // Task function.
"task_poll_wifi_status", // Task name
1024, // Stack size in bytes
NULL, // Parameters for task
5, // Task priority
NULL // Task handle.
);