乐鑫技术交流
直播中

刘强

8年用户 1261经验值
私信 关注
[问答]

请问如何每5秒发送一次http请求?

我想知道我如何每 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)

jjll652

2024-7-12 18:01:36
要实现每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操作系统。如果您使用的是其他硬件或操作系统,可能需要进行相应的调整。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分