TCP Client Connection breaks after AP reconnect

muris
Posts: 35
Joined: Fri Oct 24, 2014 7:25 pm
Location: Bosnia-Herzegovina
Contact:

TCP Client Connection breaks after AP reconnect

Postby muris » Wed Nov 05, 2014 2:29 am

Hello,

I made a custom firmware to connect to TCP Socket Server and everything works great until WIFI loses connection and reconnects again. I will try to explain:

1. ESP8266EX starts up, and when it gets connected to my AP (I check in timer when wifi_station_get_connect_status() == STATION_GOT_IP because there is no WIFI CONNECTION CALLBACK) then I create a TCP connection by calling my custom function like so:

Code: Select all

struct espconn *ctrlConn; // global variable holding connection

// creates a TCP connection and starts connecting
static void ICACHE_FLASH_ATTR tcp_connection_create(void)
{
   enum espconn_type linkType = ESPCONN_TCP;
   ctrlConn = (struct espconn *)os_zalloc(sizeof(struct espconn));

   ctrlConn->type = linkType;
   ctrlConn->state = ESPCONN_NONE;
   ctrlConn->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
   ctrlConn->proto.tcp->local_port = espconn_port();
   ctrlConn->proto.tcp->remote_port = ctrlSetup.serverPort;
   os_memcpy(ctrlConn->proto.tcp->remote_ip, ctrlSetup.serverIp, 4);
   ctrlConn->reverse = NULL; // I don't need this, right?
   espconn_regist_connectcb(ctrlConn, tcpclient_connect_cb);
   espconn_regist_reconcb(ctrlConn, tcpclient_recon_cb);

   espconn_connect(ctrlConn);
   uart0_sendStr("TCP connection created.\r\n");
}


When callback function tcpclient_connect_cb() executes, then I start communicating with the server and everything works great without problems.

2. Now, when I turn my AP off, the ESP8266EX detects the connection break and calls a callback function tcpclient_recon_cb(). That function gets parameters: sint8 errType which has value -11 (ESPCONN_CONN), and the state of espconn (arg->State is 6 ... ESPCONN_CLOSE). In that function I call my other function to destroy the connection because I will create it again when AP gets connected again:

Code: Select all

// closes and destroys current connection
static void ICACHE_FLASH_ATTR tcp_connection_destroy(void)
{
   uart0_sendStr("tcp_connection_destroy()\r\n");

   ctrlState &= ~CTRL_STATE_AUTHENTICATED; // my custom variables
   connState = CTRL_TCP_DISCONNECTED; // my custom variables

   espconn_disconnect(ctrlConn); // this will os_free() my ctrlConn so I can create it again by calling tcp_connection_create(); when WIFI gets IP again
}


3. When I turn my AP back ON, after few seconds the ESP8266EX will get connected automatically and it will get IP. My timer will detect that it got IP again and will start the process of connecting a TCP client. Now it connects to TCP successfully, and communication works, but after ~30 seconds I get unexpected TCP connection break and call to my tcpclient_recon_cb() function. Again, it gets parameters sint8 errType which has value -11 (ESPCONN_CONN), and the state of espconn (arg->State is 6 ... ESPCONN_CLOSE). After that I destroy the current connection and recreate it again but from then it just hangs and doesn't work.

What is happening and why does ESP8266EX break the TCP Client connection after few seconds when it gets IP from WIFI AP once again?

Here is my full code if anyone is interested: https://github.com/elektronika-ba/ctrl-esp8266-test
My TCP Client code is in "user/ctrl_platform.c".

I use lubuntu and SDK v0.9.2 to compile everything.

Please HELP because I am going crazy here for three days now!!! :cry:

jackon
Posts: 28
Joined: Thu Oct 23, 2014 9:05 am

Re: TCP Client Connection breaks after AP reconnect

Postby jackon » Wed Nov 05, 2014 7:14 pm

hi, muris
espconn_disconnect(ctrlConn) will not free your ctrlConn internal, you should call os_free(ctrlConn) by yourself.
pls post your log here, we will take a look.

muris
Posts: 35
Joined: Fri Oct 24, 2014 7:25 pm
Location: Bosnia-Herzegovina
Contact:

Re: TCP Client Connection breaks after AP reconnect

Postby muris » Wed Nov 05, 2014 7:46 pm

I will try tonight and let you know what happens!

You advise that I do this:

Code: Select all

espconn_disconnect(ctrlConn);
os_free(ctrlConn); // new line of code

In the meantime, if my method is wrong, do you have any pseudocode of how I should:

1. create a TCP client connection
2. in case of connection break (tcp reconnect callback) how do I re-create the connection
3. in case of connection close (normal closing by server) how do I re-create the connection

I need a TCP Client socket connection that is always connected!

Thanks.

jackon
Posts: 28
Joined: Thu Oct 23, 2014 9:05 am

Re: TCP Client Connection breaks after AP reconnect

Postby jackon » Wed Nov 05, 2014 8:13 pm

hi, muris
You can refer to user_esp_platform.c in IoT_Demo.

muris
Posts: 35
Joined: Fri Oct 24, 2014 7:25 pm
Location: Bosnia-Herzegovina
Contact:

Re: TCP Client Connection breaks after AP reconnect

Postby muris » Wed Nov 05, 2014 8:34 pm

Thanks, it has everything I need. I don't know why I didn't investigate it in detail before :(

muris
Posts: 35
Joined: Fri Oct 24, 2014 7:25 pm
Location: Bosnia-Herzegovina
Contact:

Re: TCP Client Connection breaks after AP reconnect

Postby muris » Thu Nov 06, 2014 3:47 am

Hi jackon,

I changed my code according to user_esp_platform.c and I still have the problem with WIFI loosing connection every ~22 seconds after I turn my Access Point OFF and then ON. If I never turn OFF/ON my Access Point the module works without problems. But as soon as it looses WIFI connection and reconnects, it keeps reconnecting automatically every ~22 seconds for some reason.

I uploaded my changed code to https://github.com/elektronika-ba/ctrl- ... form.c#L42 and here is my log file: http://www.ctrl.ba/20141105202427.txt

The ESP8266EX is always in STATION_MODE and I never change the mode during operation.

I don't know what I am doing wrong as everything is like in "user_esp_platform.c".

Thanks in advance!

muris
Posts: 35
Joined: Fri Oct 24, 2014 7:25 pm
Location: Bosnia-Herzegovina
Contact:

Re: TCP Client Connection breaks after AP reconnect

Postby muris » Fri Nov 07, 2014 1:04 am

Can someone please help, maybe you can check what happens once your module looses WIFI? Does it reconnect every ~22 seconds?
Thanks

muris
Posts: 35
Joined: Fri Oct 24, 2014 7:25 pm
Location: Bosnia-Herzegovina
Contact:

Re: TCP Client Connection breaks after AP reconnect

Postby muris » Sat Nov 08, 2014 4:35 pm

It seems that this is not a TCP connection problem but a WIFI problem. As far as I can tell, there is no way to remedy this issue. This is what actually happen:

1. When module connects to AP for the first time after power-up, everything works perfect
2. When I shut down my AP the module detects and starts reconnecting internally...
3. When I turn my AP back ON, the module reconnects and detects that it is connected, and that it got IP.
4. Now after ~59seconds the module looses WIFI connection and reconnects. This happens forever...

Did anyone else try shutting their AP down while module is in STATION mode and currently connected to WIFI? Now that I think of it, maybe it is my AP that is causing this problem, I can't be sure.

I opened another thread for this issue here: viewtopic.php?f=7&t=33

Who is online

Users browsing this forum: No registered users and 215 guests