ESP32 连接到天气 API 以使用 JSON 数组获取信息
ESP32 Connect to Weather API to get info with JSON array
所以,我正在尝试使用 ESP32 创建一个系统,从网络服务器收集信息到 ESP,但我遇到了 Json 数组的问题。
这里是我遇到问题的地方,在开头 os 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] = '[=12=]';
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);
}
这是我遇到问题的地方:
//Json part
char jsonArray [result.length()+1];
result.toCharArray(jsonArray,sizeof(jsonArray));
jsonArray[result.length() + 1] = '[=13=]';
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"];
错误:
StaticJsonBuffer is a class from ArduinoJson 5. Please see arduinojson.org/upgrade to learn how to
upgrade your program to ArduinoJson version 6
我查看了文档,但没有任何成功。 https://arduinojson.org/v6/doc/upgrade/
将 StaticJsonBuffer
重命名为 StaticJsonDocument
。
https://arduinojson.org/v6/doc/upgrade/ 州
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;
有一个如何使用 StaticJsonDocument
的例子
https://arduinojson.org/v6/example/parser/
尝试:
StaticJsonDocument<1024> root;
deserializeJson(root, jsonArray);
为静态 JsonObject 将 ArduinoJson v5 转换为 ArduinoJson v6。
对于 ArduinoJson v5.x
StaticJsonBuffer<1024> json_buf;
JsonObject &root = json_buf.parseObject(jsonArray);
if (!root.success())
{
Serial.println("parseObject() failed");
}
ArduinoJson v6.0
StaticJsonDocument<1024> root;
DeserializationError error = deserializeJson(root, jsonArray);
if (error) {
Serial.print("deserializeJson() failed with code ");
}
root
现在包含反序列化的 JsonObject。您的其余代码保持不变。
将 v5 语法转换为 v6 的所有信息都可以在 https://arduinojson.org/v6/doc/upgrade/
找到
所以,我正在尝试使用 ESP32 创建一个系统,从网络服务器收集信息到 ESP,但我遇到了 Json 数组的问题。
这里是我遇到问题的地方,在开头 os 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] = '[=12=]';
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);
}
这是我遇到问题的地方:
//Json part
char jsonArray [result.length()+1];
result.toCharArray(jsonArray,sizeof(jsonArray));
jsonArray[result.length() + 1] = '[=13=]';
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"];
错误:
StaticJsonBuffer is a class from ArduinoJson 5. Please see arduinojson.org/upgrade to learn how to
upgrade your program to ArduinoJson version 6
我查看了文档,但没有任何成功。 https://arduinojson.org/v6/doc/upgrade/
将 StaticJsonBuffer
重命名为 StaticJsonDocument
。
https://arduinojson.org/v6/doc/upgrade/ 州
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;
有一个如何使用 StaticJsonDocument
的例子
https://arduinojson.org/v6/example/parser/
尝试:
StaticJsonDocument<1024> root;
deserializeJson(root, jsonArray);
为静态 JsonObject 将 ArduinoJson v5 转换为 ArduinoJson v6。
对于 ArduinoJson v5.x
StaticJsonBuffer<1024> json_buf;
JsonObject &root = json_buf.parseObject(jsonArray);
if (!root.success())
{
Serial.println("parseObject() failed");
}
ArduinoJson v6.0
StaticJsonDocument<1024> root;
DeserializationError error = deserializeJson(root, jsonArray);
if (error) {
Serial.print("deserializeJson() failed with code ");
}
root
现在包含反序列化的 JsonObject。您的其余代码保持不变。
将 v5 语法转换为 v6 的所有信息都可以在 https://arduinojson.org/v6/doc/upgrade/
找到