Hi. I have this code bellow where I'm trying to assign values to a Json Object (root). In the first few iterations, the value of data is assigned correctly to the object. However after some time the JSON object does not get the new value. It keeps repeating the last value.
Any clues on why this is happening?
void loop()
{
BTserial.begin(ESP8266_BAUD);
BTserial.readBytes(data_length_char, 1); // data length
Serial.print("data_length: ");
int data_length = data_length_char[0];
Serial.println(data_length);
Serial.print("data: ");
char data[data_length * 2];
array_to_string(data_byte, data_length, data);
Serial.println(data);
root["bytesarray"] = data;
root.printTo(requestData);
Serial.println("requestData: " + requestData);
postRequest = "POST " + PATH + " HTTP/1.1\r\n" +
"Host: " + HOST + "\r\n" +
"Accept: *" + "/" + "*\r\n" +
"Content-Length: " + requestData.length() + "\r\n" +
"Content-Type: application/json\r\n" +
"\r\n" + requestData;
Serial.println("POSTREQUEST: " + String(postRequest.length()));
Serial.println(">>>>>>>>>>>>>>>");
}
void array_to_string(byte array[], unsigned int len, char buffer[])
{
for (unsigned int i = 0; i < len; i++)
{
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len * 2] = '\0';
}
This is the result I get:
data_length: data: 0201061AFF4C000215CA5ABA5F201751D143E60A582A54113500020003AF
requestData: {"bytesarray":"0201061AFF4C000215CA5ABA5F201751D143E60A582A54113500020003AF"}data_length: data: 1EFF060001092002D5B25DA76FA4A661FBD223ED190E0B2EA4EE764F4D6203
requestData: {"bytesarray":"1EFF060001092002D5B25DA76FA4A661FBD223ED190E0B2EA4EE764F4D6203"}data_length: data: 02010607FF4C0010020708
requestData: {"bytesarray":"02010607FF4C0010020708"}data_length: data: 0201061AFF4C000215CA5ABA5F201751D143E60A582A54113500010001A8
requestData: {"bytesarray":"02010607FF4C0010020708"}data_length: data: 0201061AFF4C000215E1F54E021E2344E09C3D512EB56ADEC900640064B9
requestData: {"bytesarray":"02010607FF4C0010020708"}data_length: data: 02010403039AFE16169AFE11A98138B739283257B0C5E6E9F4D8EE14C201
requestData: {"bytesarray":"02010607FF4C0010020708"}