I have a program that I am planning to implement publishing of json data with, but I have encountered a use case that I can’t find well documented, or just may not be possible.
Problem description
My use case looks something like this
int publishBufferLen = 678;
// make a buffer on the heap to store our sensor readings until we are ready to publish
char *publishBuffer = new char[publishBufferLen];
memset(publishBuffer, 0, publishBufferLen);
// add a reading to the buffer if there is room, else publish readings
void addReadingToBuffer(float value)
{
if (sizeof(publishBuffer) == 0) // if the buffer is empty
{
JSONBufferWriter writer(testJsonBuffer, jsonBufferLen);
writer.beginObject();
writer.name("d").beginArray();
writer.value(value);
writer.endArray();
writer.endObject();
}
else if (sizeof(publishBuffer) + sizeof(String(value)) < publishBufferLen) // append to the array
{
/**
This is where I would like to append to the array,
previously the data structure was defined as:
{
"d": [ ]
}
I would like to append a reading to the json buffer
{
"d": [ 123.123 ]
}
Like so
*/
}
else // the data wont fit in the buffer, publish what we have and create a new one
{
publish("test", publishBuffer, PRIVATE);
delete[] publishBuffer;
publishBuffer = new char[publishBufferLen];
addReadingToBuffer(value);
}
}
void loop()
{
delay(5000);
addReadingToBuffer(sensor.getReading());
}
Question
If possible, how can I use the JSONBufferWriter object to append this data to publishBuffer
in this way?