Esp8266学习之旅⑤ 8266原生乐鑫SDK高级使用之封装Post与Get请求云端,拿到“天气预报信息”。(带Demo)
来源:互联网 发布:开淘宝店铺要多少钱 编辑:程序博客网 时间:2024/06/06 14:01
本系列博客学习由非官方人员 半颗心脏 潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途。如有不对之处,请留言,本人及时更改。
①、 Esp8266之 搭建开发环境,开始一个“hellow world”串口打印。
②、 Esp8266 之利用GPIO开始使用按钮点亮你的“第一盏灯”。
③、 Esp8266之 利用 ” 软件定时器 ” 定时0.5秒闪烁点亮一盏LED。
④ 、Esp8266之 了解PWM,更为深入地用PWM控制一盏LED的亮度变化。
⑤ 、Esp8266之 原生乐鑫SDK高级使用之封装Post与Get请求云端,拿到“天气预报信息”。
⑥ 、Esp8266之 了解SmartConfig与AirKiss的一键配网区别,给8266一键配网上云端。
⑦ 、Esp8266之 了解 softAP热点配网模式原理,仿“机智云”定义自己的热点配网模式协议。
⑧、Esp8266之 你要找的8266作为UDP、TCP客户端或服务端的角色通讯,都在这了。
9、 Esp8266进阶之路第一篇: [小实战上篇]Windows系统搭建8266的本地Mqtt服务器,局域网点亮一盏LED灯。
10、 Esp8266进阶之路第二篇: [小实战下篇]Windows系统搭建8266的本地Mqtt服务器,局域网点亮一盏LED灯。
一、前言。
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.1Content-Type: text/html;charset=utf-8Accept: */*Host: api.seniverse.comConnection: Keep-Alive -----请求数据结束----- 发送数据成功! ----- 开始接受数据----- HTTP/1.1 200 OKDate: Tue, 28 Nov 2017 08:23:45 GMTContent-Type: application/json; charset=utf-8Content-Length: 900Connection: keep-aliveCache-Control: no-cachePragma: no-cacheExpires: -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"//剖析URLvoid 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#daily
Demo: https://github.com/xuhongv/StudyInEsp8266/tree/master/6_PostAndGet
阅读全文
0 0
- Esp8266学习之旅⑤ 8266原生乐鑫SDK高级使用之封装Post与Get请求云端,拿到“天气预报信息”。(带Demo)
- Esp8266学习之旅⑥ 了解SmartConfig与Airkiss一键配网,给8266一键配网上云端。(带Demo)
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求 .
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- JavaWeb学习之Get请求和POST
- 【unity3D】插件推荐
- 利用华硕Xtion Pro Live 在ROS-kinetic下跑rgbdslam_v2
- android画布局部矩形擦除
- Android 配置shareSDK出现错误
- 记录一些软件安装过程参考的博客
- Esp8266学习之旅⑤ 8266原生乐鑫SDK高级使用之封装Post与Get请求云端,拿到“天气预报信息”。(带Demo)
- IL指令
- location.herf=url参数有中文传到后台java代码有乱码
- 小米小钢炮等常用蓝牙设备(音箱/键盘/打印机)连接电脑(Windows/Linux)使用笔记
- 12M晶振下,STM32串口波特率设置问题
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测
- 从零使用qemu模拟器搭建arm执行环境
- 【心情】工作之余对工作两年的回顾(内容与标题不符)
- 队列的链式存储方式