1

I have project with an ESPAsyncWebServer with websockets working already. What I need to add is Client functionality to poll a site for a response. I looked at the ASyncTCP library and it has two different implementations that aren't well documented in how to use.

There's the SyncClient option that looks like this:

  SyncClient client;
  if(!client.connect("www.google.com", 80)){
    Serial.println("Connect Failed");
    return;
  }
  client.setTimeout(2);
  if(client.printf("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n") > 0){
    while(client.connected() && client.available() == 0){
      delay(1);
    }
    while(client.available()){
      Serial.write(client.read());
    }
    if(client.connected()){
      client.stop();
    }
  } else {
    client.stop();
    Serial.println("Send Failed");
    while(client.connected()) delay(0);
  }

I don't know if i'm supposed to put code after a client.connected() or client.available()? Of if I need the full structure as shown or just one of the commands. It also doesn't show all the commands and syntax available (such as an equivalent for client.write(), client.print or the like).

Then there is the simply named ClientServer > Client which only has the following:

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 handleData(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 connected to %s on port %d \n", SERVER_HOST_NAME, TCP_PORT);
    replyToServer(client);
}


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

    // connects to access point
    WiFi.mode(WIFI_STA);
    WiFi.begin(SSID, PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print('.');
        delay(500);
    }

    AsyncClient* client = new AsyncClient;
    client->onData(&handleData, client);
    client->onConnect(&onConnect, client);
    client->connect(SERVER_HOST_NAME, TCP_PORT);

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

This doesn't tell me a whole lot about how to use this library.

I need some guidance on how to deploy the client alongside my server. I don't need much, just listen for server directive, parse it and then send a response (client.println) or upload a file (client.write). I'd like to implement the asynchronous version of this.

2
  • It is actually well-documented and plenty of examples online on how to use it. client.connect() establish a TCP connection, it is up to you implement your own http request with client.print or client.printf in according to http protocol, you can add http headers, POST or GET as per your need. The http response can be read with client.read(), it is up to you on how you want to capture it (e.g. reading into a buffer and parse it) or simple print it on Serial Monitor(as show in the example code).
    – hcheung
    Feb 7 at 6:43
  • If you don't want to handle the http creation and parsing, you can use ESP8266HTTP client which provide simpler API.
    – hcheung
    Feb 7 at 6:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.