[英]ESP32 Connect to Weather API to get info with JSON array
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.所以,我正在尝试使用 ESP32 创建一个系统,将信息从网络服务器收集到 ESP,但我在使用 Json arrays 时遇到了问题。
Here's where I'm having problems, at the start os the Json part:这是我遇到问题的地方,在 Json 部分的开头:
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/
https://arduinojson.org/v6/doc/upgrade/
Rename StaticJsonBuffer
to StaticJsonDocument
.将
StaticJsonBuffer
重命名为StaticJsonDocument
。
https://arduinojson.org/v6/doc/upgrade/ states https://arduinojson.org/v6/doc/upgrade/状态
As the JsonBuffer, there are two versions of the JsonDocument.
作为JsonBuffer,有两个版本的JsonDocument。
The first is StaticJsonDocument, which is the equivalent of StaticJsonBuffer:
第一个是 StaticJsonDocument,它相当于 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/在https://arduinojson.org/v6/example/parser/有一个如何使用
StaticJsonDocument
的例子
Try:尝试:
StaticJsonDocument<1024> root;
deserializeJson(root, jsonArray);
To convert ArduinoJson v5 to ArduinoJson v6 for Static JsonObject.为 Static JsonObject 将 ArduinoJson v5 转换为 ArduinoJson v6。
For ArduinoJson v5.x对于 ArduinoJson v5.x
StaticJsonBuffer<1024> json_buf;
JsonObject &root = json_buf.parseObject(jsonArray);
if (!root.success())
{
Serial.println("parseObject() failed");
}
For ArduinoJson v6.0对于 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. root
现在包含反序列化的 JsonObject。 Rest of your codes remains the same.您的代码 Rest 保持不变。
All the information for converting v5 syntax to v6 can be found at https://arduinojson.org/v6/doc/upgrade/所有将 v5 语法转换为 v6 的信息都可以在https://arduinojson.org/v6/doc/upgrade/找到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.