Hi together,
I'm happy to mention, that I found a solution:
I added a TCP server. The ESPAsyncWebServer seems not to be able to handle data send by a client. Please share the sketch, when you found a better solution 
Server sketch:
AsyncWebServer (with or without authentifikation; index.html is stored on 'SPIFFS'). Listening to client data at "TCP_PORT". Just reply to client with the same data (for commit on client side).
//AsyncWebServer
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <DNSServer.h>
#include <vector>
//WLAN SSID & Password
char* wlan_ssid = "YOURSSID";
char* wlan_password = "YOURPW";
//Access Point (on ESP8266)
#define SSID "TestDataServer"
#define PASSWORD "ESP8266xx" //Min. 8 chars, if used
#define SERVER_HOST_NAME "DataCollectionServer"
#define TCP_PORT 7050
#define DNS_PORT 53
static DNSServer DNS;
static std::vector<AsyncClient*> clients; // a list to hold all clients
String vClientData;
/* 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);
Serial.println("");
vClientData = String((char*)data).substring(0, len);
Serial.print("vClientData: ");
Serial.println(vClientData);
// reply to client
if (client->space() > 64 && client->canSend()) {
char reply[64];
//sprintf(reply, "this is from %s", SERVER_HOST_NAME);
//sprintf(reply, "Data received: %s From: %s",vClientData.c_str(), SERVER_HOST_NAME);
//sprintf(reply, "Data received: %s",vClientData.c_str());
sprintf(reply, "%s", vClientData.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()
{
//Debugging
Serial.begin(115200);
delay(200);
Serial.println("###Setup###");
// 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");
// start TCP server
AsyncServer* TCP_server = new AsyncServer(TCP_PORT); // start listening on tcp port 7050
TCP_server->onClient(&handleNewClient, TCP_server);
TCP_server->begin();
//WLAN + Web Server configuration
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
//Set WiFi to access point (client data collection) AND station mode (html web server)
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Connect Failed! Rebooting...");
delay(1000);
ESP.restart();
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Initialize SPIFFS
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
//With authentifikation
HTML_server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
if(!request->authenticate(http_username, http_password))
return request->requestAuthentication();
request->send(SPIFFS, "/index.html");
});
//No authentifikation
// // Route for root / web page
// HTML_server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
// request->send(SPIFFS, "/index.html");
// });
HTML_server.onNotFound([](AsyncWebServerRequest *request){
request->send(404);
});
// remove HTML_server.onNotFound handler
HTML_server.onNotFound(NULL);
HTML_server.begin();
Serial.println("###Setup end###");
}
void loop(void)
{
DNS.processNextRequest();
}
Client sketch:
The clients just sends the A0 value to the server. If the server sends back the same value, the client goes to deepsleep for 60s (connection between Pin1 and RESET necessary! Otherwise the ESP will not wake up in time :))
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
extern "C" {
#include <osapi.h>
#include <os_type.h>
}
#define SSID "TestDataServer"
#define PASSWORD "ESP8266xx"
#define SERVER_HOST_NAME "DataCollectionServer"
#define TCP_PORT 7050
#define DNS_PORT 53
static os_timer_t intervalTimer;
char message[64];
String vServerData = "empty";
//String vClientName = "Gurke";
static void replyToServer(void* arg) {
AsyncClient* client = reinterpret_cast<AsyncClient*>(arg);
// send reply
if (client->space() > 64 && client->canSend()) {
int sensorValue = analogRead(A0);
sprintf(message, "A0: %s From: %s",String(sensorValue).c_str(), 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 server %s \n", client->remoteIP().toString().c_str());
// Serial.write((uint8_t*)data, len);
// Serial.println("");
vServerData = String((char*)data).substring(0, len);
Serial.print("vServerData: ");
Serial.println(vServerData);
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(200);
Serial.println();
Serial.println("###Setup###");
// connects to access point
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
// Print local IP address and start web server
Serial.println("");
Serial.println("Connected to Access Point");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
AsyncClient* client = new AsyncClient;
int vDataSentSuccesfully = 0;
while(vDataSentSuccesfully == 0)
{
client->onData(&handleData, client);
client->onConnect(&onConnect, client);
client->connect(SERVER_HOST_NAME, TCP_PORT);
// //Compare vale sent (message) with value received (vServerData)
// Serial.print("Compare <");
// Serial.print(message);
// Serial.print("> with <");
// Serial.print(vServerData);
// Serial.println(">");
if(String(message) == vServerData)
{
Serial.println("###vDataSentSuccesfully###");
vDataSentSuccesfully = 1;
}
delay(1000);
}
os_timer_disarm(&intervalTimer);
os_timer_setfn(&intervalTimer, &replyToServer, client);
Serial.print("Uptime in ms: ");
Serial.println(millis());
Serial.println("###Setup end -> Deepsleep###");
//Deepsleep
client->stop();
ESP.deepSleep(60e6 - millis()*1000); //e6 = * 1.000.000
}
void loop() {
}
Please send me a PN, if you have any questions or if the code is not working.
Best regards
Noyen