1

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!

Standard
  • 111
  • 2
  • 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. – Sniggerfardimungus Sep 30 '21 at 09:00
  • @Sniggerfardimungus Okay I understand that. - I'm not using malloc in my code. – Standard Sep 30 '21 at 09:39
  • @Juraj I'm using a String indeed to concat several values, then I convert this to an chart array. On every sensor refresh the string gets re-set again. – Standard Sep 30 '21 at 09:39
  • 4
    Add `Serial.println(ESP.getFreeHeap());` to your sketch and monitoring the value the first step for debugging. – hcheung Sep 30 '21 at 10:41
  • @wernersbacher there’s a relevant post regarding use of strings you should have a look through. https://arduino.stackexchange.com/questions/85157/using-string-instead-of-c-string-yet-another-attempt-to-touch-a-loaded-issue (Arduino strings are a bit of a rite of passage in the Arduino world - causes a lot of discussion. The referenced answers are a solid intro to the issues and resolutions) – RowanP Oct 01 '21 at 09:21
  • 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? – Sniggerfardimungus Oct 02 '21 at 08:56
  • report to the server more often to make it fail sooner. I would also bet on Strings. I have some older stuff I should but never have rewritten that also hangs after a long time; I have it reboot the ESP every few hours to avoid it, and it reboots in seconds, so i never miss my 5-min reporting interval (which is why i've never gotten around to rewritting the strings)... – dandavis Oct 02 '21 at 19:01
  • 1
    `I'm using a String indeed to concat several values`. If you using String.concat, take a look at this [answer](https://stackoverflow.com/a/61144973/4902099) from me on how it could create the problem and how to avoid it. – hcheung Oct 03 '21 at 14:51

0 Answers0