完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
我想知道我如何每 5 秒(左右)发送一个 http 请求?我目前发送 http 请求的方法:
本地空ICACHE_FLASH_ATTR tcp_connect_cb(void *arg){ os_printf("Connected to TCP Server.n"); struct espconn *conn = (struct espconn*)arg; char requestText[SIZE]; os_sprintf(requestText,"GET /toggleLED.php?getnumber /HTTP/1.1rnUser-Agent: curl/7.37.0rnHost: %srnAccept: */*rnrn",DOMAIN); espconn_send(conn,requestText,SIZE); } 本地空ICACHE_FLASH_ATTR tcp_sent_cb(void *arg){ os_printf("Send command is out.n"); } 本地空ICACHE_FLASH_ATTR tcp_recv_cb(void *arg, char *data, unsigned short len){ struct espconn *conn = (struct espconn*)arg; //do some stuff } 本地空ICACHE_FLASH_ATTR tcp_recon_cb(void *arg, sint8 err){ os_printf("TCP reconnected %dn", err); } 本地空ICACHE_FLASH_ATTR tcp_discon_cb(void *arg){ os_printf("TCP disconnected.n"); } 本地空ICACHE_FLASH_ATTR getHostByNameCB(const char *name, ip_addr_t *ip,void *arg){ static esp_tcp tcp; struct espconn *conn = (struct espconn*)arg; conn->type=ESPCONN_TCP; conn->state=ESPCONN_NONE; conn->proto.tcp=&tcp; conn->proto.tcp->local_port=espconn_port(); conn->proto.tcp->remote_port=80; os_memcpy(conn->proto.tcp->remote_ip,&ip->addr,4); espconn_regist_connectcb(conn,tcp_connect_cb); espconn_regist_recvcb(conn,tcp_recv_cb); espconn_regist_sentcb(conn,tcp_sent_cb); espconn_regist_reconcb(conn,tcp_recon_cb); espconn_regist_disconcb(conn,tcp_discon_cb); espconn_connect(conn); } 本地空ICACHE_FLASH_ATTR check_ip(无效){ struct ip_info ipconf; bool res = wifi_get_ip_info(STATION_IF, &ipconf); if(!res) os_printf("No WiFi Connection."); else{ if(wifi_station_get_connect_status() == STATION_GOT_IP && ipconf.ip.addr != 0){ os_printf("Got IP Address.n"); static struct espconn con; static ip_addr_t ip; espconn_gethostbyname(&con,DOMAIN,&ip,getHostByNameCB); } else { os_timer_setfn(&timer,(os_timer_func_t*)check_ip,NULL); os_timer_arm(&timer,100,0); //recall function after 100ms, don't repeat } } } 本地空ICACHE_FLASH_ATTR set_station_config(void){ struct station_config stationConf; char *SSID = ""; char *PW = ""; stationConf.bssid_set = 0; os_memcpy(&stationConf.ssid,SSID,32); os_memcpy(&stationConf.password,PW,64); wifi_station_set_config(&stationConf); wifi_station_connect(); } void user_init(void){ wifi_set_opmode(STATION_MODE); set_station_config(); check_ip(); } |
|
相关推荐
1个回答
|
|
|
要实现每5秒发送一次HTTP请求,您可以使用一个定时器来实现。以下是使用ESP8266模块和FreeRTOS操作系统的示例代码:
1. 首先,确保您已经包含了所需的头文件: ```c #include "osapi.h" #include "espconn.h" #include "os_type.h" #include "user_interface.h" ``` 2. 定义一个全局变量,用于存储定时器句柄: ```c static os_timer_t myTimer; ``` 3. 定义一个定时器回调函数,用于发送HTTP请求: ```c static void ICACHE_FLASH_ATTR sendHttpRequestCb(void *arg) { struct espconn *conn = (struct espconn *)arg; char requestText[128]; os_sprintf(requestText, "GET /toggleLED.php?getnumber HTTP/1.1rnUser-Agent: curl/7.37.0rnHost: %srnAccept: */*rnrn", DOMAIN); espconn_send(conn, (uint8 *)requestText, strlen(requestText)); } ``` 4. 在您的TCP连接回调函数中,创建一个定时器,并设置定时器周期为5000毫秒(5秒): ```c LOCAL void ICACHE_FLASH_ATTR tcp_connect_cb(void *arg) { os_printf("Connected to TCP Server.n"); struct espconn *conn = (struct espconn *)arg; // 创建定时器 os_timer_disarm(&myTimer); os_timer_setfn(&myTimer, (os_timer_func_t *)sendHttpRequestCb, conn); os_timer_arm(&myTimer, 5000, 1); // 设置定时器周期为5000毫秒,重复执行 } ``` 5. 在您的程序中,确保已经初始化了TCP连接和定时器: ```c void user_init(void) { // 初始化TCP连接 struct espconn conn; conn.type = ESPCONN_TCP; conn.proto.tcp = NULL; espconn_regist_connectcb(&conn, tcp_connect_cb); // 连接到服务器 espconn_connect(&conn, &server_addr); } ``` 这样,您的程序将每5秒发送一次HTTP请求。请注意,这个示例使用了ESP8266模块和FreeRTOS操作系统。如果您使用的是其他硬件或操作系统,可能需要进行相应的调整。 |
|
|
|
|
只有小组成员才能发言,加入小组>>
944 浏览 0 评论
1671 浏览 0 评论
请问一下我想用ESP8685直接替换ESP8266而不用写程序,可以让ESP8685直接通过之前ESP8266的外挂的flash运行程序吗
1330 浏览 1 评论
1233 浏览 1 评论
5015 浏览 2 评论
为blufi_device设置自定义名称,但是无法修改,为什么?
1250浏览 4评论
请问ESP32-S2-WROOM怎么获得ESP32-S2外接FLASH的唯一序列号?
927浏览 3评论
2330浏览 3评论
ESP-IDF的VScode插件的build按钮点击会报错的原因?
2525浏览 3评论
ESP-Jumpstart例程中第5个工程:5_cloud连接报错是哪里的问题?
1051浏览 2评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-1 20:36 , Processed in 0.539899 second(s), Total 72, Slave 55 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
6559
