ESP8266 - 连接到TCP服务器(在C中)

时间:2016-09-12 14:50:26

标签: c tcp esp8266

我目前正在尝试让esp8266连接到我的http服务器。连接到我的本地wifi网络工作,但如果我尝试连接到我的服务器,我在终端窗口上收到此错误:

  

致命异常9(LoadStoreAlignmentCause):   epc1 = 0x4026027b,epc2 = 0x00000000,epc3 = 0x00000000,excvaddr = 0x00000011,depc = 0x00000000

user_init函数中,我调用check_ip()来处理tcp连接,如下所示:

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
        }
    }
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果我没记错的话,你需要动态分配esp_conn,而不是使用堆栈变量。

(内心深处,espconn_tcp_client(struct espconn * espconn)函数执行此操作: espconn_list_creat(&plink_active, pclient); pclient->pespconn = espconn; <---- it stores your pointer

示例:https://myesp8266.blogspot.com.cy/2015/03/publish-data-from-your-esp8266-to.html(刚好在谷歌,但似乎没问题)

PS:IMO esp_conn_xxxx功能有点不可靠......我建议使用LWIP。