Arduino randomly stops working after a few days

Another Update - Potentially found the issue!!

Hey all, im beginning to believe it really is a memory problem with the mega. Implementing a soft-reset every 12 hours has been working, but not well enough (still crashes sometimes).

Testing & Results

I wanted to stress test and find where exactly in the code the program stops (by spamming the MQTT broker with a few messages from every function). So that's what I've been doing over the last week. After going through all the logs here is the summary.

  • 5 out of 6 tests ended in the CheckDHT() function
  • 2 tests ended whilst reading the sensor data into local variables h and t
  • 3 tests ended whilst assigning h and t to the lastFrontTemp (or back/humidity) Arrays
void CheckDHT(DHT sen, int i) {
// 2 OF THE TESTS ENDED HERE (either reading h or t)
 float h = sen.readHumidity();     // Get humidity %
 float t = sen.readTemperature();  // Get temperature (celcius)

// THE OTHER 3 TESTS ENDED SOMEWHERE BELOW
 // This loop replaces delay(100); 
 lastTime = millis();
 while (millis() < lastTime + 100) { /*do nothing*/ } 

 // IF h AND t BOTH HAVE VALUE:
 if (!isnan(h) && !isnan(t)) {
   if (i >= 3) {  // if index >= 3 then it is a back sensor
     i -= 3;
     lastBackHumidity[i] = h;
     lastBackTemp[i] = t;
   } else {  // if not, use normal index as its a front sensor
     lastFrontHumidity[i] = h;
     lastFrontTemp[i] = t;
   }
 } else { 
   if (i >= 3) {  // if index >= 3 then it is a back sensor
     i -= 3;
     lastBackHumidity[i] = 999.999;
     lastBackTemp[i] = 999.999;
   } else {  // if not, use normal index as its a front sensor
     lastFrontHumidity[i] = 999.999;
     lastFrontTemp[i] = 999.999;
   }
  }
}
  • 1 test ended in the CompilePayload() function whilst creating the JSON object

The code stopped somewhere in here:

// Snippet from CompilePayload() Function
JsonDocument doc;
doc["type"] = "new_data";
doc["device"] = deviceID;
JsonObject data = doc.createNestedObject("data");
JsonObject temperature_sensors = data.createNestedObject("temperature_sensors");  
JsonObject humidity_sensors = data.createNestedObject("humiditiy_sensors");       
if (debug) { doc["free_ram"] = stackAvailable(); }   

So... what's the cause? Memory Fragmentation, Arrays, MQTT?

I think it has to be either my use of Arrays, as 3 of 6 tests did fail in the area of code where I assign values to arrays, however I think it could also be Memory fragmentation which would explain why it crashes when creating the JsonDocument. However, I am starting to believe that the Arduino MQTT Client is our main culprit, let me explain:

My methodology for my "tests" are to send an MQTT messages (containing where in code it is, as one does with print statement debugging :slight_smile: , and also data from stackAvailable() - which showed memory was fine all the way until it crashes). However this method would mean the arduino is sending about 10 messages over MQTT a second.

Whilst running this modified version of my code - The Arduino would shut off after ~20-30 minutes instead of the usual 12+ hours, which means the extra usage of MQTT is 100% effecting the arduino (for the print-logs over MQTT I do not use JSON, instead I just send raw char[] like client.print("hello world"))

Because the stackAvailable() function showed memory to be fine, I think the problem is Memory Fragmentation (assuming that it isn't detectable via the stackAvailable() function, from what I've read online). The use of MQTT and constantly assigning data to different arrays I assume is what's causing memory issues. Otherwise I have no idea :frowning:

As I'm still pretty clueless about Memory Fragmentation, if anyone has any knowledge on how I can improve my code or any other ideas/comments on what could be the problem/fixes, PLEASE HELP :upside_down_face: