diff options
| author | schererleander <leander@schererleander.de> | 2026-01-20 08:34:54 +0100 |
|---|---|---|
| committer | schererleander <leander@schererleander.de> | 2026-01-20 08:34:54 +0100 |
| commit | 85ea4e995a75abe061f6fc375ea0481084dddd43 (patch) | |
| tree | 7eb5d57653ecd8f041aeac4e68d7d554c1168681 /libraries/ESPAsyncTCP/examples | |
Diffstat (limited to 'libraries/ESPAsyncTCP/examples')
5 files changed, 235 insertions, 0 deletions
diff --git a/libraries/ESPAsyncTCP/examples/ClientServer/Client/Client.ino b/libraries/ESPAsyncTCP/examples/ClientServer/Client/Client.ino new file mode 100644 index 0000000..b30d791 --- /dev/null +++ b/libraries/ESPAsyncTCP/examples/ClientServer/Client/Client.ino @@ -0,0 +1,62 @@ +#include <ESP8266WiFi.h> +#include <ESPAsyncTCP.h> + +extern "C" { +#include <osapi.h> +#include <os_type.h> +} + +#include "config.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 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); +} + +void loop() { + +} diff --git a/libraries/ESPAsyncTCP/examples/ClientServer/Client/config.h b/libraries/ESPAsyncTCP/examples/ClientServer/Client/config.h new file mode 100644 index 0000000..cf51e91 --- /dev/null +++ b/libraries/ESPAsyncTCP/examples/ClientServer/Client/config.h @@ -0,0 +1,23 @@ +#ifndef CONFIG_H +#define CONFIG_H + +/* + * This example demonstrate how to use asynchronous client & server APIs + * in order to establish tcp socket connections in client server manner. + * server is running (on port 7050) on one ESP, acts as AP, and other clients running on + * remaining ESPs acts as STAs. after connection establishment between server and clients + * there is a simple message transfer in every 2s. clients connect to server via it's host name + * (in this case 'esp_server') with help of DNS service running on server side. + * + * Note: default MSS for ESPAsyncTCP is 536 byte and defualt ACK timeout is 5s. +*/ + +#define SSID "ESP-TEST" +#define PASSWORD "123456789" + +#define SERVER_HOST_NAME "esp_server" + +#define TCP_PORT 7050 +#define DNS_PORT 53 + +#endif // CONFIG_H diff --git a/libraries/ESPAsyncTCP/examples/ClientServer/Server/Server.ino b/libraries/ESPAsyncTCP/examples/ClientServer/Server/Server.ino new file mode 100644 index 0000000..c8c9b7f --- /dev/null +++ b/libraries/ESPAsyncTCP/examples/ClientServer/Server/Server.ino @@ -0,0 +1,73 @@ +#include <ESP8266WiFi.h> +#include <ESPAsyncTCP.h> +#include <DNSServer.h> +#include <vector> + +#include "config.h" + +static DNSServer DNS; + +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", SERVER_HOST_NAME); + 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); + + // create access point + while (!WiFi.softAP(SSID, PASSWORD, 6, false, 15)) { + delay(500); + } + + // start dns server + if (!DNS.start(DNS_PORT, SERVER_HOST_NAME, WiFi.softAPIP())) + Serial.printf("\n failed to start dns service \n"); + + AsyncServer* server = new AsyncServer(TCP_PORT); // start listening on tcp port 7050 + server->onClient(&handleNewClient, server); + server->begin(); +} + +void loop() { + DNS.processNextRequest(); +} diff --git a/libraries/ESPAsyncTCP/examples/ClientServer/Server/config.h b/libraries/ESPAsyncTCP/examples/ClientServer/Server/config.h new file mode 100644 index 0000000..cf51e91 --- /dev/null +++ b/libraries/ESPAsyncTCP/examples/ClientServer/Server/config.h @@ -0,0 +1,23 @@ +#ifndef CONFIG_H +#define CONFIG_H + +/* + * This example demonstrate how to use asynchronous client & server APIs + * in order to establish tcp socket connections in client server manner. + * server is running (on port 7050) on one ESP, acts as AP, and other clients running on + * remaining ESPs acts as STAs. after connection establishment between server and clients + * there is a simple message transfer in every 2s. clients connect to server via it's host name + * (in this case 'esp_server') with help of DNS service running on server side. + * + * Note: default MSS for ESPAsyncTCP is 536 byte and defualt ACK timeout is 5s. +*/ + +#define SSID "ESP-TEST" +#define PASSWORD "123456789" + +#define SERVER_HOST_NAME "esp_server" + +#define TCP_PORT 7050 +#define DNS_PORT 53 + +#endif // CONFIG_H diff --git a/libraries/ESPAsyncTCP/examples/SyncClient/SyncClient.ino b/libraries/ESPAsyncTCP/examples/SyncClient/SyncClient.ino new file mode 100644 index 0000000..6ecc525 --- /dev/null +++ b/libraries/ESPAsyncTCP/examples/SyncClient/SyncClient.ino @@ -0,0 +1,54 @@ +#ifdef ESP8266 +#include <ESP8266WiFi.h> +#include <ESP8266mDNS.h> +#include <ArduinoOTA.h> +#else +#include <ESP31BWiFi.h> +#endif +#include "ESPAsyncTCP.h" +#include "SyncClient.h" + +const char* ssid = "**********"; +const char* password = "************"; + +void setup(){ + Serial.begin(115200); + WiFi.begin(ssid, password); + if (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.printf("WiFi Failed!\n"); + return; + } + Serial.printf("WiFi Connected!\n"); + Serial.println(WiFi.localIP()); +#ifdef ESP8266 + ArduinoOTA.begin(); +#endif + + 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); + } +} + +void loop(){ +#ifdef ESP8266 + ArduinoOTA.handle(); +#endif +} |
