Chat freely about anything...

User avatar
By piontec
#31598 Hi,
I'm struggling with TCP connection on esp8266-12 with SDK 1.4. I spent
dozens of hours and it doesn't work. In a simpler project I used TCP exactly like this and it worked. Now i checked connection status, free memory, everything I could think of - and nothing. Could you please help me?

Here is what I'm doing:

1) I'm building HTTP query string.

2) I set up a
void wifi_set_event_handler_cb(wifi_event_handler_cb_t cb)
exactly as in espressif's 2C doc for sdk 1.4, p. 73

3) Then, I initialize TCP connection like this:
(Serial.printf is from Sming framework)
Code: Select allstruct espconn *pCon = (struct espconn *) os_zalloc(sizeof (struct espconn));
    if (pCon == NULL) {
        os_printf("Error: TCP connect failed - memory allocation for conn failed\n");
        return;
    }
    pCon->type = ESPCONN_TCP;
    uint32_t ip = ipaddr_addr("192.168.3.113");
    pCon->proto.tcp = (esp_tcp *) os_zalloc(sizeof (esp_tcp));
    if (pCon->proto.tcp == NULL) {
        os_printf("Error: TCP connect failed - memory allocation for TCP failed\n");
        return;
    }
    pCon->proto.tcp->local_port = espconn_port();
    pCon->proto.tcp->remote_port = lwip_htons(80);

    os_memcpy(pCon->proto.tcp->remote_ip, &ip, 4);

    espconn_regist_connectcb(pCon, at_tcpclient_connect_cb);
    espconn_regist_reconcb(pCon, at_tcpclient_reconnect_cb);
    Serial.printf("Free mem: %d\r\n", system_get_free_heap_size());
    Serial.printf("Connection status: %d\n", wifi_station_get_connect_status ());
    Serial.printf("TCP connecting...\n");
    sint8 res = espconn_connect(pCon);
    Serial.printf ("result %d\n", res);


4) The reconnect callback is the only one callback I get - it's code is:
Code: Select allstatic void ICACHE_FLASH_ATTR at_tcpclient_reconnect_cb(void *arg,
sint8 errType) {
    Serial.printf("Free mem: %d, err %d\r\n", system_get_free_heap_size(), errType);
    struct espconn *pespconn = (struct espconn *) arg;
    espconn_delete(pespconn);
    Serial.printf("Reconnect callback - retry... ");
    os_delay_us(1 * 1000 * 1000);
    Serial.printf("now.\n");
    submitData();
}

Now, the problem is, that the TCP is never successful - I only get TCP
reconnect callback. The output on serial is like this:

Code: Select allscandone
�хѕ� 2 -> 3 (0)b0)
state: 3 -> 5 (10)
add 0
aid 4
cnt

connecte�x�ѡ konet�6�������
dh�|z��[���х�ѹ..
event 0
*** connect �
             ���|�����l�ha�nY
                             �7ip:192��3.790,mask:255.255.255.0,gw:192.168.3.1
event 3
*** ip:192.168.3.190,mask:255.255.255.0,gw:192.168.3.1
Free mem: 35784
Connection status: 5
                    TCP connecting...
                                     result 0
                                             Free mem: 35784, err -9
Reconnect callback - retry... now.


So, my TCP connection is successful (espconn_connect == 0), I got IP
address (status == 5), I got over 30kB of free mem, but every time I try to connect I get error with TCP_RESET (err
== -9 in callback).

Does anybody have any idea what am I doing wrong or what more can I check?
User avatar
By smorg
#31713 Hi.

It would help if we could get the complete code, or at least the connect callback source.

And I think you should avoid long delays, since the TCP stack does not work preemptive: a long delay like one second will block networking.
Code: Select all...
    os_delay_us(1 * 1000 * 1000);
...


Since you get a TCP_RESET, you could have a timeout already.