Play with ESP8266 + ArduinoJSON Library (V6 version)
- Many people may encounter the problem of how to deal with json data when making weather stations. Today, we take the weather json data as an example to introduce how to deal with it.
I Take Hefeng meteorological data as an example:
Material Science: API data provided by Hefeng official website;
Request URL
Live weather HTTP GET
- Development version https://devapi.qweather.com/v7/weather/now? {request parameters}
3-day forecast - Development version https://devapi.qweather.com/v7/weather/3d? {request parameters}
(about the account and apikey, you can apply for it yourself and provide it yourself. Everyone can register for free and apply for 3 free apikeys.)
Live weather
1. Prepared api for access:
https://devapi.qweather.com/v7/weather/now?location=101010100&key=xxx // Please replace the KEY in the sample request URL with your own KEY
2. Copy and paste the api into the browser and press enter;
Returned data:
{"code":"200","updateTime":"2021-02-15T13:26+08:00","fxLink":"http://hfx. Where it '' '' '' ''a one in a one in a one in a one in one one-in-a-2015-2015-a-time-at-00-08-00-a-time-a-time-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-one-in-a-one-in-a-a-one-in-a-one-in-a-a-a-time-to-a-time-to-a-a-2015-a-2015-a-2015-a-2015-a-a-15-time-a-a-13::00-00-00-00-00-a-time-time-time-00-00-00-00-00-00-00-00-00-00-a-a-time-in-a-a-a-a-a-a-time-a-a-a-time-a-time-time-time-time s ": [" weather China "]," license ": [" no commercial use "]}
3. Copy the above data;
- Use the online data conversion provided by Arduinojson official website; website
- After selecting the version, click type selection;
- As for the Mode and Input type, I don't know how to choose, just default, and then click NEXT:JSON
- Paste the data just copied into the Input box, and then click NEXT:Size in the lower right corner
- The size of json data will be calculated here. This information can help us apply for the memory size of json. It is quite convenient and clear at a glance.
- Click NEXT:Program in the lower right corner
- Here, the website has serialized and generated the code for us
// 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"]; // "Sunny" const char* now_wind360 = now["wind360"]; // "225" const char* now_windDir = now["windDir"]; // "Southwest wind" 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"
- We don't need to copy all of them. We just need to copy the information we need.
For example:
StaticJsonDocument<768> doc;// Memory capacity used to apply for storing json data: JsonObject now = doc["now"]// Define a name for the data container. const char* now_temp = now["temp"]; // "3" / / find keyword information, which is the same below, and will not be repeated. const char* now_feelsLike = now["feelsLike"]; // "-2" const char* now_icon = now["icon"]; // "100" const char* now_text = now["text"]; // "Sunny" const char* now_wind360 = now["wind360"]; // "225" const char* now_windDir = now["windDir"]; // "Southwest wind" 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" - The above data can basically meet the relevant data we need. As for how to be called in the program, it depends on how you write it.
- Here is the relevant GET function reference;
void HeFeng::doUpdateCurr(CurrentData *data, String HEFENG_KEY, String HEFENG_LOCATION) { //Get real-time weather conditions 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 version 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); //Print json data EEPROM.begin(96); DynamicJsonDocument doc1(780); deserializeJson(doc1, json); JsonObject root = doc1["now"]; byte now_temp = root["temp"];//Get the temperature of the day byte Read_temp = EEPROM.read(30); if (Read_temp != now_temp) { EEPROM.write(30, now_temp); //Write the value of lunarYear to bit 0 of EEPROM // EEPROM.commit(); EEPROM.end(); //Write to flash and free up memory space } Serial.println(now_temp, DEC); data->tmp = now_temp; String now_feelsLike = root["feelsLike"]; //Serial.println(now_feelsLike); data->fl = now_feelsLike;//Get somatosensory temperature byte now_humidity = root["humidity"];//Get humidity byte Read_humidity = EEPROM.read(40); if (Read_humidity != now_humidity) { EEPROM.write(40, now_humidity); EEPROM.commit(); EEPROM.end(); //Write to flash and free up memory space } Serial.println(now_humidity, DEC); data->hum = now_humidity; byte now_windScale = root["windScale"];//Get wind size 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"];//Get weather icon code char now_icon0[4]; strcpy(now_icon0, now_icon.c_str()); //String now_icon assigned to char now_icon0; int address = 80; for (int i = 0; i < strlen(now_icon0); i++) { //now_icon0 length is passed to eeprom deposit address; address++; EEPROM.write(address, now_icon0[i]); } EEPROM.commit(); EEPROM.end(); //Write to flash and free up memory space String meteoconIcon = getMeteoconIcon(now_icon); String now_text = root["text"]; Serial.println(now_text); data->cond_txt = now_text; data->iconMeteoCon = meteoconIcon; void clear();//Clear the JsonDocument and free up memory space } } 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(); //EEPROM.end(); // Write to flash and free up memory space 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 length is passed to eeprom deposit address; address++; now_icon1[i] = EEPROM.read(address ); } EEPROM.end(); //Write to flash and free up memory space 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(); // Write memory, and free flash space Serial.println("Hum0:" + hum0); //data->hum = 0; byte wind_sc0 = EEPROM.read(50); data->wind_sc = wind_sc0; EEPROM.commit(); // EEPROM.end(); // Write to flash and free up memory space Serial.println("wind_sc0:" + wind_sc0); // data->wind_sc = 0; data->cond_txt = "eeprom";//If there is no network, the meteorological name will be displayed with the words eeprom to distinguish the network status int address = 80; char now_icon1[4]; for (int i = 0; i < 3; i++) { //now_icon0 length is passed to eeprom deposit address; address++; now_icon1[i] = EEPROM.read(address); } EEPROM.commit();//Submit EEPROM.end(); //Write to flash and free up memory space String meteoconIcon = getMeteoconIcon(now_icon1); data->iconMeteoCon = meteoconIcon; } }