When and how to use espconn_set_keepalive?

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

When and how to use espconn_set_keepalive?

Postby Stational » Tue Sep 13, 2016 11:02 pm

Hi,

i would like to establish a tcp connection which doesn't get closed after the first data send, i read about the espconn_set_keepalive function which sends heartbeats to the server so it doesn't disconnect (if i understood correctly). But i don't know when i should call the function? In the connectcb?

I tried it like this:

Code: Select all

LOCAL void ICACHE_FLASH_ATTR
tcp_connect_cb(void *arg){
   os_printf("We are connected!\n");
   sint8 ret = espconn_set_keepalive((struct espconn*)arg,ESPCONN_KEEPCNT,(void*)1000);
   if(ret==0)
      os_printf("Set keepalive.\n");
}


But the text didn't get printed on my serial terminal. What did I do wrong?

Any help is very much appreciated!

stupiid
Posts: 6
Joined: Tue Feb 02, 2016 5:29 pm

Re: When and how to use espconn_set_keepalive?

Postby stupiid » Wed Sep 14, 2016 11:44 am

I think you should call espconn_set_opt() first.

for example:

Code: Select all

   int keep_alive = 30;   //keep alive checking per 30s
   espconn_set_opt(my_espconn, ESPCONN_KEEPALIVE);
   espconn_set_keepalive(my_espconn, ESPCONN_KEEPIDLE, &keep_alive);
   keep_alive = 5; //repeat interval = 5s
   espconn_set_keepalive(my_espconn, ESPCONN_KEEPINTVL, &keep_alive);
   keep_alive = 2;//repeat 2times
   espconn_set_keepalive(my_espconn, ESPCONN_KEEPCNT, &keep_alive);


regards,

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

Re: When and how to use espconn_set_keepalive?

Postby Stational » Wed Sep 14, 2016 10:59 pm

Thanks for your answer.

Could you explain why i have to use all three of those keepalive functions? It also doesn't help to check every 30s if the connection is still established because the discon_cb is almost instantaneously called after espconn_send, maybe there are a couple of ms between sending something to a tcp server and the discon_cb.

Who is online

Users browsing this forum: No registered users and 56 guests