Hey @sn13TX,
I looked through your example code and nothing super obvious is jumping out to me. I do have a few suggestions:
-
I’d recommend adding a boolean in your code that gives you the ability to turn off serial debugging for when you deploy your device. I don’t think this would be causing memory problems, but at the very least it’s unnecessary processing.
-
I’d try the following trick to see if you can track down the memory problem.
#include <malloc.h>
void logMemoryUsage (const char *ctx_, bool enter_ = false) {
struct mallinfo mi = mallinfo();
Serial.print("[MEMORY][");
Serial.print((enter_ ? ">>>>" : "<<<<"));
Serial.print(ctx_);
Serial.print("] Allocated: ");
Serial.print(mi.uordblks, DEC);
Serial.println(" bytes");
}
The idea is you can call this function at the start and end of certain processes to see whether your memory allocation is going up unexpectedly.
To start I’d do something like…
void loop() {
logMemoryUsage(__FUNCTION__, true);
// everything
logMemoryUsage(__FUNCTION__, false);
}
When I add this code to our sensor tutorial, for example, I see the following output:
[MEMORY][<<<<loop] Allocated: 924 bytes
[MEMORY][>>>>loop] Allocated: 924 bytes
[INFO] {"req":"card.temp","crc":"0046:CE0D7EDD"}
[INFO] {"value":26.9375,"calibration":-1}
Temperature = 26.94 *C
Humidity = 48.29 %
[INFO] {"req":"note.add","file":"sensors.qo","sync":true,"body":{"temp":26.9375,"humidity":48.28680038452148},"crc":"0047:98CEE59A"}
[INFO] {"total":1}
[INFO] {"req":"env.get","name":"reading_interval","crc":"0048:B7D4A244"}
[INFO] {}
Delaying 15 seconds
[MEMORY][<<<<loop] Allocated: 924 bytes
[MEMORY][>>>>loop] Allocated: 924 bytes
[INFO] {"req":"card.temp","crc":"0049:CE0D7EDD"}
[INFO] {"value":26.9375,"calibration":-1}
Temperature = 26.94 *C
Humidity = 45.70 %
[INFO] {"req":"note.add","file":"sensors.qo","sync":true,"body":{"temp":26.9375,"humidity":45.69800186157227},"crc":"004A:172139B0"}
[INFO] {"total":1}
[INFO] {"req":"env.get","name":"reading_interval","crc":"004B:B7D4A244"}
[INFO] {}
Delaying 15 seconds
[MEMORY][<<<<loop] Allocated: 924 bytes
[MEMORY][>>>>loop] Allocated: 924 bytes
[INFO] {"req":"card.temp","crc":"004C:CE0D7EDD"}
[INFO] {"value":26.9375,"calibration":-1}
Temperature = 26.94 *C
Humidity = 45.62 %
[INFO] {"req":"note.add","file":"sensors.qo","sync":true,"body":{"temp":26.9375,"humidity":45.62229919433594},"crc":"004D:A8BE5351"}
[INFO] {"total":1}
[INFO] {"req":"env.get","name":"reading_interval","crc":"004E:B7D4A244"}
[INFO] {}
Delaying 15 seconds
If you add this debugging and see your memory allocation going up, you know you have a memory problem somewhere. From there you can add the logging in individual functions to try to track down specifically where the problem is at.
If you want a more sophisticated way of figuring this out, our firmware team recommends using unit tests based on Valgrind. Tools like that go above and beyond my understand of C, but if you have questions I can rope people who use it into this thread.
TJ