ESP8266 - Connect to TCP Server (in C)

0

I am currently trying to get the esp8266 to connect to my http server. Connecting to my local wifi network works but if I try to connect to my server I get this error on the terminal window:

Fatal exception 9(LoadStoreAlignmentCause): epc1=0x4026027b, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000011, depc=0x00000000

In the user_init function I call check_ip() which handles the tcp connection and looks like this:

LOCAL void ICACHE_FLASH_ATTR check_ip(void){
    struct espconn conn;
    struct ip_info ipconf;
    esp_tcp tcp;
    uint8 ipAddr[4] = {1,2,3,4};
    uint32 localPort = espconn_port();
    bool res = wifi_get_ip_info(STATION_IF, &ipconf);
    if(!res)
        os_printf("No Success.");
    else{
        os_memcpy(tcp.local_ip,&ipconf.ip,4);
        if(wifi_station_get_connect_status() == STATION_GOT_IP && ipconf.ip.addr != 0){
            os_printf("Got IP Address.\n");

            tcp.remote_port = 80;
            tcp.local_port = espconn_port();
            os_memcpy(tcp.remote_ip,ipAddr,4);

            conn.type = ESPCONN_TCP;
            conn.state = ESPCONN_NONE;
            conn.proto.tcp = &tcp;

            espconn_regist_connectcb(&conn,tcp_connect_cb);
            espconn_regist_reconcb(&conn,tcp_recon_cb);

            espconn_connect(&conn);
            os_timer_disarm(&timer);
        } else {
            os_timer_setfn(&timer,(os_timer_func_t*)check_ip,NULL);
            os_timer_arm(&timer,100,0); //recall function after 100ms, don't repeat
        }
    }
}

Any help is very much appreciated.

c
tcp
esp8266
asked on Stack Overflow Sep 12, 2016 by binaryBigInt • edited Sep 14, 2016 by 0ndre_

1 Answer

0

If I remember correctly, you need to dynamically allocate esp_conn,instead of using stack variable.

(deep inside, espconn_tcp_client(struct espconn *espconn) function does this: espconn_list_creat(&plink_active, pclient); pclient->pespconn = espconn; <---- it stores your pointer

Example: https://myesp8266.blogspot.com.cy/2015/03/publish-data-from-your-esp8266-to.html (just first in Google, but seems ok)

PS: IMO esp_conn_xxxx functions is a bit unreliable... I'd recommend to use LWIP.

answered on Stack Overflow Sep 13, 2016 by Alexander Alashkin

User contributions licensed under CC BY-SA 3.0