Hello,
I am programming an IoT device that uses ESP8266 microcontroller.
The thing is that in the code I am building a message that is sent to a MQTT broker. This message's size is variable and it depends on the number of sensors connected to the device.
I am actually using dynamic memory allocation:
String *data = new String(buildMessageFromModbusData(associatedDevices, numberOfConnectedNodes, currentUnixTime));
And I am also using an SD card to retrieve unsent values, so I append this SD's message to the one just requested to the sensors:
if (!data->isEmpty())
{
String *sdData = new String();
if (sdStorage && storedData)
{
Serial1.println(F("Retrieving stored data from SD card..."));
*sdData = SDCardManager::readFile();
if (!sdData->isEmpty())
{
*data = *sdData + "," + *data; // sdData and data sent together as one JSON
}
At the end of the code, I am destroying the object using delete function.
After all this, my output is:
Retrieving stored data from SD card...
[String] '{"id":1,"t ... 29,0.36000': Reallocating large String(143 -> 144 bytes)
[String] '{"id":1,"t ... ,39.139999': Reallocating large String(159 -> 160 bytes)
[String] '{"id":1,"t ... 999939]} ,': Reallocating large String(165 -> 256 bytes)
Message: {"id":1,"time":1700105400,"data":[229.3999939,0,0,0,0,1,50,60.99000168,0.029999999,61.02000046,1.470000029,39.5,40.97000122,1.470000029,0.360000014,0,39.13999939]} ,{"id":1,"time":1700131960,"data":[225,0,0,0,0,1,50,61.02999878,0.029999999,61.05999756,41]}
So apparently I have some error I am not being able to spot. Can you help me out?
Thanks!