1

It was has been observed, that due to some reasons. The delay is causing issues with the free heap memory which indirectly is causing data issues. The following is the part in void loop of the sketch. Releasing something around 4kb and causing data corruption to a variable of type const char*

void loop()
{
  Serial.print("Heap size start loop: ");
  Serial.println(ESP.getFreeHeap());
  Serial.println(mqttServer);  
  Serial.println(mqttPort);
  
  
  //added in v0.5
  fanPinChange = (bool)( (fanPin1 != digitalRead(s1) ) || (fanPin2 != digitalRead(s2) ) || (fanPin3 != digitalRead(s3) ) || (fanPin4 != digitalRead(s4) ));

  if (fanPin1 != digitalRead(s1) ) {
    fanPin1 = digitalRead(s1);
  }
  if (fanPin2 != digitalRead(s2) ) {
    fanPin2 = digitalRead(s2);
  }
  if (fanPin3 != digitalRead(s3) ) {
    fanPin3 = digitalRead(s3);
  }
  if (fanPin4 != digitalRead(s4) ) {
    fanPin4 = digitalRead(s4);
  } //v0.5

  Serial.print("Heap size check loop1: ");
  Serial.println(ESP.getFreeHeap());
  Serial.println(mqttServer); 

  Serial.println(WiFi.status());
  Serial.print("Heap size check loop2: ");
  Serial.println(ESP.getFreeHeap());
  Serial.println(mqttServer);
**//no issues so far**



  delay(1000);//**notice from here**
  Serial.print("Heap size check loop3: ");
  Serial.println(ESP.getFreeHeap());
  Serial.println(mqttServer); **//prints gibberish/corrupted data**

  //Check WiFi connection status
  Serial.print("Wifi status: ");


  if (WiFi.status() != WL_CONNECTED)
  {// you can skip to the below else part for now
    digitalWrite(R6, LOW);
    if (DEBUG_SW) Serial.println("Wifi Not Connected");
    delay(400); //custom
    digitalWrite(R6, HIGH);
    delay(400); //custom
    Serial.print("Heap size check loop wifi off: ");
  Serial.println(ESP.getFreeHeap());
  Serial.println(mqttServer); 

  }
  else
  {
    Serial.println("Wifi Connected");
        Serial.print("Heap size after wifi connected and before delay: ");
        Serial.println(ESP.getFreeHeap());
        Serial.println(mqttServer); 
    delay(1000);
    //if (DEBUG_SW) 
    Serial.println("Wifi Connected");
        Serial.print("Heap size after wifi connected: ");
        Serial.println(ESP.getFreeHeap());
        Serial.println(mqttServer); //prints gibberish/corrupted data
    //0.4 logic added
    //reconnect to mqtt or process if incoming string present
    if (pubsubclient.connected()) {
      Serial.println("connected with MQTT");
      digitalWrite(R6, HIGH);
      Serial.print("Heap size check wifi on pubsub connected: ");
  Serial.println(ESP.getFreeHeap());
  Serial.println(mqttServer); 
      
      pubsubclient.loop();
      Serial.print("Heap size check LOOP wifi on pubsub connected: ");
  Serial.println(ESP.getFreeHeap());
  Serial.println(mqttServer); 
      InternetStatus =true;
       Serial.print("Heap size check LOOP wifi on pubsub connected2: ");
  Serial.println(ESP.getFreeHeap());
  Serial.println(mqttServer); 

    } else {
      Serial.print("Heap size after pubsub: ");
      Serial.println(ESP.getFreeHeap());
      Serial.println(mqttServer); 
      Serial.print("Reconnect to MQTT server failed with state ");
      Serial.println(pubsubclient.state());
      digitalWrite(R6, LOW);
      InternetStatus = setup_mqtt();

    }
  }
 
  Serial.print("Heap size before internetstatus: ");
  Serial.println(ESP.getFreeHeap());

//not important here

  if (InternetStatus)
    with_internet();
  else
    without_internet();
    Serial.print("Heap size on exit loop: ");
  Serial.println(ESP.getFreeHeap());
}

The serial monitor prints:

[IP removed in question]
3
Heap size check loop2: 252872
[IP removed in question]
Heap size check loop3: 256044
 ��?6�
Wifi status: Wifi Connected
Heap size after wifi connected and before delay: 256044
 ��?6�
Wifi Connected
Heap size after wifi connected: 256044
 ��?�
connected with MQTT
Heap size check wifi on pubsub connected: 256044
 ��?�
Heap size check LOOP wifi on pubsub connected: 256604

The serial logs

Full code in :paste bin code link

How to resolve this issue?

I was expecting it to print IP which is printed before the delay is introduced.

Shariq Azim
  • 136
  • 16
  • Delay won't cause heap memory leak, what make you think that you have a heap memory leak to begin with? You don't need to add `ESP.getFreeHeap()` everywhere, you only need one in the `loop()`... – hcheung Jun 03 '23 at 11:20
  • Hello @hcheung thats what I'm trying to prove that ... if you follow the sequence of serial logs you would notice that the issue would not arise until delay is introduced – Shariq Azim Jun 03 '23 at 15:18
  • Well, that's what I try to told you, to find the bug, comment out the code, not to add Serial.print() and ESP>getFreeHeap() everywhere. If you restore your code to its original form, and comment out the delay, you will probably see that memory leak is still there... or you don't have memory leap at all, so my question again, what make you think that you had a memory leak to begin with? – hcheung Jun 05 '23 at 00:20
  • Check the the first three messages that has the heap memory? You will notice that 252872 later when delay was added it went to 256044 ...isnt this memory leak ? – Shariq Azim Jun 05 '23 at 05:49
  • 1
    If that's the case, I think you need to have better understanding of what the FreeHeap means, the FreeHeap value represented the memory available for heap, the higher the better, if the free heap increase from 252872 to 256044 bytes, it means that 4k of memory has been free up from the usage, which is a good think, which is the opposite of memory leak... Again, what make you think that you had a memory leak to begin with? If you have memory leak, the FreeHeap memory value will go down on each loop and eventually crash, do you experience that? – hcheung Jun 05 '23 at 06:45
  • I see so probably my understanding of the free heap and memory leak was not correct..but now also isnt it also a thing to note that the 4kb getting freed is causing the data corruption of const char* variable? – Shariq Azim Jun 05 '23 at 08:21
  • Gus i have added the full code to https://pastebin.com/utnWEang – Shariq Azim Jun 05 '23 at 15:44

0 Answers0