Chat freely about anything...

User avatar
By elektronika_ba
#2256 Hi everyone,

*** UPDATE: Click here if you don't want to read all this stuff ***

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 allstruct 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.

Any ideas? I am going crazy here :x
Last edited by elektronika_ba on Mon Nov 10, 2014 9:43 am, edited 2 times in total.
User avatar
By elektronika_ba
#2262 It turns out that espconn_disconnect() doesn't os_free() the connection for us so that might be the problem I am having.
I don't have the hardware until tonight to test, but looking at the espconn.c implementation it looks promising that os_free() will solve my problem mainly because of how
Code: Select allbool ICACHE_FLASH_ATTR espconn_find_connection(struct espconn *pespconn, espconn_msg **pnode)

works. It looks for the connection that has the same remote IP and port and returns it. All the connections I create and don't destroy have the same remote IP and port so that's probably it.
User avatar
By elektronika_ba
#2327 Ok guys, this is actually 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.