But I am facing below issue when the function is commented in the wifi_init_sta() function.
Code: Select all
E (458594) esp_netif_lwip: esp_netif_new_api: Failed to configure netif with config=0x3ffc8640 (config or if_key is NULL or duplicate key)
assert failed: esp_netif_create_default_wifi_sta wifi_default.c:388 (netif)
Backtrace: 0x40081a56:0x3ffc84e0 0x400893c1:0x3ffc8500 0x400911b1:0x3ffc8520 0x400e11f9:0x3ffc8640 0x400d7d5c:0x3ffc8670 0x400d7861:0x3ffc8840 0x400e35d5:0x3ffc88e0 0x400e2331:0x3ffc8920 0x400e23d0:0x3ffc89b0 0x400e2ad8:0x3ffc89d0 0x400e12c4:0x3ffc89f0 0x4015753b:0x3ffc8a10 0x400e19c2:0x3ffc8a30 0x400e1a2a:0x3ffc8a80 0x40089ecd:0x3ffc8aa0
My wifi_init_sta() function is like below:
Code: Select all
void wifi_init_sta(char *ssid, char *ssid_password)
{
printf("starting wifi_init_sta\n");
// Disconnect from the current Wi-Fi
ESP_ERROR_CHECK(esp_wifi_disconnect());
// Create an event group to track the Wi-Fi events
s_wifi_event_group = xEventGroupCreate();
// Initialize the network interface
ESP_ERROR_CHECK(esp_netif_init());
//....................commenting below function ....................
//ESP_ERROR_CHECK(esp_event_loop_create_default());
// Create the default station interface
esp_netif_create_default_wifi_sta();
// Initialize the Wi-Fi driver with default configuration
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
// Register event handlers for Wi-Fi 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));
// Set the Wi-Fi configuration using the provided SSID and password
wifi_config_t wifi_config = {
.sta = {
.ssid = {0},
.password = {0},
.threshold.authmode = WIFI_AUTH_WPA2_PSK, // Use WPA2 by default
},
};
strncpy((char *)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
strncpy((char *)wifi_config.sta.password, ssid_password, sizeof(wifi_config.sta.password));
// Set the Wi-Fi mode to station and configure the Wi-Fi settings
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
// Start the Wi-Fi driver
ESP_ERROR_CHECK(esp_wifi_start());
// Connect to the specified Wi-Fi network
ESP_ERROR_CHECK(esp_wifi_connect());
// Wait for either the connection to succeed or fail
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
// Check which event happened
if (bits & WIFI_CONNECTED_BIT)
{
ESP_LOGI(TAG, "Connected to Wi-Fi network SSID: %s, Password: %s", ssid, ssid_password);
}
else if (bits & WIFI_FAIL_BIT)
{
ESP_LOGI(TAG, "Failed to connect to SSID: %s, Password: %s", ssid, ssid_password);
}
else
{
ESP_LOGE(TAG, "Unexpected Wi-Fi event");
}
// Unregister event handlers when done
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
}