*** 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:
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:
// 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