ESPASync server + client

I have project with an ESPAsyncWebServer with websockets working already. I also need the device to operate as a client 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.

Code for what?

The example shows that the interface supports the printf() method so you can expect it to support print, println and write too.

I think it shows the concept and provides a quite useful example although it doesn't replace a documentation. But you have the full source code, that should help a lot.

If you need the asynchronous version you don't have to look at the SyncClient as that does wait for the response.

Just take care to keep the overview over all you asynchronous communication if you have server and client code in the same sketch.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.