0

So, I'm trying to create a system with the ESP32, that gathers info from a webserver into the ESP, but I'm having problems with the Json arrays.

Here's where I'm having problems, at the start os the Json part:

WiFiClient client;
char servername[]="api.openweathermap.org";  //O servidor que estamos 
    // conectando para pegar as informações do clima
String result;

int  counter = 60;

String weatherDescription ="";
String weatherLocation = "";
String Country;
float Temperature;
float Humidity;
float Pressure;

void setup()
{
    Serial.begin(115200);

    //Conectando
    Serial.println("Connecting");
    WiFi.begin(ssid, password); //Conectar ao servidor WIFI

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
    }

    Serial.println("Connected");
    delay(1000);
}

void loop()
{
    if(counter == 60) //Get new data every 10 minutes
    {
        counter = 0;
        delay(1000);
        getWeatherData();
    }
    else
    {
        counter++;
        delay(5000);
    }
}

void getWeatherData() //função cliente para mandar/receber GET request data.
{
    if (client.connect(servername, 80)) {  //Começar conexão, chekar conexão
        client.println("GET /data/2.5/weather?id="+CityID+"&units=metric&APPID="+APIKEY);
        client.println("Host: api.openweathermap.org");
        client.println("User-Agent: ArduinoWiFi/1.1");
        client.println("Connection: close");
        client.println();
    } 
    else {
        Serial.println("connection failed"); //error message if no client connect
        Serial.println();
    }

    while(client.connected() && !client.available())
        delay(1); //Esperar pela data

    while (client.connected() || client.available()) { //Conectado ou Data disponivel
        char c = client.read(); //Pega Bytes do ethernet buffer
        result = result+c;
    }

    client.stop(); //stop client
    result.replace('[', ' ');
    result.replace(']', ' ');
    Serial.println(result);

    //Json part
    char jsonArray [result.length()+1];
    result.toCharArray(jsonArray,sizeof(jsonArray));
    jsonArray[result.length() + 1] = '\0';

    StaticJsonBuffer<1024> json_buf;
    JsonObject &root = json_buf.parseObject(jsonArray);
    //StaticJsonDocument<1024> json_buf;
    //deserializeJson(root, jsonArray);

    if (!root.success()) {

        Serial.println("parseObject() failed");
    }

    String location = root["name"];
    String country = root["sys"]["country"];
    float temperature = root["main"]["temp"];
    float humidity = root["main"]["humidity"];
    String weather = root["weather"]["main"];
    String description = root["weather"]["description"];
    float pressure = root["main"]["pressure"];

    weatherDescription = description;
    weatherLocation = location;
    Country = country;
    Temperature = temperature;
    Humidity = humidity;
    Pressure = pressure;

    Serial.println(weatherLocation);
    Serial.println(weatherDescription);
    Serial.println(Country);
    Serial.println(Temperature);
}

Here's where I'm having issues:

//Json part
char jsonArray [result.length()+1];
result.toCharArray(jsonArray,sizeof(jsonArray));
jsonArray[result.length() + 1] = '\0';

StaticJsonBuffer<1024> json_buf;
JsonObject &root = json_buf.parseObject(jsonArray);
//StaticJsonDocument<1024> json_buf;
//deserializeJson(root, jsonArray);

if (!root.success()) {
    Serial.println("parseObject() failed");
}

String location = root["name"];
String country = root["sys"]["country"];
float temperature = root["main"]["temp"];
float humidity = root["main"]["humidity"];
String weather = root["weather"]["main"];
String description = root["weather"]["description"];
float pressure = root["main"]["pressure"];

The error:

StaticJsonBuffer is a class from ArduinoJson 5. Please see arduinojson.org/upgrade to learn how to 
upgrade your program to ArduinoJson version 6

I looked into the documentation, but didn't have any success. https://arduinojson.org/v6/doc/upgrade/

2 Answers 2

1

Rename StaticJsonBuffer to StaticJsonDocument.

https://arduinojson.org/v6/doc/upgrade/ states

As the JsonBuffer, there are two versions of the JsonDocument.

The first is StaticJsonDocument, which is the equivalent of StaticJsonBuffer:

// ArduinoJson 5
StaticJsonBuffer<256> jb;

// ArduinoJson 6
StaticJsonDocument<256> doc;

There is an example for how to use StaticJsonDocument at https://arduinojson.org/v6/example/parser/

Try:

StaticJsonDocument<1024> root;
deserializeJson(root, jsonArray);
Sign up to request clarification or add additional context in comments.

Comments

0

To convert ArduinoJson v5 to ArduinoJson v6 for Static JsonObject.

For ArduinoJson v5.x

StaticJsonBuffer<1024> json_buf;
JsonObject &root = json_buf.parseObject(jsonArray);

if (!root.success())
{
 Serial.println("parseObject() failed");
}

For ArduinoJson v6.0

StaticJsonDocument<1024> root;
DeserializationError error = deserializeJson(root, jsonArray);

if (error) {
  Serial.print("deserializeJson() failed with code ");
}

The root now contains the deserialised JsonObject. Rest of your codes remains the same.

All the information for converting v5 syntax to v6 can be found at https://arduinojson.org/v6/doc/upgrade/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.