Cum să deserializați și să afișați date JSON pe un ESP8266

cod folosit in lectia de aici.

Language: C++

            #include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <EEPROM.h>
#include <ArduinoJson.h>


const char* data_input = "{\"turbineId\":\"TURB12345\","
                         "\"timestamp\":\"2024-05-20T10:30:00Z\","
                         "\"windSpeed\":12.5,"
                         "\"powerGenerated\":1500,"
                         "\"motorTemperature\":75,"
                         "\"operationalStatus\":\"active\","
                         "\"location\":{\"latitude\":45.6789,\"longitude\":23.4567},"
                         "\"maintenanceRequired\":false}";
JsonDocument doc;
DeserializationError error = deserializeJson(doc, data_input);

void setup() {
    Serial.begin(115200);
    while (!Serial) continue;
}

void loop() {
    if (error) {
        Serial.print(F("Failed to read JSON: "));
        Serial.println(error.c_str());
        return;
    } else {
        deserializeJson(doc, data_input);
        const char* turbineId = doc["turbineId"];
        const char* timestamp = doc["timestamp"];
        float windSpeed = doc["windSpeed"];
        int powerGenerated = doc["powerGenerated"];
        int motorTemperature = doc["motorTemperature"];
        const char* operationalStatus = doc["operationalStatus"];
        float latitude = doc["location"]["latitude"];
        float longitude = doc["location"]["longitude"];
        bool maintenanceRequired = doc["maintenanceRequired"];

        Serial.println("Deserialized JSON data:");
        Serial.print("Turbine ID: ");
        Serial.println(turbineId);
        Serial.print("Timestamp: ");
        Serial.println(timestamp);
        Serial.print("Wind Speed: ");
        Serial.println(windSpeed);
        Serial.print("Power Generated: ");
        Serial.println(powerGenerated);
        Serial.print("Motor Temperature: ");
        Serial.println(motorTemperature);
        Serial.print("Operational Status: ");
        Serial.println(operationalStatus);
        Serial.print("Location (Latitude, Longitude): ");
        Serial.print(latitude);
        Serial.print(", ");
        Serial.println(longitude);
        Serial.print("Maintenance Required: ");
        Serial.println(maintenanceRequired);
        delay(5000);
    }
}
        
Copied!