|
8266原生乐鑫SDK高级使用之封装Post与Get请求云端,拿到“天气预报信息”。
嗨喽各位机智云小伙伴,小徐我又来啦!这次带来的不是Android的App开发教程,带来的是esp8266全新的知识,昨天社区出了个STM32加8266的天气预报的博文,我甚是欣慰,以为是8266直接加OLED显示天气预报,没想到不是呐!个人大失所望, 逛了这么久的度娘,原生的SDK开发8266天气预报少之又少哎呀!
今天就给大家先出个"8266天气预报的原理与获取数据博文",暂不进一步剖析数据与显示到 OLED。如果有兴趣的,可以私聊我!我们一起搞!哈哈~~
<Tips: 本博文理论比较多,所以特意画图进一步给大家说下原理!回复帖子即可下载工程源码!!>
机智云发烧友:小徐
2017/11/28
目 录
一、效果演示。
二、通讯原理。
三、代码揭示。
更多8266SDK教程请移步我的GitHub: https://github.com/xuhongv/StudyInEsp8266
一、效果演示。
下图是串口助手返回的天气预报信息,双击打开瞧瞧。
好吧!我还是复制代码各位看看吧。可能手机党打开不了呢!
- GM�X匑IeI旝E爻釩Q@娈]_[窜N臫瀺郹\�%aHP@i薱hFa咤爾@G阝吿TQ征枀H歃Ea蹷�aHP@iI塒@娈a繲氣厛TQ睁邊瞃a蹷�uJQ@i嫂Q@娈q萒陉吺Y蓥迏H鋂膄�a\�� *惣謱滯鮂
- ----- PB start -----
- api.seniverse.com/v3/weather/daily.json?key=rrpd2zmqkpwlsckt&location=guangzhou&language=en&unit=c&start=0&days=3
- ----- PB next -----
- /v3/weather/daily.json?key=rrpd2zmqkpwlsckt&location=guangzhou&language=en&unit=c&start=0&days=3
- ----- PB end -----
- ----- PB=true -----
- api.seniverse.com
- ----- 请求数据开始-----
- GET /v3/weather/daily.json?key=rrpd2zmqkpwlsckt&location=guangzhou&language=en&unit=c&start=0&days=3 HTTP/1.1
- Content-Type: text/html;charset=utf-8
- Accept: */*
- Host: api.seniverse.com
- Connection: Keep-Alive
- -----请求数据结束-----
- 发送数据成功!
- ----- 开始接受数据-----
- HTTP/1.1 200 OK
- Date: Tue, 28 Nov 2017 08:23:45 GMT
- Content-Type: application/json; charset=utf-8
- Content-Length: 900
- Connection: keep-alive
- Cache-Control: no-cache
- Pragma: no-cache
- Expires: -1
- {"results":[{"location":{"id":"WS0E9D8WN298","name":"Guangzhou","country":"CN","path":"Guangzhou,Guangzhou,Guangdong,China","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"daily":[{"date":"2017-11-28","text_day":"Cloudy","code_day":"4","text_night":"Overcast","code_night":"9","high":"25","low":"18","precip":"","wind_direction":"CLM","wind_direction_degree":"","wind_speed":"10","wind_scale":"2"},{"date":"2017-11-29","text_day":"Overcast","code_day":"9","text_night":"Light rain","code_night":"13","high":"25","low":"16","precip":"","wind_direction":"CLM","wind_direction_degree":"","wind_speed":"10","wind_scale":"2"},{"date":"2017-11-30","text_day":"Light rain","code_day":"13","text_night":"Light rain","code_night":"13","high":"21","low":"14","precip":"","wind_direction":"CLM","wind_direction_degree":"","wind_speed":"10","wind_scale":"2"}],"last_update":"2017-11-28T11:00:00+08:00"}]}
- -----结束接受数据-----
- 断开连接成功!
复制代码
二、通讯原理。(以下的图片均为个人辛苦所绘制,绝对原创!!)
2.1 、 esp8266从联网到云端取数据的原理。
2.2 、 原理流程大概都懂,但是重要的是我们电子硬件开发者可能不懂什么是post和get请求方式?二者有啥区别?好吧,我再画一个图!!
2.3 、既然请求方式都懂了,那么我们的8266是怎么做到请求数据呢?好吧!我再再画一个图:
三、代码揭示。
3.1 写头文件 my_http.h”的实现C文件。用于剖析URL,定义post请求或get请求。
- #include "my_http.h"
- #include "c_types.h"
- #include "client.h"
- //剖析URL
- void ICACHE_FLASH_ATTR http_parse_request_url(char *URL, char *host,char *filename, unsigned short *port) {
- char *PA;
- char *PB;
- memset(host, 0, sizeof(host));
- memset(filename, 0, sizeof(filename));
- *port = 0;
- if (!(*URL)){
- uart0_sendStr("\r\n ----- URL return ----- \r\n");
- return;
- }
- PA = URL;
- if (!strncmp(PA, "http://", strlen("http://"))) {
- PA = URL + strlen("http://");
- } else if (!strncmp(PA, "https://", strlen("https://"))) {
- PA = URL + strlen("https://");
- }
- PB = strchr(PA, '/');
- if (PB) {
- uart0_sendStr("\r\n ----- PB=true ----- \r\n");
- memcpy(host, PA, strlen(PA) - strlen(PB));
- if (PB + 1) {
- memcpy(filename, PB + 1, strlen(PB - 1));
- filename[strlen(PB) - 1] = 0;
- }
- host[strlen(PA) - strlen(PB)] = 0;
- uart0_sendStr(host,strlen(host));
- } else {
- uart0_sendStr("\r\n ----- PB=false ----- \r\n");
- memcpy(host, PA, strlen(PA));
- host[strlen(PA)] = 0;
- uart0_sendStr(host,strlen(host));
- }
- PA = strchr(host, ':');
- if (PA){
- *port = atoi(PA + 1);
- }else{
- *port = 80;
- }
- }
- //寻找DNS解析,并且配置
- void ICACHE_FLASH_ATTR user_esp_dns_found(const char *name, ip_addr_t *ipaddr,void *arg) {
- struct ip_info info;
- wifi_get_ip_info(STATION_IF, &info);
- my_station_init(ipaddr, &info.ip, port);
- }
- //定义Get请求的实现
- void ICACHE_FLASH_ATTR startHttpQuestByGET(char *URL){
- struct ip_addr addr;
- memset(buffer,0,1024);
- http_parse_request_url(URL,host,filename,&port);
- os_sprintf(buffer,GET,filename,host);
- espconn_gethostbyname(&user_tcp_conn,host, &addr,
- user_esp_dns_found);
- }
- //定义Post请求的实现
- void ICACHE_FLASH_ATTR startHttpQuestByPOST(char *URL,char *method,char *postdata){
- struct ip_addr addr;
- memset(buffer,0,1024);
- http_parse_request_url(URL,host,filename,&port);
- os_sprintf(buffer,POST,filename,strlen(postdata),host,postdata);
- espconn_gethostbyname(&user_tcp_conn,host, &addr,
- user_esp_dns_found);
- }
复制代码
3.2、写头文件client.h的实现C文件。用于配置本地端口、连接服务器。
- #include "client.h"
- #include "iconv.h"
- #include "stdio.h"
- #include "string.h"
- //成功接收到服务器返回数据函数
- void ICACHE_FLASH_ATTR user_tcp_recv_cb(void *arg, char *pdata,
- unsigned short len) {
- uart0_sendStr("\r\n ----- 开始接受数据----- \r\n ");
- uart0_tx_buffer(pdata, strlen(pdata));
- uart0_sendStr("\r\n -----结束接受数据----- \r\n ");
- }
- //发送数据到服务器成功的回调函数
- void ICACHE_FLASH_ATTR user_tcp_sent_cb(void *arg) {
- uart0_sendStr("发送数据成功!\r\n ");
- }
- //断开服务器成功的回调函数
- void ICACHE_FLASH_ATTR user_tcp_discon_cb(void *arg) {
- uart0_sendStr("断开连接成功!\r\n ");
- }
- //连接失败的回调函数,err为错误代码
- void ICACHE_FLASH_ATTR user_tcp_recon_cb(void *arg, sint8 err) {
- uart0_sendStr("连接错误,错误代码为%d\r\n", err);
- espconn_connect((struct espconn *) arg);
- }
- //成功连接到服务器的回调函数
- void ICACHE_FLASH_ATTR user_tcp_connect_cb(void *arg) {
- struct espconn *pespconn = arg;
- espconn_regist_recvcb(pespconn, user_tcp_recv_cb);
- espconn_regist_sentcb(pespconn, user_tcp_sent_cb);
- espconn_regist_disconcb(pespconn, user_tcp_discon_cb);
- uart0_sendStr("\r\n ----- 请求数据开始----- \r\n");
- uart0_tx_buffer(buffer, strlen(buffer));
- uart0_sendStr("\r\n -----请求数据结束----- \r\n");
- espconn_sent(pespconn, buffer, strlen(buffer));
- }
- void ICACHE_FLASH_ATTR my_station_init(struct ip_addr *remote_ip,
- struct ip_addr *local_ip, int remote_port) {
- //配置
- user_tcp_conn.type = ESPCONN_TCP;
- user_tcp_conn.state = ESPCONN_NONE;
- user_tcp_conn.proto.tcp = (esp_tcp *) os_zalloc(sizeof(esp_tcp));
- os_memcpy(user_tcp_conn.proto.tcp->local_ip, local_ip, 4);
- os_memcpy(user_tcp_conn.proto.tcp->remote_ip, remote_ip, 4);
- user_tcp_conn.proto.tcp->local_port = espconn_port();
- user_tcp_conn.proto.tcp->remote_port = remote_port;
- //注册
- espconn_regist_connectcb(&user_tcp_conn, user_tcp_connect_cb);
- espconn_regist_reconcb(&user_tcp_conn, user_tcp_recon_cb);
- //连接服务器
- espconn_connect(&user_tcp_conn);
- }
复制代码
3.3 程序入口,user_main.c。
- #include "driver/uart.h"
- #include "user_main.h"
- //连接路由器的定时器
- os_timer_t checkTimer_wifistate;
- void Check_WifiState(void) {
- uint8 getState;
- getState = wifi_station_get_connect_status();
- //如果状态正确,证明已经成功连接到路由器
- if (getState == STATION_GOT_IP) {
- os_printf("WIFI连接成功!把连接路由器的定时器关闭!");
- os_timer_disarm(&checkTimer_wifistate);
- os_timer_disarm(&connect_timer);
- uint8 status = wifi_station_get_connect_status();
- if (status == STATION_GOT_IP) {
- uart0_sendStr("WIFI连接成功!开始请求数据!");
- startHttpQuestByGET(
- "https://api.seniverse.com/v3/weather/daily.json?key=rrpd2zmqkpwlsckt&location=guangzhou&language=en&unit=c&start=0&days=3");
- return;
- }
- }
- }
- void user_init() {
- uart_init(57600, 57600);
- wifi_set_opmode(0x01); //设置为STATION模式
- struct station_config stationConf;
- os_strcpy(stationConf.ssid, "TP-LINK_AliYun"); //改成你自己的 路由器的用户名
- os_strcpy(stationConf.password, "aliyun_123456"); //改成你自己的 路由器的密码
- wifi_station_set_config(&stationConf); //设置WiFi station接口配置,并保存到 flash
- wifi_station_connect(); //连接路由器
- os_timer_disarm(&checkTimer_wifistate); //取消定时器定时
- os_timer_setfn(&checkTimer_wifistate, (os_timer_func_t *) Check_WifiState,
- NULL); //设置定时器回调函数
- os_timer_arm(&checkTimer_wifistate, 500, true); //启动定时器,单位:毫秒500
- }
复制代码
附加:关于服务器的选择与怎么看接口使用。
心知天气免费接口:https://www.seniverse.com/doc#daily
IoT软件平台
|
|