I have a bug!
Four days ago the microcontroller got stuck. The LED was on, but it was no longer transmitting. So I hit reset and continued testing. One of the things I added was a watchdog trigger. The CubeCell board has a build in watchdog – and the only thing you need to do is switch it on – how cool is that?
I did not think back to the problem until today suddenly the board rebooted. I noticed because the LED I configured to drain the battery was off – the default after reboot. Looking at the number of messages it sent, I noticed it always rebooted after 165 or 164 messages. This is to much of a coincidence to be random. Even if you are not a seasoned developer (reminder: I am a network engineer, not a software developer) this smells like a memory leak. The CubeCell has only 16kb of RAM and that fills up quickly if you are not careful.
But my code does never allocate memory! So I checked when these reboots first occurred and, as I use git to version my code, I found out it started after adding a library to query the ADS1115 A/D converter. As the CubeCell goes into deep sleep so often and powers down the sensors I simply re-initialized the sensors every time it woke up. Checking the ADS1115 library I was using I found the follwing code:
void Adafruit_ADS1X15::begin(uint8_t i2c_addr, TwoWire *wire) {
m_i2c_dev = new Adafruit_I2CDevice(i2c_addr, wire);
m_i2c_dev->begin();
}
So that means each time I call the begin() function a memory object is allocated. And after 165 times free memory is gone. I made sure begin() was only called once (I was a bit concerned about the deep sleep) and it now works fine.