How to send a http request every 5 seconds?

Stational
Posts: 42
Joined: Sat Jul 02, 2016 10:54 pm

How to send a http request every 5 seconds?

Postby Stational » Mon Nov 21, 2016 5:43 am

I am wondering how i send a http request every 5 seconds (or so)? My current approach to send a http request:

Code: Select all


LOCAL void 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.1\r\nUser-Agent: curl/7.37.0\r\nHost: %s\r\nAccept: */*\r\n\r\n",DOMAIN);
   espconn_send(conn,requestText,SIZE);
}

LOCAL void ICACHE_FLASH_ATTR
tcp_sent_cb(void *arg){
   os_printf("Send command is out.\n");
}

LOCAL void ICACHE_FLASH_ATTR
tcp_recv_cb(void *arg, char *data, unsigned short len){
   struct espconn *conn = (struct espconn*)arg;
   //do some stuff
}

LOCAL void ICACHE_FLASH_ATTR
tcp_recon_cb(void *arg, sint8 err){
   os_printf("TCP reconnected %d\n", err);
}

LOCAL void ICACHE_FLASH_ATTR
tcp_discon_cb(void *arg){
   os_printf("TCP disconnected.\n");
}

LOCAL void 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);
}

LOCAL void ICACHE_FLASH_ATTR
check_ip(void){
   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
      }
   }
}

LOCAL void 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();
}


So how can i repeatedly send that http request in the "tcp_connect_cb"? Any help is very much appreciated!

druiz
Posts: 5
Joined: Wed Nov 02, 2016 9:11 pm

Re: How to send a http request every 5 seconds?

Postby druiz » Mon Nov 21, 2016 3:51 pm

You can use software timers. It executes a callback every X seconds.

Code: Select all

xTimerHandle xTimers;
void vTimerCallback(xTimerHandle xTimer) {...} // That is the callback function.
xTimers = xTimerCreate("Timer", (10 / portTICK_RATE_MS), pdTRUE, (void *) 0,vTimerCallback);

And then, you can use:

Code: Select all

xTimerStop( xTimers, 0);
xTimerStart( xTimers, 0);
xTimerReset( xTimers, 0);

Take a look to the API: http://www.freertos.org/FreeRTOS-Software-Timer-API-Functions.html

Hope it will helps you!

Who is online

Users browsing this forum: No registered users and 58 guests