I've got an ESP32 with an CO2 sensor attached. The Arduino script I'm using is quite old and worked for sending them over LoraWAN to TTN and is stable. I added an Wifi-Client to read data more frequently when an Wifi is near. The data get send over to my server via MQTT. Now after some time the device stops sending; I think it's just a few hours working. (I used libraries)

Now my question - how do I find the problem? I mean it has something to be the memory, doesn't it? The code is quite long so I would prefer to find the actual problem on my own - but how? My idea was to send debug messages by MQTT, hooking up a client to save all the MQTT messages so I can view the last messages the device sent. (I'm not even sure if the device fully stops or just loses connection etc). This whole thing sound like a tedious problem as I'd have to wait a few hours after every code fix I'd have implemented..

Thank for any help!

  • Without any detail to work with, there's not much help I can give. If your sensor is being read periodically, take out the delay so you're spamming output as quickly as possible. Hopefully, you'll get to the error more quickly that way. Remove every call to malloc() in your code - it leads to unavoidable fragmentation problems. Without more info, there's not much more I can suggest. Commented Sep 30, 2021 at 9:00
  • @Sniggerfardimungus Okay I understand that. - I'm not using malloc in my code.
    – Standard
    Commented Sep 30, 2021 at 9:39
  • 4
    Add Serial.println(ESP.getFreeHeap()); to your sketch and monitoring the value the first step for debugging.
    – hcheung
    Commented Sep 30, 2021 at 10:41
  • 1
    @wernersbacher If you're doing string concatenation, you may not be allocating, but the concatenation methods are. If you're doing much of this, particularly if you're slowly growing a string over time, I'd say there is a strong possibility that you're fragmenting memory. What String library are you using and exactly what function are you using to do concatenation? Commented Oct 2, 2021 at 8:56
  • 1
    I'm using a String indeed to concat several values. If you using String.concat, take a look at this answer from me on how it could create the problem and how to avoid it.
    – hcheung
    Commented Oct 3, 2021 at 14:51


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.