WIFI + MQTT Causes Memory Leakage
Posted: Fri Jul 28, 2023 9:09 am
Dear All,
When I connect Wifi and connect a broker with PubSubClient MQTT library, program works properly. I check periodically (2 sec) min heap memory level using ESP.getMinFreeHeap() function. I experienced that heap memory drops down from 250K to 212 K in 12 hours. I can see every 2 sec some times until 1K drop. At the end ESP restarts when heap reaches under 212K. What should be the issue?
Using VScode PlatformIO
platfromio.ini:
[env:esp32dev]
platform = https://github.com/platformio/platform-espressif32.git
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
knolleary/PubSubClient@^2.8
Program Code:
main.cpp
#include <Arduino.h>
#include <WiFi.h>
#include <ESP.h>
#include <PubSubClient.h>
const char* ssid = "RajaNet";
const char* password = "rtu1235557x";
/*
const char* broker = "5.189.189.4";
const char* muser = "";
const char* mpass = "";
const char* mid = "esp32user2";
const int port =1883;
*/
const char* broker = "broker.hivemq.com";
const char* muser = "";
const char* mpass = "";
const char* mid = "esp32user2";
const int port =1883;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
WiFiClient espClient;
PubSubClient client(espClient);
const int Watch_Out = 14;
const int Wifi_Out = 13;
uint32_t heartbeat=0;
uint32_t heartbeat2=0;
unsigned long MqttreconnectTime,WifiPreviousMillis;
void initWifi() {
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(Wifi_Out, HIGH);
}
bool reconnectMqtt() {
Serial.println("Attempting MQTT connection...");
Serial.print(muser);
Serial.print(" ");
Serial.print(mpass);
Serial.print(" ");
Serial.println(port);
// Attempt to connect
//client.connect(mid,muser,mpass)
String clientId = mid;
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str())) {
Serial.println("Connected to the Mqtt Broker");
// Subscribe
client.subscribe("esp32/command");
} else {
Serial.print("failed, rc=");
Serial.println(client.state());
return false;
}
return client.connected();
}
void check_heap_mem(){
Serial.printf("Lowest level of free heap since boot: %d\n",ESP.getMinFreeHeap());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message);
messageTemp += (char)message;
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
// Changes the output state according to the message
if (String(topic) == "esp32/output") {
Serial.print("MQTT message: ");
messageTemp.trim();
if (messageTemp.startsWith("q"))
{
Serial.println("q..");
}
if(messageTemp == "mem")
{
check_heap_mem();//
}
else if(messageTemp.startsWith("wr")|messageTemp.startsWith("WR"))
{
Serial.println("write");
}
}
}
void setup() {
Serial.begin(115200);
while (!Serial) continue;
pinMode(Watch_Out, OUTPUT);
pinMode(Wifi_Out, OUTPUT);
initWifi();
client.setServer(broker,1883);
client.setKeepAlive(60);
client.setCallback(callback);
//reconnectMqtt();
//delay(5000);
}
void loop() {
unsigned long currentMillis = millis();
// if WiFi is down, try reconnecting every CHECK_WIFI_TIME seconds
if ((WiFi.status() != WL_CONNECTED) && (currentMillis - WifiPreviousMillis >=10000)) {
Serial.print(millis());
Serial.println("Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.reconnect();
WifiPreviousMillis = currentMillis;
}
unsigned long nowb=millis();
if(nowb - heartbeat>2000)
{
heartbeat = millis();
check_heap_mem();
Serial.println(client.state());
if (client.connected())
{
value=10;
snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("esp32/output", msg);
}
}
//************** MQTT ****************************
if (!client.connected() && WiFi.status() == WL_CONNECTED) {
long now = millis();
if (now - MqttreconnectTime > 10000) {
MqttreconnectTime = now;
// Attempt to reconnect
if (reconnectMqtt()) {
MqttreconnectTime = 0;
}
}
} else {
// Client connected
client.loop();
}
}
Terminal Screen:
WiFi connected
IP address:
192.168.1.4
Attempting MQTT connection...
1883
Connected to the Mqtt Broker
Lowest level of free heap since boot: 253296
Lowest level of free heap since boot: 252840
Lowest level of free heap since boot: 252840
Lowest level of free heap since boot: 252840
Lowest level of free heap since boot: 252840
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
When I connect Wifi and connect a broker with PubSubClient MQTT library, program works properly. I check periodically (2 sec) min heap memory level using ESP.getMinFreeHeap() function. I experienced that heap memory drops down from 250K to 212 K in 12 hours. I can see every 2 sec some times until 1K drop. At the end ESP restarts when heap reaches under 212K. What should be the issue?
Using VScode PlatformIO
platfromio.ini:
[env:esp32dev]
platform = https://github.com/platformio/platform-espressif32.git
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
knolleary/PubSubClient@^2.8
Program Code:
main.cpp
#include <Arduino.h>
#include <WiFi.h>
#include <ESP.h>
#include <PubSubClient.h>
const char* ssid = "RajaNet";
const char* password = "rtu1235557x";
/*
const char* broker = "5.189.189.4";
const char* muser = "";
const char* mpass = "";
const char* mid = "esp32user2";
const int port =1883;
*/
const char* broker = "broker.hivemq.com";
const char* muser = "";
const char* mpass = "";
const char* mid = "esp32user2";
const int port =1883;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
WiFiClient espClient;
PubSubClient client(espClient);
const int Watch_Out = 14;
const int Wifi_Out = 13;
uint32_t heartbeat=0;
uint32_t heartbeat2=0;
unsigned long MqttreconnectTime,WifiPreviousMillis;
void initWifi() {
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(Wifi_Out, HIGH);
}
bool reconnectMqtt() {
Serial.println("Attempting MQTT connection...");
Serial.print(muser);
Serial.print(" ");
Serial.print(mpass);
Serial.print(" ");
Serial.println(port);
// Attempt to connect
//client.connect(mid,muser,mpass)
String clientId = mid;
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str())) {
Serial.println("Connected to the Mqtt Broker");
// Subscribe
client.subscribe("esp32/command");
} else {
Serial.print("failed, rc=");
Serial.println(client.state());
return false;
}
return client.connected();
}
void check_heap_mem(){
Serial.printf("Lowest level of free heap since boot: %d\n",ESP.getMinFreeHeap());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message);
messageTemp += (char)message;
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
// Changes the output state according to the message
if (String(topic) == "esp32/output") {
Serial.print("MQTT message: ");
messageTemp.trim();
if (messageTemp.startsWith("q"))
{
Serial.println("q..");
}
if(messageTemp == "mem")
{
check_heap_mem();//
}
else if(messageTemp.startsWith("wr")|messageTemp.startsWith("WR"))
{
Serial.println("write");
}
}
}
void setup() {
Serial.begin(115200);
while (!Serial) continue;
pinMode(Watch_Out, OUTPUT);
pinMode(Wifi_Out, OUTPUT);
initWifi();
client.setServer(broker,1883);
client.setKeepAlive(60);
client.setCallback(callback);
//reconnectMqtt();
//delay(5000);
}
void loop() {
unsigned long currentMillis = millis();
// if WiFi is down, try reconnecting every CHECK_WIFI_TIME seconds
if ((WiFi.status() != WL_CONNECTED) && (currentMillis - WifiPreviousMillis >=10000)) {
Serial.print(millis());
Serial.println("Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.reconnect();
WifiPreviousMillis = currentMillis;
}
unsigned long nowb=millis();
if(nowb - heartbeat>2000)
{
heartbeat = millis();
check_heap_mem();
Serial.println(client.state());
if (client.connected())
{
value=10;
snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("esp32/output", msg);
}
}
//************** MQTT ****************************
if (!client.connected() && WiFi.status() == WL_CONNECTED) {
long now = millis();
if (now - MqttreconnectTime > 10000) {
MqttreconnectTime = now;
// Attempt to reconnect
if (reconnectMqtt()) {
MqttreconnectTime = 0;
}
}
} else {
// Client connected
client.loop();
}
}
Terminal Screen:
WiFi connected
IP address:
192.168.1.4
Attempting MQTT connection...
1883
Connected to the Mqtt Broker
Lowest level of free heap since boot: 253296
Lowest level of free heap since boot: 252840
Lowest level of free heap since boot: 252840
Lowest level of free heap since boot: 252840
Lowest level of free heap since boot: 252840
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250760
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 250624
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364
Lowest level of free heap since boot: 248364