I am new, I have a project that it require to switch between WiFi AP mode and WiFi STA mode (Not AP_STA mode), However, in my implementation, I always get system panic problem when I try to restart wifi to STA mode from AP mode or AP mode from STA mode,
so I want to ask where I get wrong. Below is my code section,
Test code section in main function:
Code: Select all
while (1){
wifi_ap_start("APPPPP", "1234567890");
ESP_LOGI(MAIN_TAG, "ap started");
vTaskDelay(pdMS_TO_TICKS(10000));
wifi_ap_sta_stop();
ESP_LOGI(MAIN_TAG, "ap stopped");
vTaskDelay(pdMS_TO_TICKS(1000));
wifi_sta_start();
ESP_LOGI(MAIN_TAG, "sta started");
vTaskDelay(pdMS_TO_TICKS(10000));
wifi_ap_sta_stop();
ESP_LOGI(MAIN_TAG, "sta stopped");
vTaskDelay(pdMS_TO_TICKS(1000));
}
Code: Select all
static WiFiStatus_t wifi_ap_sta_init(uint8_t mode)
{
if(NULL == g_wifi_internal_event_group)
g_wifi_internal_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_LOGI(WIFI_IFACE_TAG, "esp_netif_init");
if(WIFI_STA_MODE == mode) {
g_wifi_netif = esp_netif_create_default_wifi_sta();
ESP_LOGI(WIFI_IFACE_TAG, "esp_netif_create_default_wifi_sta");
} else {
g_wifi_netif = esp_netif_create_default_wifi_ap();
ESP_LOGI(WIFI_IFACE_TAG, "esp_netif_create_default_wifi_ap");
}
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_LOGI(WIFI_IFACE_TAG, "esp_wifi_init");
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
ESP_LOGI(WIFI_IFACE_TAG, "wifi_ap_sta_init ok");
return WIFI_STATUS_OK;
}
static WiFiStatus_t wifi_ap_sta_stop(void)
{
if(NULL == g_wifi_netif)
return WIFI_STATUS_OK;
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler);
wifi_mode_t current_mode;
esp_wifi_get_mode(¤t_mode);
if(WIFI_MODE_AP == current_mode) {
esp_wifi_deauth_sta(0);
} else if(WIFI_MODE_STA == current_mode) {
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler);
wifi_sta_disconnect();
}
vEventGroupDelete(g_wifi_internal_event_group);
if(ESP_ERR_WIFI_NOT_INIT != esp_wifi_stop())
esp_wifi_deinit();
esp_netif_destroy(g_wifi_netif);
esp_wifi_clear_default_wifi_driver_and_handlers(g_wifi_netif);
g_wifi_netif = NULL;
ESP_LOGI(WIFI_IFACE_TAG, "wifi stop");
return WIFI_STATUS_OK;
}
static WiFiStatus_t wifi_ap_start(char* ssid, char* pwd)
{
if(g_wifi_netif)
return WIFI_STATUS_FAIL;
wifi_ap_sta_init(WIFI_AP_MODE);
wifi_config_t wifi_config = {
.ap = {
.max_connection = 1,
.authmode = WIFI_AUTH_WPA2_PSK
},
};
wifi_config.ap.ssid_len = strlen(ssid);
memcpy(wifi_config.ap.ssid, ssid, strlen(ssid));
memcpy(wifi_config.ap.password, pwd, strlen(pwd));
esp_wifi_set_mode(WIFI_MODE_AP);
esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config);
esp_wifi_start();
EventBits_t bits = xEventGroupWaitBits(g_wifi_internal_event_group,
WIFI_START_BIT,
pdTRUE,
pdFALSE,
pdMS_TO_TICKS(10000));
if (bits & WIFI_START_BIT) {
return WIFI_STATUS_OK;
}
wifi_ap_sta_stop();
return WIFI_STATUS_FAIL;
}
static WiFiStatus_t wifi_sta_start(void)
{
if(g_wifi_netif)
return WIFI_STATUS_FAIL;
wifi_ap_sta_init(WIFI_STA_MODE);
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL);
wifi_config_t wifi_config = {
.sta = {
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
esp_wifi_start();
EventBits_t bits = xEventGroupWaitBits(g_wifi_internal_event_group,
WIFI_START_BIT,
pdTRUE,
pdFALSE,
pdMS_TO_TICKS(10000));
if (bits & WIFI_START_BIT) {
ESP_LOGI(WIFI_IFACE_TAG, "sta start");
return WIFI_STATUS_OK;
}
wifi_ap_sta_stop();
return WIFI_STATUS_FAIL;
}