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.
@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.
@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.