Esp32S3 : How to close the Wifi properly in AP Mode.

ThomasESP32
Posts: 229
Joined: Thu Jul 14, 2022 5:15 am

Esp32S3 : How to close the Wifi properly in AP Mode.

Postby ThomasESP32 » Tue Jun 27, 2023 8:49 am

Good morning,

I am using an Esp32s3 in AP Mode (OTA update).
Using the example provided, I have two methods that I use to Start the Wifi in AP Mode.

esp_err_t Wifi::softap_init(void)
{
esp_err_t res = ESP_OK;

res |= esp_netif_init();
res |= esp_event_loop_create_default();
pEspNetIf = esp_netif_create_default_wifi_ap();

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
res |= esp_wifi_init(&cfg);

wifi_config_t wifi_config = {
.ap = {
.ssid = "SSIDNAME",
.ssid_len = 8,
.channel = 6,
.max_connection = 3
},
};

res |= esp_wifi_set_mode(WIFI_MODE_AP);
res |= esp_wifi_set_config((wifi_interface_t)ESP_IF_WIFI_AP, &wifi_config);
res |= esp_wifi_start();
return res;
}

/* Méthode utilisée pour initialiser le serveur HTTP : */
esp_err_t Wifi::http_server_init(void)
{
/*
* HTTP Server
*/
httpd_uri_t index_get = {
.uri = "/",
.method = HTTP_GET,
.handler = index_get_handler,
.user_ctx = NULL
};

httpd_uri_t update_post = {
.uri = "/update",
.method = HTTP_POST,
.handler = update_post_handler,
.user_ctx = NULL
};

httpd_config_t config = HTTPD_DEFAULT_CONFIG();

if (httpd_start(&Handle, &config) == ESP_OK) {
httpd_register_uri_handler(Handle, &index_get);
httpd_register_uri_handler(Handle, &update_post);
}

return Handle == NULL ? ESP_FAIL : ESP_OK;
}

So, I have created two methods in order to stop the Wifi. In these methods, I use the "opposite functions".

esp_err_t Wifi::http_server_deinit(void)
{
esp_err_t Err = ESP_FAIL;
Err = httpd_stop(Handle);
return Err;
}

/* On appelle la méthode permettant de faire un arrêt du Wifi en Mode SoftAP : */
esp_err_t Wifi::softap_deinit(void)
{
esp_err_t Err = ESP_FAIL;

Err = esp_wifi_stop();
if (Err != ESP_OK)
return Err;

Err = esp_wifi_deinit();
if (Err != ESP_OK)
return Err;

esp_netif_destroy_default_wifi(pEspNetIf);

Err = esp_event_loop_delete_default();
if (Err != ESP_OK)
return Err;

Err = esp_netif_deinit();

return Err;
}

My problem is that when I call the softap_deinit() method, I get the error 0x106 executing the last function : esp_netif_deinit().
Feature not implemented or something like that. Do you know why please ?

Looking at the Heap memory available :
1) Before Starting the Wifi :
FreeHeapInternal = 173 623 bytes
FreeHeapExternal = 2 062 588 bytes

2) After starting the Wifi :
FreeHeapInternal = 128 155 bytes
FreeHeapExternal = 2 002 104 bytes

3) After stopping the Wifi :
FreeHeapInternal = 167 179 bytes
FreeHeapExternal = 2 062 100 bytes

So, it seems that some part of the memory allocated are not freed correctly.

However, the access point disappear under Windows. But it takes 2 minutes before it disappears.

Thank you for your help,

Best regards,

Thomas TRUILHE

userbogd
Posts: 1
Joined: Tue Jun 27, 2023 6:00 am

Re: Esp32S3 : How to close the Wifi properly in AP Mode.

Postby userbogd » Tue Jun 27, 2023 9:10 am

Just yesterday i faced with the same task - to switching on and off the wifi AP. I also started from manipulation with start/stop and init/deinit wifi driver functions. In the result i stopped on simplest solution - the changing wifi mode on the fly without stopping and deinit of driver. I added this code and it works fine for me without any side effects or memory leaks:

Code: Select all

void WiFiStopAP()
{
    if (GetSysConf()->wifiSettings.WiFiMode == WIFI_MODE_APSTA || GetSysConf()->wifiSettings.WiFiMode == WIFI_MODE_STA)
        ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    else
        ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
}
void WiFiStartAP()
{
    if (GetSysConf()->wifiSettings.WiFiMode == WIFI_MODE_APSTA)
        ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
    else if (GetSysConf()->wifiSettings.WiFiMode == WIFI_MODE_STA)
        ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    else
        ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
}

Who is online

Users browsing this forum: No registered users and 98 guests