Learning from previous post ESP32 -2432S028 LVGL Weather Station

From my previous post, solved by Max, I have attempted to understand Max’s solution and learn how it works.
Please be critical if my understanding isn’t strictly correct, I’ve learned quite a lot in the last two days that I hope it helps other ‘newbies’ along the way.
First I examined why the Working Code didn’t exactly work even though it compiled and built run-time without errors, which caused me no end of confusion?
Max was obviously correct (though completely unknown to me at the time why, and more to the point how to ‘look’ at the problem) that a ‘null’ character was the issue.

Looking at Max’s solution, I had no idea it was referring to JSON script (which I knew nothing about) until I read these web links:

After some hours reading and looking at the code I went through the following folders:
.pio/libdeps\esp32dev/ArduinoJson/StringExample/StringExample.ino

At lines 36 to 39 in that code it states the following:

// Unfortunately, the following doesn't work (issue #118): // sensor = doc["sensor"]; // <- error "ambiguous overload for 'operator='" // As a workaround, you need to replace by: sensor = doc["sensor"].as<String>();

Which matches Max’s solution

      ////Non Working Code
      ////temperature = String(doc["current"]["temperature_2m"]);
      ////humidity = String(doc["current"]["relative_humidity_2m"]);
      ////is_day = String(doc["current"]["is_day"]).toInt();
      ////weather_code = String(doc["current"]["weather_code"]).toInt();
// Working Code
      //// String::String(const char *cstr = "")
      //// constructors
      //// creates a copy of the initial value.
      //// if the initial value is null or invalid, or if memory allocation
      //// fails, the string will be marked as invalid (i.e. "if (s)" will
      //// be false).  
      ////temperature = String((const char *)doc["current"]["temperature_2m"]);
      ////humidity = String((const char *)doc["current"]["relative_humidity_2m"]);
      ////is_day = doc["current"]["is_day"].as<int>();
      ////weather_code = doc["current"]["weather_code"].as<int>();

      /***************************************
       * 
       * Code provided by Max from platformio
       * 
       ***************************************/
      //// ArduinoJson::V704PB2::JsonDocument doc Parse the JSON to extract the time
  //// As a workaround, you need to replace by:
 sensor = doc["sensor"].as<String>();

      float temp = doc["current"]["temperature_2m"].as<float>();
      int hum = doc["current"]["relative_humidity_2m"].as<int>();
      //// explicit String::String(float, unsigned int decimalPlaces = 2U)
      temperature = String(temp, 1);      // Create string 'temperature' e.g. "17.9"
      humidity = String(hum);             // Create string 'humidity' e.g. "74"
      //// ArduinoJson::V704PB2::JsonDocument doc Parse the JSON to extract the time
      is_day = doc["current"]["is_day"].as<int>();
      weather_code = doc["current"]["weather_code"].as<int>()
;

In conclusion, my understanding is that Max created a JSON “key” called temperature as type string.
The JSON “value” [current][temperature] of type float were “parsed” as the data in the correct format required by ArduinoJson::V704PB2::JsonDocument
Once Max had the correct string format, it was then converted to the two decimal format, and the same applies for the humidity solution.

I hope my understanding makes sense, and helps ‘newbies’ following or building this project!

1 Like