Skip to content

Instantly share code, notes, and snippets.

@miceno
Created November 5, 2023 00:31
Show Gist options
  • Save miceno/f4c306a753e0111f988670e221a12aca to your computer and use it in GitHub Desktop.
Save miceno/f4c306a753e0111f988670e221a12aca to your computer and use it in GitHub Desktop.
Example of a persistent TCP server using ESP8266Wifi and ESPAsyncTCP
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#define SERVER_HOST_NAME "esp_server"
#define SSID "YOUR_SSID"
#define PASSWORD "YOUR_SSID_PASSWORD"
#define SERVER_HOST_NAME "esp_server"
#define TCP_PORT 8888
#define MAX_BAUD_RATE 115200
AsyncServer* server = NULL;
/* 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());
// register events
client->onData(&handleData, NULL);
client->onError(&handleError, NULL);
client->onDisconnect(&handleDisconnect, NULL);
client->onTimeout(&handleTimeOut, NULL);
}
void setup_wifi() {
// WiFi parameters
const char* ssid = SSID;
const char* password = PASSWORD;
//Static IP address configuration
IPAddress staticIP(192, 168, 1, 61); //ESP static ip
IPAddress gateway(192, 168, 1, 1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress dns(8, 8, 8, 8); //DNS
WiFi.disconnect(); // Prevent connecting to wifi based on previous configuration
ESP.eraseConfig();
WiFi.config(staticIP, subnet, gateway);
WiFi.mode(WIFI_STA); // WiFi mode station (connect to wifi router only
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.begin(ssid, password);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { // blink to indicate the device is trying to connect to WIFI
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("START MIRRORING SERIAL");
Serial.println(WiFi.localIP());
}
void setup_serial() {
Serial.begin(MAX_BAUD_RATE);
}
void setup() {
setup_serial();
setup_wifi();
server = new AsyncServer(TCP_PORT); // start listening on tcp port 7050
server->onClient(&handleNewClient, server);
server->begin();
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment