Page 1 of 1

the keep alive seem failed.

Posted: Thu Nov 26, 2015 6:22 pm
by wjzhang
I meet a problem. i want ESP8266 keep alive about 30mins. but always get 20s to disconnect. SDK: 1.41 NO OS :(

here code for set alive .
esp_gateway_tcp_connected(void *arg)
uint32_t keeplive;
pclientConnect = (struct espconn *)arg;

//set keepalive: 30mins=1800s = 1500 + 10*30
keeplive = 3000; //1500s: 500ms*3000
espconn_set_keepalive(pclientConnect, ESPCONN_KEEPIDLE, &keeplive);
keeplive = 60; //again 30s: 500ms*60
espconn_set_keepalive(pclientConnect, ESPCONN_KEEPINTVL, &keeplive);
keeplive = 10; //try times
espconn_set_keepalive(pclientConnect, ESPCONN_KEEPCNT, &keeplive);
espconn_set_opt(pclientConnect, ESPCONN_KEEPALIVE);
espconn_set_opt(pclientConnect, ESPCONN_NODELAY);
espconn_set_opt(pclientConnect, ESPCONN_COPY);

//register write finish call back
tcpTxCounter = 0;
tcpTxReadIndex = 0;
tcpTxWriteIndex = 0;
tcpTxInusing = false;
espconn_regist_write_finish(pclientConnect, esp_gateway_tcp_next_send);

/*Send OnCconnected package to MCU*/
//stop advise

PC side(using javascript Nodejs v0.10) also set to
//set TCP keepalive to 30 min
remoteDevice.socket.setKeepAlive(true, 1800000);

Re: the keep alive seem failed.

Posted: Fri Nov 27, 2015 12:18 pm
by ESP_Faye

Sorry that I'm confused about your question.

You mean that if the TCP connection broke, ESP8266 can know it immediately, need not to wait 30 minutes ?
Or you mean that the TCP connection does not break, but ESP8266 seems to always disconnect the TCP connection after 20s ?

Re: the keep alive seem failed.

Posted: Mon Nov 30, 2015 8:19 am
by wjzhang
the esp8266 broken the TCP connection when the TCP connetcion idle about 20s. the TCP FIN start from ESP8266.
i want keep TCP alive about 30 minutes when TCP no data send or receive.

Re: the keep alive seem failed.

Posted: Mon Nov 30, 2015 4:20 pm
by wjzhang
Hi Faye:
Sorry for confuse, it's 2 split issues.
1) if it not call espconn_regist_time(), it should keep TCP alive about 2 hours, why now became 20 seconds?
and why the timeout is 383s after i call espconn_regist_time(&tcpconnect, 180, 0), do you think it deviation too large?

2) do the espconn_set_keepalive() function time uint change to 1 second from 500ms?

Re: the keep alive seem failed.

Posted: Wed Dec 02, 2015 10:32 am
by ESP_Faye

Please update the lib LWIP as the attachment, based on ESP8266_NONOS_SDK_V1.5.0.

1. If did not call espconn_regist_time, the time out will be about 9~10s.
2. The unit of espconn_set_keepalive is 1 second, not 500 ms.

Code: Select all

keeplive = 60; //60s
espconn_set_keepalive(pclientConnect, ESPCONN_KEEPINTVL, &keeplive);

3. You need to call espconn_set_opt to enable TCP keep alive first, then call espconn_set_keepalive to set the configuration.

Code: Select all

uint32_t keeplive;

espconn_set_opt(pesp_conn, ESPCONN_KEEPALIVE); // enable TCP keep alive

//set keepalive: 75s = 60 + 5*3
keeplive = 60;
espconn_set_keepalive(pesp_conn, ESPCONN_KEEPIDLE, &keeplive);
keeplive = 5;
espconn_set_keepalive(pesp_conn, ESPCONN_KEEPINTVL, &keeplive);
keeplive = 3; //try times
espconn_set_keepalive(pesp_conn, ESPCONN_KEEPCNT, &keeplive);

Thanks for your interest in ESP8266 !

Re: the keep alive seem failed.

Posted: Fri Dec 04, 2015 3:35 pm
by wjzhang
Hi Faye:
what's the time unit of espconn_regist_time()? 1 second?
in my test result, it show the unit is about 2 second. :o
both in SDK 1.4.1 and 1.5.0

Re: the keep alive seem failed.

Posted: Fri Dec 04, 2015 4:32 pm
by ESP_Faye

Did you use the "" above ?

Re: the keep alive seem failed.

Posted: Mon Dec 07, 2015 8:46 am
by wjzhang
Hi Faye:
sorry, i don't known "", but i use No OS SDK. it's and

Re: the keep alive seem failed.

Posted: Wed Dec 09, 2015 2:46 pm
by ESP_Faye

Please have a try with the "" I provided above.

If your problem is still unsolved, please feel free to let us know.

Re: the keep alive seem failed.

Posted: Fri Mar 31, 2017 3:39 am
by pwolf
I am using the ESP8266 in a Arduino enviroment and I have multiple clients connecting to the ESP8266 and sometimes a client can drop off unexpectedly and the TCP socket remains open. I am looking for a way to implement a keepalive function and I found this post. I am not sure how to use/implement the keepalive in the Arduino enviroment, any help would be great.