玩转 ESP8266 + ArduinoJSON库(V6版本)
作者:互联网
玩转 ESP8266 + ArduinoJSON库(V6版本)
- 好多人在制作气象站的时候,可能遇到过json数据不知道如何处理的问题,今天专门拿气象json数据为例来介绍是如何处理的。
一.以和风气象数据为例:
材料:和风官网提供的API数据;
请求URL
实况天气 HTTP GET
- 开发版 https://devapi.qweather.com/v7/weather/now?{请求参数}
3天预报 HTTP GET - 开发版 https://devapi.qweather.com/v7/weather/3d?{请求参数}
(关于账号和apikey自己申请,自备,每个人可以免费注册,申请获得3个免费的apikey。)
实况天气
1.准备好的访问的api:
https://devapi.qweather.com/v7/weather/now?location=101010100&key=xxx
// 请将示例请求URL中的KEY更换成你自己的KEY
2.将api复制粘贴到浏览器中,回车;
返回的数据:
{"code":"200","updateTime":"2021-02-15T13:26+08:00","fxLink":"http://hfx.link/2ax1","now":{"obsTime":"2021-02-15T13:00+08:00","temp":"3","feelsLike":"-2","icon":"100","text":"晴","wind360":"225","windDir":"西南风","windScale":"3","windSpeed":"16","humidity":"20","precip":"0.0","pressure":"1029","vis":"30","cloud":"0","dew":"-18"},"refer":{"sources":["Weather China"],"license":["no commercial use"]}}
3.将以上数据拷贝下来;
- 利用Arduinojson官网提供的在线数据转换;网址
- 选择好版本之后,点击类型选择;
- 至于Mode和Input type不知道如何选择,就默认即可,然后点击
NEXT:JSON
- 将刚刚拷贝的数据,粘贴到Input方框里面,然后点击,右下角的
NEXT:Size
- 这里会计算json数据的大小,这个信息可以帮助我们申请存放json的内存大小,相当的方便,一目了然。
- 点击右下角的:NEXT:Program
- 到这里,网站已经帮我们序列化并生成好了代码
// Stream& input;
StaticJsonDocument<768> doc;
DeserializationError error = deserializeJson(doc, input);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
const char* code = doc["code"]; // "200"
const char* updateTime = doc["updateTime"]; // "2021-02-15T13:26+08:00"
const char* fxLink = doc["fxLink"]; // "http://hfx.link/2ax1"
JsonObject now = doc["now"];
const char* now_obsTime = now["obsTime"]; // "2021-02-15T13:00+08:00"
const char* now_temp = now["temp"]; // "3"
const char* now_feelsLike = now["feelsLike"]; // "-2"
const char* now_icon = now["icon"]; // "100"
const char* now_text = now["text"]; // "晴"
const char* now_wind360 = now["wind360"]; // "225"
const char* now_windDir = now["windDir"]; // "西南风"
const char* now_windScale = now["windScale"]; // "3"
const char* now_windSpeed = now["windSpeed"]; // "16"
const char* now_humidity = now["humidity"]; // "20"
const char* now_precip = now["precip"]; // "0.0"
const char* now_pressure = now["pressure"]; // "1029"
const char* now_vis = now["vis"]; // "30"
const char* now_cloud = now["cloud"]; // "0"
const char* now_dew = now["dew"]; // "-18"
const char* refer_sources_0 = doc["refer"]["sources"][0]; // "Weather China"
const char* refer_license_0 = doc["refer"]["license"][0]; // "no commercial use"
- 我们并不需要全部拷贝下来,只需要拷贝我们所需要的信息即可。
例如:
StaticJsonDocument<768> doc;//用来申请存放json数据的内存容量
JsonObject now = doc["now"];//给存放数据容器定义一个名字。
const char* now_temp = now["temp"]; // "3"//查找关键字信息,下面同是,不做赘述。
const char* now_feelsLike = now["feelsLike"]; // "-2"
const char* now_icon = now["icon"]; // "100"
const char* now_text = now["text"]; // "晴"
const char* now_wind360 = now["wind360"]; // "225"
const char* now_windDir = now["windDir"]; // "西南风"
const char* now_windScale = now["windScale"]; // "3"
const char* now_windSpeed = now["windSpeed"]; // "16"
const char* now_humidity = now["humidity"]; // "20"
const char* now_precip = now["precip"]; // "0.0"
const char* now_pressure = now["pressure"]; // "1029"
const char* now_vis = now["vis"]; // "30"
const char* now_cloud = now["cloud"]; // "0"
const char* now_dew = now["dew"]; // "-18"
- 以上获取的数据基本可以满足我们所需的相关数据了,至于如何在程序里面被调用,那就看你自己怎么写了。
- 这里给出相关的GET函数参考;
void HeFeng::doUpdateCurr(CurrentData *data, String HEFENG_KEY, String HEFENG_LOCATION) { //获取实时天气状况
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setInsecure();
HTTPClient https;
String url = "https://devapi.qweather.com/v7/weather/now?lang=en&location=" + HEFENG_LOCATION + "&key=" + HEFENG_KEY + "&gzip=n";//V7版本
Serial.print("[HTTPS] begin...now\n");
if (https.begin(*client, url)) { // HTTPS
// start connection and send HTTP header
int httpCode = https.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String json = https.getString();
Serial.println(json); //打印json数据
EEPROM.begin(96);
DynamicJsonDocument doc1(780);
deserializeJson(doc1, json);
JsonObject root = doc1["now"];
byte now_temp = root["temp"];//获取当天温度
byte Read_temp = EEPROM.read(30);
if (Read_temp != now_temp) {
EEPROM.write(30, now_temp);
EEPROM.end(); //写入flash,并且释放内存空间
}
Serial.println(now_temp, DEC);
data->tmp = now_temp;
String now_feelsLike = root["feelsLike"];
//Serial.println(now_feelsLike);
data->fl = now_feelsLike;//获取体感温度
byte now_humidity = root["humidity"];//获取湿度
byte Read_humidity = EEPROM.read(40);
if (Read_humidity != now_humidity) {
EEPROM.write(40, now_humidity);
EEPROM.commit();
EEPROM.end(); //写入flash,并且释放内存空间
}
Serial.println(now_humidity, DEC);
data->hum = now_humidity;
byte now_windScale = root["windScale"];//获取风力大小
byte Read_windScale = EEPROM.read(40);
if (Read_windScale != now_windScale) {
EEPROM.write(50, now_windScale);
EEPROM.commit();
}
Serial.println(now_windScale, DEC);
data->wind_sc = now_windScale;
String now_icon = root["icon"];//获取天气图标代码
char now_icon0[4];
strcpy(now_icon0, now_icon.c_str()); //把String now_icon赋值给char now_icon0;
int address = 80;
for (int i = 0; i < strlen(now_icon0); i++) { //now_icon0长度传递给eeprom寄存address;
address++;
EEPROM.write(address, now_icon0[i]);
}
EEPROM.commit();
EEPROM.end(); //写入flash,并且释放内存空间
String meteoconIcon = getMeteoconIcon(now_icon);
String now_text = root["text"];
Serial.println(now_text);
data->cond_txt = now_text;
data->iconMeteoCon = meteoconIcon;
void clear();//清除JsonDocument并释放内存空间
}
} else {
EEPROM.begin(96);
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
byte now_temp0 = EEPROM.read(30);
data->tmp = now_temp0;
EEPROM.commit();
String temp0 = "Temp:" + now_temp0;
Serial.println(temp0);
//data->tmp = 0;
data->fl = "-1";
byte hum0 = EEPROM.read(40);
data->hum = hum0;
EEPROM.commit();
String hum1 = "Hum:" + hum0;
Serial.println(hum1);
//data->hum = 0;
byte wind_sc0 = EEPROM.read(50);
data->wind_sc = wind_sc0;
EEPROM.commit();
String wind_SC = "Wind:" + wind_sc0;
Serial.println(wind_SC);
int address = 80;
char now_icon1[4];
for (int i = 0; i < 3; i++) { //now_icon0长度传递给eeprom寄存address;
address++;
now_icon1[i] = EEPROM.read(address );
}
EEPROM.end(); //写入flash,并且释放内存空间
String meteoconIcon = getMeteoconIcon(now_icon1);
data->iconMeteoCon = meteoconIcon;
data->cond_txt = "eeprom";
}
https.end();
} else {
EEPROM.begin(96);
Serial.printf("[HTTPS] Unable to connect\n");
byte now_temp0 = EEPROM.read(30);
data->tmp = now_temp0;
EEPROM.commit();
Serial.println("Temp:" + now_temp0);
//data->tmp = 0;
data->fl = "-1";
byte hum0 = EEPROM.read(40);
data->hum = hum0;
EEPROM.commit();
//EEPROM.end(); //写入flash,并且释放内存空间
Serial.println("Hum0:" + hum0);
//data->hum = 0;
byte wind_sc0 = EEPROM.read(50);
data->wind_sc = wind_sc0;
EEPROM.commit();
// EEPROM.end(); //写入flash,并且释放内存空间
Serial.println("wind_sc0:" + wind_sc0);
// data->wind_sc = 0;
data->cond_txt = "eeprom";//无网络就将气象名称显示eeprom字样以区别有网络状态
int address = 80;
char now_icon1[4];
for (int i = 0; i < 3; i++) { //now_icon0长度传递给eeprom寄存address;
address++;
now_icon1[i] = EEPROM.read(address);
}
EEPROM.commit();//提交
EEPROM.end(); //写入flash,并且释放内存空间
String meteoconIcon = getMeteoconIcon(now_icon1);
data->iconMeteoCon = meteoconIcon;
}
}
标签:const,ESP8266,char,V6,Serial,now,data,ArduinoJSON,EEPROM 来源: https://blog.csdn.net/weixin_42880082/article/details/113815396