Esp8266学习之旅⑤ 8266原生乐鑫SDK高级使用之封装Post与Get请求云端,拿到“天气预报信息”。(带Demo)
2017-11-28 16:34
579 查看
本系列博客学习由非官方人员 半颗心脏 潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途。如有不对之处,请留言,本人及时更改。
1、 Esp8266之 搭建开发环境,开始一个“hellow world”串口打印。
2、 Esp8266之 利用GPIO开始使用按钮点亮你的“第一盏灯”。
3、 Esp8266之 利用”软件定时器 “或”硬件定时器”定时0.5秒闪烁点亮一盏LED。
4 、Esp8266之 了解PWM,更为深入地用PWM控制一盏LED的亮度变化。
5 、Esp8266之 原生乐鑫SDK高级使用之封装Post与Get请求云端,拿到“天气预报信息”。
6 、Esp8266之 了解 SmartConfig与Airkiss一键配网,给8266配网上云端。无需把wifi名字密码写在固件里。
7 、Esp8266之 了解 softAP热点配网模式原理,仿“机智云”定义自己的热点配网模式协议。
8、 Esp8266之 你要找的8266作为UDP、TCP客户端或服务端的角色通讯,都在这了。
9、 Esp8266进阶之路第一篇: [小实战上篇]Windows系统搭建8266的本地Mqtt服务器,局域网点亮一盏LED灯。
10、 Esp8266进阶之路第二篇: [小实战下篇]Windows系统搭建8266的本地Mqtt服务器,局域网点亮一盏LED灯。
11、 Esp8266进阶之路第三篇: 8266接入阿里智能,点亮一盏LED灯,期待天猫精灵语音控制的不约而至!
12、 Esp8266进阶之路第四篇: 图文并茂学习阿里云主机搭建8266MQTT服务器,实现移动网络远程控制一盏LED。
13、 Esp8266进阶之路第五篇: 动手做个8266毕设小案例,smartConfig + MQTT协议轻松实现远程控制一盏LED。
14、 Esp8266进阶之路第六篇: esp8266的Rtos系统学习的正确姿势 [上篇]—— 环境搭建、烧录。
Demo: https://github.com/xuhongv/StudyInEsp8266/tree/master/6_PostAndGet
1、 Esp8266之 搭建开发环境,开始一个“hellow world”串口打印。
2、 Esp8266之 利用GPIO开始使用按钮点亮你的“第一盏灯”。
3、 Esp8266之 利用”软件定时器 “或”硬件定时器”定时0.5秒闪烁点亮一盏LED。
4 、Esp8266之 了解PWM,更为深入地用PWM控制一盏LED的亮度变化。
5 、Esp8266之 原生乐鑫SDK高级使用之封装Post与Get请求云端,拿到“天气预报信息”。
6 、Esp8266之 了解 SmartConfig与Airkiss一键配网,给8266配网上云端。无需把wifi名字密码写在固件里。
7 、Esp8266之 了解 softAP热点配网模式原理,仿“机智云”定义自己的热点配网模式协议。
8、 Esp8266之 你要找的8266作为UDP、TCP客户端或服务端的角色通讯,都在这了。
9、 Esp8266进阶之路第一篇: [小实战上篇]Windows系统搭建8266的本地Mqtt服务器,局域网点亮一盏LED灯。
10、 Esp8266进阶之路第二篇: [小实战下篇]Windows系统搭建8266的本地Mqtt服务器,局域网点亮一盏LED灯。
11、 Esp8266进阶之路第三篇: 8266接入阿里智能,点亮一盏LED灯,期待天猫精灵语音控制的不约而至!
12、 Esp8266进阶之路第四篇: 图文并茂学习阿里云主机搭建8266MQTT服务器,实现移动网络远程控制一盏LED。
13、 Esp8266进阶之路第五篇: 动手做个8266毕设小案例,smartConfig + MQTT协议轻松实现远程控制一盏LED。
14、 Esp8266进阶之路第六篇: esp8266的Rtos系统学习的正确姿势 [上篇]—— 环境搭建、烧录。
一、前言。
1.1、 由于本篇博文理论多于代码,大家要耐心看下去哦。先上效果图:
1.2、我还是复制过来吧,图片看的模糊:
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"}]} -----结束接受数据----- 断开连接成功!
1.3、电路图:最小系统就行。因为不需要物理按键触发配网:
二、理论知识 。
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 }
4、关于服务器的选择与怎么看接口使用。
4.1、这个我使用的是心知天气,不是卖广告,*不喜勿喷!!
接口:https://www.seniverse.com/doc#dailyDemo: https://github.com/xuhongv/StudyInEsp8266/tree/master/6_PostAndGet
相关文章推荐
- 【HttpClient学习与实战】1.使用httpClient实现get与post信息传输
- OkHttp学习(1)-->>同步和异步(get、post键值对、post带map、请求头体封装json)
- Http学习之使用HttpURLConnection发送post和get请求
- java使用httpclient封装post请求和get的请求
- Http学习之使用HttpURLConnection发送post和get请求 android
- java使用httpclient封装post请求和get的请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求(3)
- C/C++使用libcurl库发送http请求(get和post可以用于请求html信息,也可以请求xml和json等串)
- Http学习之使用HttpURLConnection发送post和get请求
- C++ 使用 curl 进行 http 请求(GET、POST、Download)的封装
- Http学习之使用HttpURLConnection发送post和get请求(1)
- 学习笔记--post 相比get 有很多优点,为什么现在的HTTP通信中大多数请求还是使用get?
- Http学习之使用HttpURLConnection发送post和get请求 .
- C/C++使用libcurl库发送http请求(get和post可以用于请求html信息,也可以请求xml和json等串)
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- 接口测试入门(2)--get和post初级请求/使用httpclient做一个获取信息list的请求(需要登录才可以)
- Http学习之使用HttpURLConnection发送post和get请求
- Java学习笔记———《http的get请求和post请求及封装的工具类》