ESP32/ESP8266通过HTTP使用Arduino_JSON获取并解析json数据
- ✨本示例所调用的为yytianqi API数据接口.
- 📌相关API数据接口::
http://www.yytianqi.com/api.html
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/167337da777812b863f25807f30f38ea.png)
🏁请求方式:HTTP GET
- 🌿城市ID请求地址:
http://api.yytianqi.com/aqi?city=CH010100&key=pb7276fgpr5bki02
- 🌿城市IP请求地址:
http://api.yytianqi.com/aqi?city=123.113.202.176&key=pb7276fgpr5bki02
- 🌿城市经纬度请求地址:
http://api.yytianqi.com/aqi?city=39.93,116.40&key=pb7276fgpr5bki02
🛠所需库
- 🔖
Arduino_JSON
是基于cJSON
,和ArduinoJson
不同,ArduinoJson
是一个C++
的JSON库。
- 🔧
Arduino_JSON
:https://github.com/arduino-libraries/Arduino_JSON
📝程序代码
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#endif
#include <Arduino_JSON.h>
const char* ssid = "######";
const char* password = "*******";
String YYWeatherApiKey = "填写API Key";
String cityCode = "CH250303";
unsigned long lastTime = 0;
unsigned long timerDelay = 32000;
String jsonBuffer;
void setup() {
Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
if(WiFi.status()== WL_CONNECTED){
String serverPath = "http://api.yytianqi.com/observe?city=" + cityCode + "&key=" + YYWeatherApiKey;
jsonBuffer = httpGETRequest(serverPath.c_str());
JSONVar myObject = JSON.parse(jsonBuffer);
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON object = ");
Serial.println(myObject);
Serial.print("City: ");
Serial.println((const char*)myObject["data"]["cityName"]);
Serial.print("Temperature: ");
Serial.print((const char*)myObject["data"]["qw"]);
Serial.println("°C");
Serial.print("Humidity: ");
Serial.print((const char*)myObject["data"]["sd"]);
Serial.println(" %");
Serial.print("Weather: ");
Serial.println((const char*)myObject["data"]["tq"]);
Serial.print("Wind: ");
Serial.println((const char*)myObject["data"]["fl"]);
Serial.print("Wind Direction: ");
Serial.println((const char*)myObject["data"]["fx"]);
Serial.println("");
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
String payload = "";
http.begin(client, serverName);
int httpCode = http.GET();
if (httpCode>0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
payload = http.getString();
}
else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
return payload;
}
使用 1.0 版本的库 ESP8266WiFi 在文件夹: C:\Users\Administrator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.1\libraries\ESP8266WiFi
使用 1.2 版本的库 ESP8266HTTPClient 在文件夹: C:\Users\Administrator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.1\libraries\ESP8266HTTPClient
使用 0.2.0 版本的库 Arduino_JSON 在文件夹: C:\Users\Administrator\Documents\Arduino\libraries\Arduino_JSON
使用 2.0.0 版本的库 WiFi 在文件夹: C:\Users\Administrator\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.7\libraries\WiFi
使用 2.0.0 版本的库 HTTPClient 在文件夹: C:\Users\Administrator\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.7\libraries\HTTPClient
使用 2.0.0 版本的库 WiFiClientSecure 在文件夹: C:\Users\Administrator\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.7\libraries\WiFiClientSecure
使用 0.2.0 版本的库 Arduino_JSON 在文件夹: C:\Users\Administrator\Documents\Arduino\libraries\Arduino_JSON
"C:\\Users\\Administrator\\AppData\\Local\\Arduino15\\packages\\esp32\\tools\\riscv32-esp-elf-gcc\\esp-2021r2-patch5-8.4.0/bin/riscv32-esp-elf-size" -A "d:\\arduino\\MyHexDir/YYweather.ino.elf"
项目使用了 746662 字节,占用了 (56%) 程序存储空间。最大为 1310720 字节。
全局变量使用了39868字节,(12%)的动态内存,余留287812字节局部变量。最大为327680字节。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dbf859595c9f28ff26bc3ad3d43e4f6b.png)