单片机利用8266获取天气信息
1、esp8266从联网到云端取数据的原理
2、使用esp8266模块获取天气的AT指令设置方法 (1)使用串口发送指令AT+CWMODE=1设置模块Wi-Fi应用模式为Station模式;
(2)发送指令AT+RST重启模块使应用模式更改生效;
(3)发送指令AT+CWJAP =”ssid”,”pwd”连接AP;
(4)发送指令AT+CIPMUX=0设置模块为单路连接模式,模块默认为单路连接模式;
(5)发送指令AT+CIPSTART=”TCP”,” api.seniverse.com”,80 与服务器建立TCP连接 ;
(6)发送指令AT+CIPMODE=1设置模块传输模式为透传模式;
(7)发送指令AT+CIPSEND开启透传模式向服务器发送数据,模块收到此指令后先换行后返回“>”
(8)剩下只需要发送 GET https://api.seniverse.com/v3/weather/now.json?key=XXXXXXXXXXX&location=beijing&language=zh-Hans&unit=c,就能获取到天气信息了。
说明: 天气网站:心知天气 tcp server: api.seniverse.com 端口:80 使用域名解析工具对“api.seniverse.com”进行解析, 得到该域名绑定的ip地址为:116.62.81.138
返回数据格式:心知天气返回的数据是json格式
解析方法:使用到第三方的开源库cJSON,ESP8266的SDK自带这些移植好的库。
esp8266模块作为tcp client主动连接心知天气服务器,方式如下: AT指令连接:AT+CIPSTART="TCP","api.seniverse.com",80
连接知心天气网站服务器:
心知天气网站的天气api接口正常情况下应该返回的数据内容格式: {"results":[{"location":{"id":"WX4FBXXFKE4F","name":"北京","country":"CN","path":"北京,北京,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"阴","code":"9","temperature":"14"},"last_update":"2019-04-19T09:35:00+08:00"}]}
实测1: 请求方式:利用串口调试助手以http协议的GET方式向天气网站服务器提交请求
Api接口1(返回北京当天天气信息)):https://api.seniverse.com/v3/weather/now.json?key=XXXXXXXXXXX&location=beijing&language=zh-Hans&unit=c
请求内容:GET https://api.seniverse.com/v3/weather/now.json?key=XXXXXXXXXXX&location=beijing&language=zh-Hans&unit=c
返回内容:{"results":[{"location":{"id":"WX4FBXXFKE4F","name":"北京","country":"CN","path":"北京,北京,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"阴","code":"9","temperature":"14"},"last_update":"2019-04-19T09:35:00+08:00"}]}
我用了两种串口调试工具进行了测试,如下:
工具一:ESP8266调试工具2.0
实测2: 请求方式:利用串口调试助手以http协议的GET方式向服务器提交请求
Api接口2(返回广州近3天天气信息):https://api.seniverse.com/v3/weather/daily.json?key=XXXXXXXXXXX&location=guangzhou&language=en&unit=c&start=0&days=3
请求内容:GET https://api.seniverse.com/v3/weather/daily.json?key=XXXXXXXXXXX&location=guangzhou&language=en&unit=c&start=0&days=3
返回信息内容: {"results":[{"location":{"id":"WS0E9D8WN298","name":"Guangzhou","country":"CN","path":"Guangzhou,Guangzhou,Guangdong,China","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"daily":[{"date":"2019-04-19","text_day":"Storm","code_day":"16","text_night":"Thundershower","code_night":"11","high":"25","low":"21","precip":"","wind_direction":"CLM","wind_direction_degree":"","wind_speed":"10","wind_scale":"2"},{"date":"2019-04-20","text_day":"Moderate rain","code_day":"14","text_night":"Thundershower","code_night":"11","high":"27","low":"23","precip":"","wind_direction":"CLM","wind_direction_degree":"","wind_speed":"10","wind_scale":"2"},{"date":"2019-04-21","text_day":"Light rain","code_day":"13","text_night":"Thundershower","code_night":"11","high":"29","low":"24","precip":"","wind_direction":"CLM","wind_direction_degree":"","wind_speed":"10","wind_scale":"2"}],"last_update":"2019-04-19T08:00:00+08:00"}]}
|