Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does ESPAsyncTCP support loopback interface? #114

Closed
rozpuszczalny opened this issue Jul 16, 2019 · 2 comments
Labels

Comments

@rozpuszczalny
Copy link

@rozpuszczalny rozpuszczalny commented Jul 16, 2019

I'm trying to set TCP Server and TCP Client on the same ESP8266. I've tried to combine Server and Client examples, without success (error from clients onError callback is ERR_USE (-8)) . I'm wondering if it's ever possible with this library?

#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <DNSServer.h>
#include <vector>

#define TCP_PORT 7050

extern "C"
{
#include <osapi.h>
#include <os_type.h>
}

static os_timer_t intervalTimer;

static void replyToServer(void *arg)
{
  AsyncClient *client = reinterpret_cast<AsyncClient *>(arg);

  // send reply
  if (client->space() > 32 && client->canSend())
  {
    char message[32];
    sprintf(message, "this is from %s", WiFi.localIP().toString().c_str());
    client->add(message, strlen(message));
    client->send();
  }
}

/* event callbacks */
static void handleClientData(void *arg, AsyncClient *client, void *data, size_t len)
{
  Serial.printf("\n data received from %s \n", client->remoteIP().toString().c_str());
  Serial.write((uint8_t *)data, len);

  os_timer_arm(&intervalTimer, 2000, true); // schedule for reply to server at next 2s
}

void onConnect(void *arg, AsyncClient *client)
{
  Serial.printf("\n client has been (dis)connected to %s on port %d \n", WiFi.localIP().toString().c_str(), TCP_PORT);
  replyToServer(client);
}

void onError(void *arg, AsyncClient *client, int8_t error)
{
  Serial.printf("\n client error %d \n", error);
}

static std::vector<AsyncClient *> clients; // a list to hold all clients

/* clients events */
static void handleError(void *arg, AsyncClient *client, int8_t error)
{
  Serial.printf("\n connection error %s from client %s \n", client->errorToString(error), client->remoteIP().toString().c_str());
}

static void handleData(void *arg, AsyncClient *client, void *data, size_t len)
{
  Serial.printf("\n data received from client %s \n", client->remoteIP().toString().c_str());
  Serial.write((uint8_t *)data, len);

  // reply to client
  if (client->space() > 32 && client->canSend())
  {
    char reply[32];
    sprintf(reply, "this is from %s", WiFi.localIP().toString().c_str());
    client->add(reply, strlen(reply));
    client->send();
  }
}

static void handleDisconnect(void *arg, AsyncClient *client)
{
  Serial.printf("\n client %s disconnected \n", client->remoteIP().toString().c_str());
}

static void handleTimeOut(void *arg, AsyncClient *client, uint32_t time)
{
  Serial.printf("\n client ACK timeout ip: %s \n", client->remoteIP().toString().c_str());
}

/* server events */
static void handleNewClient(void *arg, AsyncClient *client)
{
  Serial.printf("\n new client has been connected to server, ip: %s", client->remoteIP().toString().c_str());

  // add to list
  clients.push_back(client);

  // register events
  client->onData(&handleData, NULL);
  client->onError(&handleError, NULL);
  client->onDisconnect(&handleDisconnect, NULL);
  client->onTimeout(&handleTimeOut, NULL);
}

void setup()
{
  Serial.begin(115200);
  delay(20);

  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print('.');
    delay(500);
  }

  AsyncServer *server = new AsyncServer(TCP_PORT); // start listening on tcp port 7050
  server->onClient(&handleNewClient, server);
  server->begin();

  AsyncClient *client = new AsyncClient;
  client->onData(&handleClientData, client);
  client->onConnect(&onConnect, client);
  client->onDisconnect(&onConnect, client);
  client->onError(&onError, client);
  client->connect(WiFi.localIP().toString().c_str(), TCP_PORT);

  os_timer_disarm(&intervalTimer);
  os_timer_setfn(&intervalTimer, &replyToServer, client);
}

void loop()
{
}

Build flags:

build_flags = -DLWIP_NETIF_LOOPBACK_MULTITHREADING=1 -DLWIP_HAVE_LOOPIF=1 -DLWIP_LOOPBACK_MAX_PBUFS=10 -DLWIP_NETIF_LOOPBACK=1
@stale

This comment has been minimized.

Copy link

@stale stale bot commented Sep 21, 2019

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Sep 21, 2019
@stale

This comment has been minimized.

Copy link

@stale stale bot commented Oct 5, 2019

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this Oct 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.