Deinitialize Arduino WiFi Library in ESP32

Hello, I am new to Arduino IDE and also relatively new to microcontroller programming in general. My question is related to programming ESP32 using Arduino WiFi Library. When I run the below sketch, after disconnecting from WiFi, I still see that memory is not freed up. As per my knowledge from the C programming language, after a function call, the stack memory allocated to the function should be freed. I don't see that happening here.

Any comments on this?

#include <WiFi.h>
const char* ssid       = "Radha";
const char* password   = "wifi123456";


void setup()
{
  Serial.begin(115200);
  delay(100);
  Serial.println(esp_get_free_heap_size());
  delay(5000);
  Serial.println(esp_get_free_heap_size());
  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" CONNECTED");
  Serial.println(esp_get_free_heap_size());

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
 
  Serial.println(" DISCONNECTED");
   Serial.println(esp_get_free_heap_size());
}

void loop() {
  delay(5000);
  Serial.println("In Loop");
  Serial.println(esp_get_free_heap_size());
}

Result in Terminal

266132
266132
Connecting to Radha .. CONNECTED
214604
 DISCONNECTED
216684
In Loop
216776
In Loop
216776
In Loop
216776
In Loop
216776

There are no locals in that function that could be freed.

Probably WiFi.begin() allocates buffers or whatever,
there is no reason to free them only because the user disconnected.

@vangaveeti, your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that are already written, not for questions.

Thanks @Whandall for pointing out. But I couldn't find a function in Arduino WiFi library that will free up the resources. I found the function esp_wifi_deinit() in ESP-IDF that does the job. But I want to know if a similar function is available in Arduino because working with Arduino libraries is much easier.

Thought WiFi.mode(WIFI_OFF) will free the resources but it doesn't.

@sterretje Sorry. My Bad.

What problem are you trying to solve? What resource do you think need to be "freed" and why?

@gfvalvo I need WiFi just to update my RTC. I don't need wifi afterwards. So I don't want WiFi library consuming memory (RAM) once my work (update RTC) is done. That's what I meant by freed. I need RAM for other purposes.

I am able to achieve this by using ESP32-IDF Wifi library as shown below. But the wifi connection is not established. I am working on it.

#include <esp_wifi.h>
#include <time.h>

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

void printLocalTime()
{ 
  char* time_now = NULL;
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  //String time_n = &timeinfo;
  Serial.printf("Time %d:%d:%d\n",timeinfo.tm_hour,timeinfo.tm_min,timeinfo.tm_sec);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(100);
  Serial.println(esp_get_free_heap_size());
  wifiInit();
  Serial.println(esp_get_free_heap_size());

  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();
  
  deinitialize_wifi();
  Serial.println(esp_get_free_heap_size());
}

void loop() {
  // put your main code here, to run repeatedly:
}


void wifiInit(){
  wifi_init_config_t confi = WIFI_INIT_CONFIG_DEFAULT();
  wifi_mode_t mod = WIFI_MODE_STA;
  wifi_config_t wifi_config = {
        .sta = {
            {.ssid = "Radha"},
            {.password = "wifi123456"}
        },
    };

  if( esp_wifi_init(&confi) != ESP_OK )
    Serial.println("Failed to Initialise Wifi");
  if( esp_wifi_set_mode(mod) != ESP_OK )
    Serial.println("Failed to Set WiFi mode");
  if( esp_wifi_set_config((wifi_interface_t)ESP_IF_WIFI_STA, &wifi_config) != ESP_OK )
    Serial.println("Failed to set Config");
  if( esp_wifi_start() != ESP_OK )
    Serial.println("Failed to start WiFi");
  else
    Serial.println("WiFi started");
}

void deinitialize_wifi() {
    esp_wifi_stop();
    esp_wifi_deinit();
    Serial.println("Deinitialised Wifi");
}

Result from Terminal

266476
E (617) event: Event loop not initialized via esp_event_loop_init, but esp_event_send called
WiFi started
229912
Failed to obtain time
E (5629) event: Event loop not initialized via esp_event_loop_init, but esp_event_send called
Deinitialised Wifi
260260

As we can see, the free memory after deinitialization using ESP-IDF WiFi library is 260260 bytes compared to 216776 bytes when used Arduino WiFi library. The difference is close to 45,000 bytes. I can use this 45,000 bytes of RAM for my other memory intensive tasks.

So my question is, do we have a function to deinitialization WiFi in Arduino WiFi.h?

The Arduino framework for ESP32 is simple to use, but not the best from this side of view.
Anyway, you can mix ESP-IDF functions in your code without problem if you need.

Hello @cotestatnt , that's what I am trying to do as I couldn't find a way to deinitialize using the Arduino Library. I found an example that could help me.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.