esp8266气象站

参考:

https://blog.csdn.net/xuw_xy/article/details/89407607

https://blog.csdn.net/oAlevel/article/details/86976348

//-----------------------------------------------------------------------

使用心知天气API

注册申请https://www.seniverse.com

获得免费开发密钥https://www.seniverse.com/products?iid=new

免费用户权限

免费用户可以调用哪些数据

国内 370 个主要城市

  1. 天气实况,包括天气现象文字、代码和气温 3 项数据
  2. 未来 3 天天气预报,包括白天天气现象文字及代码、晚间天气现象文字及代码、当天最高温度和最低温度、风向风速
  3. 6 项基本类生活指数,包括穿衣、紫外线强度、洗车、旅游、感冒、运动指数

//-------------------------------------------------

使用arduinojson工具做切片解析 https://arduinojson.org/v5/assistant/

是用范例,以下面的生活指数为例

const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 6*JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 2*JSON_OBJECT_SIZE(6) + 350;
DynamicJsonBuffer jsonBuffer(capacity);

const char* json = "{\"results\":[{\"location\":{\"id\":\"WTMKQ069CCJ7\",\"name\":\"杭州\",\"country\":\"CN\",\"path\":\"杭州,杭州,浙江,中国\",\"timezone\":\"Asia/Shanghai\",\"timezone_offset\":\"+08:00\"},\"suggestion\":{\"car_washing\":{\"brief\":\"不宜\",\"details\":\"\"},\"dressing\":{\"brief\":\"热\",\"details\":\"\"},\"flu\":{\"brief\":\"易发\",\"details\":\"\"},\"sport\":{\"brief\":\"较不宜\",\"details\":\"\"},\"travel\":{\"brief\":\"较不宜\",\"details\":\"\"},\"uv\":{\"brief\":\"弱\",\"details\":\"\"}},\"last_update\":\"2019-06-30T13:49:49+08:00\"}]}";

JsonObject& root = jsonBuffer.parseObject(json);

JsonObject& results_0 = root["results"][0];

JsonObject& results_0_location = results_0["location"];
const char* results_0_location_id = results_0_location["id"]; // "WTMKQ069CCJ7"
const char* results_0_location_name = results_0_location["name"]; // "杭州"
const char* results_0_location_country = results_0_location["country"]; // "CN"
const char* results_0_location_path = results_0_location["path"]; // "杭州,杭州,浙江,中国"
const char* results_0_location_timezone = results_0_location["timezone"]; // "Asia/Shanghai"
const char* results_0_location_timezone_offset = results_0_location["timezone_offset"]; // "+08:00"

JsonObject& results_0_suggestion = results_0["suggestion"];

const char* results_0_suggestion_car_washing_brief = results_0_suggestion["car_washing"]["brief"]; // "不宜"
const char* results_0_suggestion_car_washing_details = results_0_suggestion["car_washing"]["details"]; // ""

const char* results_0_suggestion_dressing_brief = results_0_suggestion["dressing"]["brief"]; // "热"
const char* results_0_suggestion_dressing_details = results_0_suggestion["dressing"]["details"]; // ""

const char* results_0_suggestion_flu_brief = results_0_suggestion["flu"]["brief"]; // "易发"
const char* results_0_suggestion_flu_details = results_0_suggestion["flu"]["details"]; // ""

const char* results_0_suggestion_sport_brief = results_0_suggestion["sport"]["brief"]; // "较不宜"
const char* results_0_suggestion_sport_details = results_0_suggestion["sport"]["details"]; // ""

const char* results_0_suggestion_travel_brief = results_0_suggestion["travel"]["brief"]; // "较不宜"
const char* results_0_suggestion_travel_details = results_0_suggestion["travel"]["details"]; // ""

const char* results_0_suggestion_uv_brief = results_0_suggestion["uv"]["brief"]; // "弱"
const char* results_0_suggestion_uv_details = results_0_suggestion["uv"]["details"]; // ""

const char* results_0_last_update = results_0["last_update"]; // "2019-06-30T13:49:49+08:00"

//-------------------------------------------------

使用API请求最近三天天气

https://api.seniverse.com/v3/weather/daily.json?key=youkey&location=hangzhou&language=zh-Hans&unit=c&start=0&days=4

{"results":[{"location":{"id":"WTMKQ069CCJ7","name":"杭州","country":"CN","path":"杭州,杭州,浙江,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"daily":[{"date":"2019-

06-30
","text_day":"暴雨","code_day":"16","text_night":"中雨","code_night":"14","high":"29","low":"25","precip":"","wind_direction":"","wind_direction_degree":"0","wind_speed":"15","wind_scale":"3"},{"date":"2019-07-01","text_day":"中雨","code_day":"14","text_night":"中雨","code_night":"14","high":"27","low":"24","precip":"","wind_direction":"无持续风向","wind_direction_degree":"","wind_speed":"10","wind_scale":"2"},{"date":"2019-07-02","text_day":"小雨","code_day":"13","text_night":"小雨","code_night":"13","high":"24","low":"22","precip":"","wind_direction":"","wind_direction_degree":"90","wind_speed":"15","wind_scale":"3"}],"last_update":"2019-06-30T11:00:00+08:00"}]}

请求当前天气温度

https://api.thinkpage.cn/v3/weather/now.json?key=youkey&location=hangzhou&language=zh-Hans&unit=c

{"results":[{"location":{"id":"WTMKQ069CCJ7","name":"杭州","country":"CN","path":"杭州,杭州,浙江,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"多云","c
ode
":"4","temperature":"28"},"last_update":"2019-06-30T16:20:00+08:00"}]}



请求生活指数

https://api.seniverse.com/v3/life/suggestion.json?key=youkey&location=shanghai&language=zh-Hans

 

{"results":[{"location":{"id":"WTW3SJ5ZBJUY","name":"上海","country":"CN","path":"上海,上海,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"suggestion":{"car_washing":{"brief":"不宜","details":""},"dressing":{"brief":"舒适","details":""},"flu":{"brief":"较易发","details":""},"sport":{"brief":"较不宜","details":""},"travel":{"brief":"一般","details":""},"uv":{"brief":"最弱","details":""}},"last_update":"2019-06-30T15:20:10+08:00"}]}

emmm,现在的问题提是,是否能够一次请求所有的数据,感觉不能

猜你喜欢

转载自www.cnblogs.com/beyondsdo/p/11110324.html