JsonDocument value is always zero

Code:

...
    StaticJsonDocument < 64 > jDoc;
    if (wifi_client.available()) {
      deserializeJson(jDoc, wifi_client);
      Serial.println("RECEIVED");
      serializeJson(jDoc, Serial);
      temp = jDoc["temp"];
      int applied = jDoc["applied"];
      Serial.print(t_current);
      Serial.print(" -> ");
      Serial.println(temp);
      Serial.print("applied: ");
      Serial.println(applied);
...

Output

RECEIVED
{"temp":60,"applied":41498}0 -> 60
applied: 0

Why does jDoc["applied"] return zero instead of the value in the received JSON?

Make applied of type long

41498 is larger than a signed int. You could also make it an unsigned int since they can go up to 65535.

Aha int is only two bytes here butmillis() is unsigned long -- which is only four bytes.

That's got it; thank you :slight_smile:

Spoke too soon; it now works sometimes.

RECEIVED
{"temp":60,"applied":327970}0 -> 60
applied: 0

and

      JsonDocument jDocFlow;
      jDocFlow["t_flow"] = t_flow;
      jDocFlow["t_return"] = t_return;
      Serial.println("--published temp--");
      serializeJson(jDocFlow, Serial);

prints

--published temp
{}

Noticed that I seem to be using version 7 not 6 of JSON therefore I've replaced StaticJsonDocument with JsonDocument.

OK, going back from JsonDocument to StaticJsonDocument<64> has made the value of applied come back.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.