Discuss here different C compiler set ups, and compiling executables for the ESP8266

User avatar
By cyborgmax
#2391 I am trying to set up a TCP Server, without using AT, I can get the server connected and then it disconnects with timout error, I think it's because I haven't set the connect_callback function so the server desn't do anything when it connects, am I right?. Then, I'm trying to set up the function but i get a lot of undeclared and function type errors.
This is the TCP Server code:

struct espconn pTcpServer;
pTcpServer.type = ESPCONN_TCP;
pTcpServer.state = ESPCONN_NONE;
pTcpServer.proto.tcp->local_port = 2000;
espconn_regist_connectcb(&pTcpServer, myfunction); // I understand that this is the function that it will call when it connects?? this is where I get errors, can someone explain me how this would work?
// espconn_regist_recvcb(&pTcpServer, data_received);
espconn_accept(&pTcpServer);
espconn_regist_time(&pTcpServer, 180, 0);

The fact that there's barely any documentation and that the examples out there are too complex, it makes hard to create a new program step by step.
I wish in this forum we could have more examples/documentation on this steps (eg, tcp server examples, wifi examples, data send/receive examples, etc)

Any help will be appreciated.
Thanks
In this case a simple TCP Server example would help me a lot with this?
User avatar
By 0ff
#2409 This is a very simple tcp server example:
Code: Select allLOCAL uint16_t server_timeover = 60*60*12; // yes. 12h timeout. so what? :)
LOCAL struct espconn masterconn;

const char *msg_welcome = "Welcome!\n\n";


LOCAL void ICACHE_FLASH_ATTR
shell_tcp_disconcb(void *arg) {
    struct espconn *pespconn = (struct espconn *) arg;

    os_printf("tcp connection disconnected\n");
}

LOCAL void ICACHE_FLASH_ATTR
shell_tcp_recvcb(void *arg, char *pusrdata, unsigned short length)
{
    struct espconn *pespconn = (struct espconn *) arg;

    if(length >= 2 && pusrdata[0] == 0xFF && (pusrdata[1] & 0xF0) == 0xF0) {       
        espconn_sent(pespconn, (uint8*)msg_welcome, os_strlen(msg_welcome));
    } else {
        os_printf(">'%s' ", pusrdata);

        int i;
        for(i = 0; i < length; i++) {
            os_printf("0x%02X ", pusrdata[i]);
        }
        os_printf("\n");
        // espconn_sent(pespconn, pusrdata, length); //echo
    }
}

LOCAL void ICACHE_FLASH_ATTR
tcpserver_connectcb(void *arg)
{
    struct espconn *pespconn = (struct espconn *)arg;

    os_printf("tcp connection established\n");

    espconn_regist_recvcb(pespconn, shell_tcp_recvcb);
    // espconn_regist_reconcb(pespconn, tcpserver_recon_cb);
    espconn_regist_disconcb(pespconn, shell_tcp_disconcb);
    // espconn_regist_sentcb(pespconn, tcpclient_sent_cb);
}

void ICACHE_FLASH_ATTR
shell_init(void)
{
    masterconn.type = ESPCONN_TCP;
    masterconn.state = ESPCONN_NONE;
    masterconn.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
    masterconn.proto.tcp->local_port = 23;
    espconn_regist_connectcb(&masterconn, tcpserver_connectcb);
    espconn_accept(&masterconn);
    espconn_regist_time(&masterconn, server_timeover, 0);

    os_printf("[%s] initializing shell!\n", __func__);
}


You might need any of these imports:
Code: Select all#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
#include "lwip/stats.h"

#include "espconn.h"
Last edited by 0ff on Tue Nov 11, 2014 4:51 am, edited 1 time in total.
User avatar
By cyborgmax
#2428 Thank you very much for your help, I was pulling my hair here for days trying to get it to compile without errors, I will try it out later when I get home, I understand now the proper way to configure the TCP Server. Thank you again. I really appreciate it.