ArduinoJson icon indicating copy to clipboard operation
ArduinoJson copied to clipboard

Value is always zero

Open rwb196884 opened this issue 1 year ago • 8 comments

    //JsonDocument jDoc; // This is the new way of doing it in v7 and it doesn't work; applied is alyways zero.
    StaticJsonDocument < 64 > jDoc;
    if (wifi_client.available()) {
      deserializeJson(jDoc, wifi_client);
      Serial.println("RECEIVED");
      serializeJson(jDoc, Serial);
      temp = jDoc["temp"];
      unsigned long applied = jDoc["applied"];
      Serial.print(t_current);
      Serial.print(" -> ");
      Serial.println(temp);
      Serial.print("applied: ");
      Serial.println(applied);

prints

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

but with JsonDocument it prints

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

rwb196884 avatar Sep 17 '24 21:09 rwb196884

Hi @rwb196884,

Do you mean that the code works with ArduinoJson 6 but not with ArduinoJson 7, or that it works with ArduinoJson 7 but only when using StaticJsonDocument?

Best regards, Benoit

bblanchon avatar Sep 18 '24 15:09 bblanchon

Version 7 is installed.

This is the latest fuckup.

      StaticJsonDocument<64> jDocFlow;
      jDocFlow["t_flow"] = t_flow;
      jDocFlow["t_return"] = t_return;
      jDocFlow["foo"] = -1;

      char payload[64];
      serializeJson(jDocFlow, payload);
      bool p = false;
      while(!p) {
        connect_wifi();
        connect_ap();
        connect_mqtt();
        p = mqtt_client.publish(mqtt_topic_temp, payload, true);
        if (!p) {
          blink(6);
        } // Error 6: couldn't report temperatures to MQTT.
        else {
          Serial.println("--published temp--");
          serializeJson(jDocFlow, Serial);
          Serial.println();
        }
      }

outputs

--published temp--
{}

rwb196884 avatar Sep 19 '24 21:09 rwb196884

What does the ArduinoJson Troubleshooter say?

bblanchon avatar Sep 20 '24 15:09 bblanchon

Another example

    StaticJsonDocument<128> jDocFlow;
    jDocFlow["t_flow"] = 11;
    jDocFlow["t_return"] = 12;
    char payload[128];
    serializeJson(jDocFlow, Serial);
      Serial.print("serializeJson overflowed: ");
    Serial.println(jDocFlow.overflowed());

Outputs

{} serializeJson overflowed: 1

rwb196884 avatar Sep 24 '24 19:09 rwb196884

Getting some junk from Serial.print

Reported temperatures: {"tt�low": 28, "t_re�r!�

I wonder if the board is fucked?

rwb196884 avatar Sep 24 '24 20:09 rwb196884

This last issue suggests a stack overflow. Reduce the size of the stack variables. Start by removing char payload[128].

bblanchon avatar Sep 26 '24 07:09 bblanchon

How do you determine whether something is stored in the heap on on the stack?

I imagine it's something to do with where you declare the variable -- either inside a function of outside of any function -- but I can't find any documentation.

rwb196884 avatar Oct 10 '24 20:10 rwb196884

There is no quick answer to this question, unfortunately. I invite you to check out the "Missing C++ Course" in my book or any other C++ course.

bblanchon avatar Oct 13 '24 07:10 bblanchon