Chat freely about anything...

User avatar
By moun1234
#59726 I have to create multiple tcp clients in a single ESP8266 wifi board(Nodemcu) and i stuck with the method of using 2 espconn simultaneously. I need to have 2 Tcp clients that can connect to 2 servers on outside and send the data to them. For 1 connection it seem ok i can send and received data back and forth between 1 client and 1 server but when i try to use another client in this case conn2 it nothing happen. Is it the limit of esp8266 that can run only 1 tcp client the same time or is there any problem with my code? Please help me thank

Here are my functions:
Code: Select allstruct espconn conn1;
struct espconn conn2;
esp_tcp tcp1;
esp_tcp tcp2;
void connectCB(void *arg) {
   os_printf("We have connected\n");
}
void errorCB(void *arg, sint8 err) {
   os_printf("We have an error: %d\n", err);
}
void disconnectCB(void *arg) {
   os_printf("Disconnected\n");
}

/* |< Using conn1 connect to first port >| */
void connect1() {
   conn1.proto.tcp->remote_port = REMOTE_PORT;
   *((uint32 *)conn1.proto.tcp->remote_ip) = ipaddr_addr(REMOTE_IP);
   espconn_connect(&conn1);
   os_printf("Inside connect1\n");
}

/* |< Using conn2 connect to other port>| */
void connect2() {
   conn2.proto.tcp->remote_port = REMOTE_PORT2;
   *((uint32 *)conn2.proto.tcp->remote_ip) = ipaddr_addr(REMOTE_IP2);
   espconn_connect(&conn2);
   os_printf("Inside connect2\n");
}


Below is User_init:

Code: Select allvoid ICACHE_FLASH_ATTR user_init(void) {
    uart_param_t para;
   para.uart_baud_rate = UART_BAUD_RATE_115200;
   para.uart_xfer_bit = UART_XFER_8_BIT;
   para.uart_parity_mode = UART_PARITY_NONE;
   para.uart_stop_bit = UART_1_STOP_BIT;
   para.uart_flow_ctrl = UART_NONE_FLOW_CTRL;
   uart0_init(&para);

   /* TCP init */
        conn1.type = ESPCONN_TCP;
   conn1.state = ESPCONN_NONE;
   conn1.proto.tcp = &tcp1;
   espconn_regist_connectcb(&conn1, connectCB);
   espconn_regist_reconcb(&conn1, errorCB);
   espconn_regist_disconcb(&conn1,disconnectCB);
   
   conn2.type = ESPCONN_TCP;
   conn2.state = ESPCONN_NONE;
   conn2.proto.tcp = &tcp2;
   espconn_regist_connectcb(&conn2, connectcb);
   espconn_regist_reconcb(&conn2, errorcb);
   espconn_regist_disconcb(&conn2,disconnectCB);
   espconn_init();
}