单片机利用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=smtq3n0ixdggurox&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=smtq3n0ixdggurox&location=beijing&language=zh-Hans&unit=c
请求内容:GET https://api.seniverse.com/v3/weather/now.json?key=smtq3n0ixdggurox&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=rrpd2zmqkpwlsckt&location=guangzhou&language=en&unit=c&start=0&days=3
请求内容:GET https://api.seniverse.com/v3/weather/daily.json?key=rrpd2zmqkpwlsckt&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"}]}
我用了两种串口调试工具进行了测试,如下:
工具一:ESP8266调试工具2.0:
工具二:友善串口调试助手
实测3(我注册申请的心知天气的免费试用的天气数据api接口):
请求方式:利用串口天调试助手以http协议的GET方式向服务器提交请求
Api接口3(杭州当天天气信息):https://api.seniverse.com/v3/weather/now.json?key=S4bQnu5_rNi_kbNJZ&location=hangzhou&language=zh-Hans&unit=c
请求内容:
GET https://api.seniverse.com/v3/weather/now.json?key=S4bQnu5_rNi_kbNJZ&location=hangzhou&language=zh-Hans&unit=c
返回信息内容:
{"results":[{"location":{"id":"WTMKQ069CCJ7","name":"Hangzhou","country":"CN","path":"Hangzhou,Hangzhou,Zhejiang,China","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"Cloudy","code":"4","temperature":"27","feels_like":"27","pressure":"1004","humidity":"60","visibility":"10.6","wind_direction":"N","wind_direction_degree":"20","wind_speed":"26.64","wind_scale":"4","clouds":"50","dew_point":""},"last_update":"2019-04-19T15:25:00+08:00"}]}
我用了两种串口调试工具进行了测试,如下:
工具一:ESP8266调试工具2.0:
工具二:友善串口调试助手
遇到问题:返回的信息中包含中文的部分,在串口调试助手中显示为乱码,我把url(就是GET请求的内容)直接输入到浏览器可以看到正常的返回数据,并在网页上正常显示中文内容。
查阅资料及问题原因猜想:请求的api返回的内容类型为j son格式类型,浏览器可以自动解析这个格式,但是串口调试助手不能对这个格式进行解析。
解决方法:在客户端(单片机)写程序调用cjson库对返回的数据进行解析即可。