Accepting incoming tcp connections on the esp8266 (trivial example)

I’ve been working on some code that accepts incoming connections on the esp8266, if your looking for a full HTTP server take a look at sprite_tm’s code. But these are my notes on accepting a TCP connection.

As with TCP transmissions, you register a bunch of callbacks which process connections. So the first step is to initialize the TCP system with your connect callback:

#include "ets_sys.h"
#include "osapi.h"
#include "os_type.h"

static struct espconn httpconfig_conn;
static esp_tcp httpconfig_tcp_conn;

void ICACHE_FLASH_ATTR httpconfig_conn_init() {


        espconn_regist_connectcb(&httpconfig_conn, httpconfig_connected_cb);

When a connection is received on the specified port, httpconfig_connected_cb will be called. In this function you can send data to the client, and register other functions to trigger when data is received/disconnect etc.

static void ICACHE_FLASH_ATTR httpconfig_recv_cb(void *arg, char *data, unsigned short len) {
  struct espconn *conn=(struct espconn *)arg;


static void ICACHE_FLASH_ATTR httpconfig_recon_cb(void *arg, sint8 err) {

static void ICACHE_FLASH_ATTR httpconfig_discon_cb(void *arg) {

static void ICACHE_FLASH_ATTR httpconfig_sent_cb(void *arg) {

static void ICACHE_FLASH_ATTR httpconfig_connected_cb(void *arg) {
  struct espconn *conn=arg;

  espconn_regist_recvcb  (conn, httpconfig_recv_cb);
  espconn_regist_reconcb (conn, httpconfig_recon_cb);
  espconn_regist_disconcb(conn, httpconfig_discon_cb);
  espconn_regist_sentcb  (conn, httpconfig_sent_cb);

  char *transmission = "OK\r\n\r\nOK!\n";
  sint8 d = espconn_sent(conn,transmission,strlen(transmission));

I currently don’t fully understand what the reconnect callback does. If you know, please mail me.