**********************************************************************
项目名称/Project : 零基础入门学用物联网
程序名称/Program name : weather_forecast
团队/Team : 太极创客团队 / Taichi-Maker (www.taichi-maker.com)
作者/Author : CYNO朔
日期/Date(YYYYMMDD) : 20200601
程序目的/Purpose :
通过心知天气(www.seniverse.com)免费服务获取天气预报信息(三日)。
-----------------------------------------------------------------------
其它说明 / Other Description
心知天气API文档说明: https://www.seniverse.com/docs
本程序为太极创客团队制作的免费视频教程《零基础入门学用物联网 》中一部分。该教程系统的
向您讲述ESP8266的物联网应用相关的软件和硬件知识。以下是该教程目录页:
http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/
***********************************************************************/
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
const char* host = "api.seniverse.com"; // 将要连接的服务器地址
const int httpPort = 80; // 将要连接的服务器端口
// 心知天气HTTP请求所需信息
String reqUserKey = "XXXXXXX"; // 私钥
String reqLocation = "ip"; // 城市
String reqUnit = "c"; // 摄氏/华氏
String todaydate;
String todaydayweather;
int todaydayweathercode;
String todaynightweather;
int todaynightweathercode;
int todaytempmax;
int todaytempmin;
int todayhumity;
String nextdaydate;
String nextdaydayweather;
int nextdaydayweathercode;
String nextdaynightweather;
int nextdaynightweathercode;
int nextdaytempmax;
int nextdaytempmin;
int nextdayhumity;
// 利用ArduinoJson库解析心知天气响应信息
void parseInfo(WiFiClient client) {
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 3 * JSON_OBJECT_SIZE(14) + 860;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, client);
JsonObject results_0 = doc["results"][0];
JsonArray results_0_daily = results_0["daily"];
JsonObject results_0_daily_0 = results_0_daily[0];
todaydate = results_0_daily_0["date"].as<String>();//预报日期
todaydayweather = results_0_daily_0["text_day"].as<String>();//白天天气状态
todaydayweathercode = results_0_daily_0["code_day"].as<int>();
todaynightweather = results_0_daily_0["text_night"].as<String>();//晚上天气状态
todaynightweathercode = results_0_daily_0["code_night"].as<int>();
todaytempmax = results_0_daily_0["high"].as<int>();//最高温度
todaytempmin = results_0_daily_0["low"].as<int>();//最低温度
/*const char* results_0_daily_0_rainfall = results_0_daily_0["rainfall"];
const char* results_0_daily_0_precip = results_0_daily_0["precip"];
const char* results_0_daily_0_wind_direction = results_0_daily_0["wind_direction"];
const char* results_0_daily_0_wind_direction_degree = results_0_daily_0["wind_direction_degree"];
const char* results_0_daily_0_wind_speed = results_0_daily_0["wind_speed"];
const char* results_0_daily_0_wind_scale = results_0_daily_0["wind_scale"];*/
todayhumity = results_0_daily_0["humidity"].as<int>();//湿度
JsonObject results_0_daily_1 = results_0_daily[1];
nextdaydate = results_0_daily_1["date"].as<String>();
nextdaydayweather = results_0_daily_1["text_day"].as<String>();
nextdaydayweathercode = results_0_daily_1["code_day"].as<int>();
nextdaynightweather = results_0_daily_1["text_night"].as<String>();
nextdaynightweathercode = results_0_daily_1["code_night"].as<int>();
nextdaytempmax = results_0_daily_1["high"].as<int>();
nextdaytempmin = results_0_daily_1["low"].as<int>();
/*const char* results_0_daily_1_rainfall = results_0_daily_1["rainfall"];
const char* results_0_daily_1_precip = results_0_daily_1["precip"];
const char* results_0_daily_1_wind_direction = results_0_daily_1["wind_direction"];
const char* results_0_daily_1_wind_direction_degree = results_0_daily_1["wind_direction_degree"];
const char* results_0_daily_1_wind_speed = results_0_daily_1["wind_speed"];
const char* results_0_daily_1_wind_scale = results_0_daily_1["wind_scale"];*/
nextdayhumity = results_0_daily_1["humidity"].as<int>();
/*JsonObject results_0_daily_2 = results_0_daily[2];
const char* results_0_daily_2_date = results_0_daily_2["date"];
const char* results_0_daily_2_text_day = results_0_daily_2["text_day"];
const char* results_0_daily_2_code_day = results_0_daily_2["code_day"];
const char* results_0_daily_2_text_night = results_0_daily_2["text_night"];
const char* results_0_daily_2_code_night = results_0_daily_2["code_night"];
const char* results_0_daily_2_high = results_0_daily_2["high"];
const char* results_0_daily_2_low = results_0_daily_2["low"];
const char* results_0_daily_2_rainfall = results_0_daily_2["rainfall"];
const char* results_0_daily_2_precip = results_0_daily_2["precip"];
const char* results_0_daily_2_wind_direction = results_0_daily_2["wind_direction"];
const char* results_0_daily_2_wind_direction_degree = results_0_daily_2["wind_direction_degree"];
const char* results_0_daily_2_wind_speed = results_0_daily_2["wind_speed"];
const char* results_0_daily_2_wind_scale = results_0_daily_2["wind_scale"];
const char* results_0_daily_2_humidity = results_0_daily_2["humidity"];
const char* results_0_last_update = results_0["last_update"];
// 从以上信息中摘选几个通过串口监视器显示
String results_0_daily_0_date_str = results_0_daily_0["date"].as<String>();
String results_0_daily_0_text_day_str = results_0_daily_0["text_day"].as<String>();
int results_0_daily_0_code_day_int = results_0_daily_0["code_day"].as<int>();
String results_0_daily_0_text_night_str = results_0_daily_0["text_night"].as<String>();
int results_0_daily_0_code_night_int = results_0_daily_0["code_night"].as<int>();
int results_0_daily_0_high_int = results_0_daily_0["high"].as<int>();
int results_0_daily_0_low_int = results_0_daily_0["low"].as<int>();
String results_0_last_update_str = results_0["last_update"].as<String>();
Serial.println(F("======Today Weahter ======="));
Serial.print(F("DATE: "));
Serial.println(results_0_daily_0_date_str);
Serial.print(F("Day Weather: "));
Serial.print(results_0_daily_0_text_day_str);
Serial.print(F(" "));
Serial.println(results_0_daily_0_code_day_int);
Serial.print(F("Night Weather: "));
Serial.print(results_0_daily_0_text_night_str);
Serial.print(F(" "));
Serial.println(results_0_daily_0_code_night_int);
Serial.print(F("High: "));
Serial.println(results_0_daily_0_high_int);
Serial.print(F("LOW: "));
Serial.println(results_0_daily_0_low_int);
Serial.print(F("Last Update: "));
Serial.println(results_0_last_update_str);
Serial.println(F("=============================="));*/
}
// 向心知天气服务器服务器请求信息并对信息进行解析
void httpRequest(String reqRes) {
WiFiClient client;
// 建立http请求信息
String httpRequest = String("GET ") + reqRes + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n";
//Serial.println("");
//Serial.print("Connecting to ");
//Serial.print(host);
// 尝试连接服务器
if (client.connect(host, 80)) {
//Serial.println(" Success!");
// 向服务器发送http请求信息
client.print(httpRequest);
//Serial.println("Sending request: ");
Serial.println(httpRequest);
// 获取并显示服务器响应状态行
//String status_response = client.readStringUntil('\n');
//Serial.print("status_response: ");
//Serial.println(status_response);
// 使用find跳过HTTP响应头
if (client.find("\r\n\r\n")) {
Serial.println("Found Header End. Start Parsing.");
}
// 利用ArduinoJson库解析心知天气响应信息
parseInfo(client);
}
else {
Serial.println(" connection failed!");
}
//断开客户端与服务器连接工作
client.stop();
}
void getweather() {
// 建立心知天气API当前天气请求资源地址
String reqRes = "/v3/weather/daily.json?key=" + reqUserKey +
+ "&location=" + reqLocation + "&language=en&unit=" +
reqUnit + "&start=0&days=3";
// 向心知天气服务器服务器请求信息并对信息进行解析
httpRequest(reqRes);
delay(3000);
}
xinzitianqi.h 太极创客的东西
最新推荐文章于 2022-07-08 10:20:49 发布
关键词由CSDN通过智能技术生成