Remove old and modified libs, switch to ESPAsyncWebserver, add support for ESP32-H2 and multiple Ethernet modules (#455)
* Asyncwebserver * Squashed commit of the following: commit 575ef02f593918ec6654c87407a4d11fc17071b8 Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 17:56:11 2024 +0200 merge master commit 35e5adf4ecd80f9829e8801181f35dd2c1d94759 Merge: a2cc7be221adca01Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 17:41:04 2024 +0200 Merge branch 'master' of github.com:technyon/nuki_hub into DM9051 commit a2cc7be2954cbd8767ab8186296c0b14134d1d0b Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 10:51:50 2024 +0200 update nuki ble commit 20c809f3dca28b29b219d1ff3a183f1981316de5 Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 10:44:46 2024 +0200 backup commit dd41c218efb5270f5efeb734e64dff695920db16 Merge: 153000b5e84b944aAuthor: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 10:40:03 2024 +0200 Merge branch 'master' of github.com:technyon/nuki_hub into DM9051 commit 153000b5b1af7df1fbeb5263df94eb26f689cc0a Author: technyon <j.o.schuemann@gmx.de> Date: Mon Aug 12 10:23:07 2024 +0200 fix linker error commit a93bbfbfc4301e46ff3696a763dd13c6c89efefb Author: technyon <j.o.schuemann@gmx.de> Date: Sun Aug 11 11:27:07 2024 +0200 backup commit f611c75ce8c35f829bcad6cf7e86188f4b3ec331 Merge: f1964917063fbab6Author: technyon <j.o.schuemann@gmx.de> Date: Sun Aug 11 11:24:47 2024 +0200 merge master commit f1964917b4dade3920f1ecdb699c58630199e6da Author: technyon <j.o.schuemann@gmx.de> Date: Sat Aug 10 15:17:45 2024 +0200 update platformio.ini commit f448e5e8a7e93be38e09e2ab0b622199a3721af6 Author: technyon <j.o.schuemann@gmx.de> Date: Sat Aug 10 11:28:09 2024 +0200 add SPIClass instance for DM9051 commit 1f190e9aa08033535a2eb442a92e6e20409bbda1 Author: technyon <j.o.schuemann@gmx.de> Date: Sat Aug 10 11:22:26 2024 +0200 add definitions and constructor for DM9051 commit 726b3602ae91594ee1210ad5b6714f75cc5e42a7 Merge: 50a2eb134af90cbcAuthor: technyon <j.o.schuemann@gmx.de> Date: Sat Aug 10 10:19:34 2024 +0200 merge master commit 50a2eb136d75d90921f1c6974f18bc107bddc123 Author: technyon <j.o.schuemann@gmx.de> Date: Fri Aug 9 11:52:09 2024 +0200 add comment commit 9437e485cae169efdf8e5a7bf188a1c7e792d1e5 Author: technyon <j.o.schuemann@gmx.de> Date: Sun Aug 4 08:29:21 2024 +0200 move LAN8720 definitions to seperate file * Remove Core 2 Ethernet library * Custom Ethernet * GPIO and Preferences * H2
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
#include <DNSServer.h>
|
||||
#ifdef ESP32
|
||||
#include <AsyncTCP.h>
|
||||
#include <WiFi.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#elif defined(TARGET_RP2040)
|
||||
#include <WebServer.h>
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
#include "ESPAsyncWebServer.h"
|
||||
|
||||
DNSServer dnsServer;
|
||||
AsyncWebServer server(80);
|
||||
|
||||
class CaptiveRequestHandler : public AsyncWebHandler {
|
||||
public:
|
||||
CaptiveRequestHandler() {}
|
||||
virtual ~CaptiveRequestHandler() {}
|
||||
|
||||
bool canHandle(__unused AsyncWebServerRequest* request) {
|
||||
// request->addInterestingHeader("ANY");
|
||||
return true;
|
||||
}
|
||||
|
||||
void handleRequest(AsyncWebServerRequest* request) {
|
||||
AsyncResponseStream* response = request->beginResponseStream("text/html");
|
||||
response->print("<!DOCTYPE html><html><head><title>Captive Portal</title></head><body>");
|
||||
response->print("<p>This is out captive portal front page.</p>");
|
||||
response->printf("<p>You were trying to reach: http://%s%s</p>", request->host().c_str(), request->url().c_str());
|
||||
response->printf("<p>Try opening <a href='http://%s'>this link</a> instead</p>", WiFi.softAPIP().toString().c_str());
|
||||
response->print("</body></html>");
|
||||
request->send(response);
|
||||
}
|
||||
};
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
Serial.println("Configuring access point...");
|
||||
|
||||
if (!WiFi.softAP("esp-captive")) {
|
||||
Serial.println("Soft AP creation failed.");
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
|
||||
dnsServer.start(53, "*", WiFi.softAPIP());
|
||||
server.addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER); // only when requested from AP
|
||||
// more handlers...
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
dnsServer.processNextRequest();
|
||||
}
|
||||
37
lib/ESPAsyncWebServer/examples/Draft/Draft.ino
Normal file
37
lib/ESPAsyncWebServer/examples/Draft/Draft.ino
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "mbedtls/md5.h"
|
||||
#include <Arduino.h>
|
||||
#include <MD5Builder.h>
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(2000);
|
||||
|
||||
const char* data = "Hello World";
|
||||
|
||||
{
|
||||
uint8_t md5[16];
|
||||
mbedtls_md5_context _ctx;
|
||||
mbedtls_md5_init(&_ctx);
|
||||
mbedtls_md5_starts(&_ctx);
|
||||
mbedtls_md5_update(&_ctx, (const unsigned char*)data, strlen(data));
|
||||
mbedtls_md5_finish(&_ctx, md5);
|
||||
char output[33];
|
||||
for (int i = 0; i < 16; i++) {
|
||||
sprintf_P(output + (i * 2), PSTR("%02x"), md5[i]);
|
||||
}
|
||||
Serial.println(String(output));
|
||||
}
|
||||
|
||||
{
|
||||
MD5Builder md5;
|
||||
md5.begin();
|
||||
md5.add(data, strlen(data);
|
||||
md5.calculate();
|
||||
char output[33];
|
||||
md5.getChars(output);
|
||||
Serial.println(String(output));
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
111
lib/ESPAsyncWebServer/examples/Filters/Filters.ino
Normal file
111
lib/ESPAsyncWebServer/examples/Filters/Filters.ino
Normal file
@@ -0,0 +1,111 @@
|
||||
// Reproduced issue https://github.com/mathieucarbou/ESPAsyncWebServer/issues/26
|
||||
|
||||
#include <DNSServer.h>
|
||||
#ifdef ESP32
|
||||
#include <AsyncTCP.h>
|
||||
#include <WiFi.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#elif defined(TARGET_RP2040)
|
||||
#include <WebServer.h>
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
#include "ESPAsyncWebServer.h"
|
||||
|
||||
DNSServer dnsServer;
|
||||
AsyncWebServer server(80);
|
||||
|
||||
class CaptiveRequestHandler : public AsyncWebHandler {
|
||||
public:
|
||||
CaptiveRequestHandler() {}
|
||||
virtual ~CaptiveRequestHandler() {}
|
||||
|
||||
bool canHandle(__unused AsyncWebServerRequest* request) {
|
||||
// request->addInterestingHeader("ANY");
|
||||
return true;
|
||||
}
|
||||
|
||||
void handleRequest(AsyncWebServerRequest* request) {
|
||||
AsyncResponseStream* response = request->beginResponseStream("text/html");
|
||||
response->print("<!DOCTYPE html><html><head><title>Captive Portal</title></head><body>");
|
||||
response->print("<p>This is out captive portal front page.</p>");
|
||||
response->printf("<p>You were trying to reach: http://%s%s</p>", request->host().c_str(), request->url().c_str());
|
||||
response->printf("<p>Try opening <a href='http://%s'>this link</a> instead</p>", WiFi.softAPIP().toString().c_str());
|
||||
response->print("</body></html>");
|
||||
request->send(response);
|
||||
}
|
||||
};
|
||||
|
||||
bool hit1 = false;
|
||||
bool hit2 = false;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
server
|
||||
.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
Serial.println("Captive portal request...");
|
||||
Serial.println("WiFi.localIP(): " + WiFi.localIP().toString());
|
||||
Serial.println("request->client()->localIP(): " + request->client()->localIP().toString());
|
||||
#if ESP_IDF_VERSION_MAJOR >= 5
|
||||
Serial.println("WiFi.type(): " + String((int)WiFi.localIP().type()));
|
||||
Serial.println("request->client()->type(): " + String((int)request->client()->localIP().type()));
|
||||
#endif
|
||||
Serial.println(WiFi.localIP() == request->client()->localIP() ? "should be: ON_STA_FILTER" : "should be: ON_AP_FILTER");
|
||||
Serial.println(WiFi.localIP() == request->client()->localIP());
|
||||
Serial.println(WiFi.localIP().toString() == request->client()->localIP().toString());
|
||||
request->send(200, "text/plain", "This is the captive portal");
|
||||
hit1 = true;
|
||||
})
|
||||
.setFilter(ON_AP_FILTER);
|
||||
|
||||
server
|
||||
.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
Serial.println("Website request...");
|
||||
Serial.println("WiFi.localIP(): " + WiFi.localIP().toString());
|
||||
Serial.println("request->client()->localIP(): " + request->client()->localIP().toString());
|
||||
#if ESP_IDF_VERSION_MAJOR >= 5
|
||||
Serial.println("WiFi.type(): " + String((int)WiFi.localIP().type()));
|
||||
Serial.println("request->client()->type(): " + String((int)request->client()->localIP().type()));
|
||||
#endif
|
||||
Serial.println(WiFi.localIP() == request->client()->localIP() ? "should be: ON_STA_FILTER" : "should be: ON_AP_FILTER");
|
||||
Serial.println(WiFi.localIP() == request->client()->localIP());
|
||||
Serial.println(WiFi.localIP().toString() == request->client()->localIP().toString());
|
||||
request->send(200, "text/plain", "This is the website");
|
||||
hit2 = true;
|
||||
})
|
||||
.setFilter(ON_STA_FILTER);
|
||||
|
||||
// assert(WiFi.softAP("esp-captive-portal"));
|
||||
// dnsServer.start(53, "*", WiFi.softAPIP());
|
||||
// server.begin();
|
||||
// Serial.println("Captive portal started!");
|
||||
|
||||
// while (!hit1) {
|
||||
// dnsServer.processNextRequest();
|
||||
// yield();
|
||||
// }
|
||||
// delay(1000); // Wait for the client to process the response
|
||||
|
||||
// Serial.println("Captive portal opened, stopping it and connecting to WiFi...");
|
||||
// dnsServer.stop();
|
||||
// WiFi.softAPdisconnect();
|
||||
|
||||
WiFi.persistent(false);
|
||||
WiFi.begin("IoT");
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
}
|
||||
Serial.println("Connected to WiFi with IP address: " + WiFi.localIP().toString());
|
||||
server.begin();
|
||||
|
||||
// while (!hit2) {
|
||||
// delay(10);
|
||||
// }
|
||||
// delay(1000); // Wait for the client to process the response
|
||||
// ESP.restart();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
134
lib/ESPAsyncWebServer/examples/SimpleServer/SimpleServer.ino
Normal file
134
lib/ESPAsyncWebServer/examples/SimpleServer/SimpleServer.ino
Normal file
@@ -0,0 +1,134 @@
|
||||
//
|
||||
// A simple server implementation showing how to:
|
||||
// * serve static messages
|
||||
// * read GET and POST parameters
|
||||
// * handle missing pages / 404s
|
||||
//
|
||||
|
||||
#include <Arduino.h>
|
||||
#ifdef ESP32
|
||||
#include <AsyncTCP.h>
|
||||
#include <WiFi.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#elif defined(TARGET_RP2040)
|
||||
#include <WebServer.h>
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <AsyncJson.h>
|
||||
#include <AsyncMessagePack.h>
|
||||
|
||||
AsyncWebServer server(80);
|
||||
|
||||
const char* PARAM_MESSAGE = "message";
|
||||
|
||||
void notFound(AsyncWebServerRequest* request) {
|
||||
request->send(404, "text/plain", "Not found");
|
||||
}
|
||||
|
||||
AsyncCallbackJsonWebHandler* jsonHandler = new AsyncCallbackJsonWebHandler("/json2");
|
||||
AsyncCallbackMessagePackWebHandler* msgPackHandler = new AsyncCallbackMessagePackWebHandler("/msgpack2");
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
// WiFi.mode(WIFI_STA);
|
||||
// WiFi.begin("YOUR_SSID", "YOUR_PASSWORD");
|
||||
// if (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
||||
// Serial.printf("WiFi Failed!\n");
|
||||
// return;
|
||||
// }
|
||||
// Serial.print("IP Address: ");
|
||||
// Serial.println(WiFi.localIP());
|
||||
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP("esp-captive");
|
||||
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
request->send(200, "text/plain", "Hello, world");
|
||||
});
|
||||
|
||||
// Send a GET request to <IP>/get?message=<message>
|
||||
server.on("/get", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
String message;
|
||||
if (request->hasParam(PARAM_MESSAGE)) {
|
||||
message = request->getParam(PARAM_MESSAGE)->value();
|
||||
} else {
|
||||
message = "No message sent";
|
||||
}
|
||||
request->send(200, "text/plain", "Hello, GET: " + message);
|
||||
});
|
||||
|
||||
// Send a POST request to <IP>/post with a form field message set to <message>
|
||||
server.on("/post", HTTP_POST, [](AsyncWebServerRequest* request) {
|
||||
String message;
|
||||
if (request->hasParam(PARAM_MESSAGE, true)) {
|
||||
message = request->getParam(PARAM_MESSAGE, true)->value();
|
||||
} else {
|
||||
message = "No message sent";
|
||||
}
|
||||
request->send(200, "text/plain", "Hello, POST: " + message);
|
||||
});
|
||||
|
||||
// JSON
|
||||
|
||||
// receives JSON and sends JSON
|
||||
jsonHandler->onRequest([](AsyncWebServerRequest* request, JsonVariant& json) {
|
||||
JsonObject jsonObj = json.as<JsonObject>();
|
||||
// ...
|
||||
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot().to<JsonObject>();
|
||||
root["hello"] = "world";
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
});
|
||||
|
||||
// sends JSON
|
||||
server.on("/json1", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot().to<JsonObject>();
|
||||
root["hello"] = "world";
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
});
|
||||
|
||||
// MessagePack
|
||||
|
||||
// receives MessagePack and sends MessagePack
|
||||
msgPackHandler->onRequest([](AsyncWebServerRequest* request, JsonVariant& json) {
|
||||
JsonObject jsonObj = json.as<JsonObject>();
|
||||
// ...
|
||||
|
||||
AsyncMessagePackResponse* response = new AsyncMessagePackResponse();
|
||||
JsonObject root = response->getRoot().to<JsonObject>();
|
||||
root["hello"] = "world";
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
});
|
||||
|
||||
// sends MessagePack
|
||||
server.on("/msgpack1", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
AsyncMessagePackResponse* response = new AsyncMessagePackResponse();
|
||||
JsonObject root = response->getRoot().to<JsonObject>();
|
||||
root["hello"] = "world";
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
});
|
||||
|
||||
server.addHandler(jsonHandler);
|
||||
server.addHandler(msgPackHandler);
|
||||
|
||||
server.onNotFound(notFound);
|
||||
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
37
lib/ESPAsyncWebServer/examples/StreamFiles/StreamConcat.h
Normal file
37
lib/ESPAsyncWebServer/examples/StreamFiles/StreamConcat.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <Stream.h>
|
||||
|
||||
class StreamConcat : public Stream {
|
||||
public:
|
||||
StreamConcat(Stream* s1, Stream* s2) : _s1(s1), _s2(s2) {}
|
||||
|
||||
size_t write(__unused const uint8_t* p, __unused size_t n) override { return 0; }
|
||||
size_t write(__unused uint8_t c) override { return 0; }
|
||||
void flush() override {}
|
||||
|
||||
int available() override { return _s1->available() + _s2->available(); }
|
||||
|
||||
int read() override {
|
||||
int c = _s1->read();
|
||||
return c != -1 ? c : _s2->read();
|
||||
}
|
||||
|
||||
#if defined(TARGET_RP2040)
|
||||
size_t readBytes(char* buffer, size_t length) {
|
||||
#else
|
||||
size_t readBytes(char* buffer, size_t length) override {
|
||||
#endif
|
||||
size_t count = _s1->readBytes(buffer, length);
|
||||
return count > 0 ? count : _s2->readBytes(buffer, length);
|
||||
}
|
||||
|
||||
int peek() override {
|
||||
int c = _s1->peek();
|
||||
return c != -1 ? c : _s2->peek();
|
||||
}
|
||||
|
||||
private:
|
||||
Stream* _s1;
|
||||
Stream* _s2;
|
||||
};
|
||||
84
lib/ESPAsyncWebServer/examples/StreamFiles/StreamFiles.ino
Normal file
84
lib/ESPAsyncWebServer/examples/StreamFiles/StreamFiles.ino
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <Arduino.h>
|
||||
#include <DNSServer.h>
|
||||
#ifdef ESP32
|
||||
#include <AsyncTCP.h>
|
||||
#include <WiFi.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#elif defined(TARGET_RP2040)
|
||||
#include <WebServer.h>
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
#include "StreamConcat.h"
|
||||
#include "StreamString.h"
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <LittleFS.h>
|
||||
|
||||
DNSServer dnsServer;
|
||||
AsyncWebServer server(80);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
LittleFS.begin();
|
||||
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP("esp-captive");
|
||||
dnsServer.start(53, "*", WiFi.softAPIP());
|
||||
|
||||
File file1 = LittleFS.open("/header.html", "w");
|
||||
file1.print("<html><head><title>ESP Captive Portal</title><meta http-equiv=\"refresh\" content=\"1\"></head><body>");
|
||||
file1.close();
|
||||
|
||||
File file2 = LittleFS.open("/body.html", "w");
|
||||
file2.print("<h1>Welcome to ESP Captive Portal</h1>");
|
||||
file2.close();
|
||||
|
||||
File file3 = LittleFS.open("/footer.html", "w");
|
||||
file3.print("</body></html>");
|
||||
file3.close();
|
||||
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
File header = LittleFS.open("/header.html", "r");
|
||||
File body = LittleFS.open("/body.html", "r");
|
||||
StreamConcat stream1(&header, &body);
|
||||
|
||||
StreamString content;
|
||||
#if defined(TARGET_RP2040)
|
||||
content.printf("FreeHeap: %d", rp2040.getFreeHeap());
|
||||
#else
|
||||
content.printf("FreeHeap: %" PRIu32, ESP.getFreeHeap());
|
||||
#endif
|
||||
StreamConcat stream2 = StreamConcat(&stream1, &content);
|
||||
|
||||
File footer = LittleFS.open("/footer.html", "r");
|
||||
StreamConcat stream3 = StreamConcat(&stream2, &footer);
|
||||
|
||||
request->send(stream3, "text/html", stream3.available());
|
||||
header.close();
|
||||
body.close();
|
||||
footer.close();
|
||||
});
|
||||
|
||||
server.onNotFound([](AsyncWebServerRequest* request) {
|
||||
request->send(404, "text/plain", "Not found");
|
||||
});
|
||||
|
||||
server.begin();
|
||||
}
|
||||
|
||||
uint32_t last = 0;
|
||||
|
||||
void loop() {
|
||||
// dnsServer.processNextRequest();
|
||||
|
||||
if (millis() - last > 2000) {
|
||||
#if defined(TARGET_RP2040)
|
||||
Serial.printf("FreeHeap: %d", rp2040.getFreeHeap());
|
||||
#else
|
||||
Serial.printf("FreeHeap: %" PRIu32, ESP.getFreeHeap());
|
||||
#endif
|
||||
last = millis();
|
||||
}
|
||||
}
|
||||
40
lib/ESPAsyncWebServer/examples/StreamFiles/StreamString.h
Normal file
40
lib/ESPAsyncWebServer/examples/StreamFiles/StreamString.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <Stream.h>
|
||||
|
||||
class StreamString : public Stream {
|
||||
public:
|
||||
size_t write(const uint8_t* p, size_t n) override { return _buffer.concat(reinterpret_cast<const char*>(p), n) ? n : 0; }
|
||||
size_t write(uint8_t c) override { return _buffer.concat(static_cast<char>(c)) ? 1 : 0; }
|
||||
void flush() override {}
|
||||
|
||||
int available() override { return static_cast<int>(_buffer.length()); }
|
||||
|
||||
int read() override {
|
||||
if (_buffer.length() == 0)
|
||||
return -1;
|
||||
char c = _buffer[0];
|
||||
_buffer.remove(0, 1);
|
||||
return c;
|
||||
}
|
||||
|
||||
#if defined(TARGET_RP2040)
|
||||
size_t readBytes(char* buffer, size_t length) {
|
||||
#else
|
||||
size_t readBytes(char* buffer, size_t length) override {
|
||||
#endif
|
||||
if (length > _buffer.length())
|
||||
length = _buffer.length();
|
||||
// Don't use _str.ToCharArray() because it inserts a terminator
|
||||
memcpy(buffer, _buffer.c_str(), length);
|
||||
_buffer.remove(0, static_cast<unsigned int>(length));
|
||||
return length;
|
||||
}
|
||||
|
||||
int peek() override { return _buffer.length() > 0 ? _buffer[0] : -1; }
|
||||
|
||||
const String& buffer() const { return _buffer; }
|
||||
|
||||
private:
|
||||
String _buffer;
|
||||
};
|
||||
107
lib/ESPAsyncWebServer/examples/issues/Issue14/Issue14.ino
Normal file
107
lib/ESPAsyncWebServer/examples/issues/Issue14/Issue14.ino
Normal file
@@ -0,0 +1,107 @@
|
||||
#include <DNSServer.h>
|
||||
#ifdef ESP32
|
||||
#include <AsyncTCP.h>
|
||||
#include <WiFi.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
#elif defined(TARGET_RP2040)
|
||||
#include <WebServer.h>
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
|
||||
#include "ESPAsyncWebServer.h"
|
||||
|
||||
const char appWebPage[] PROGMEM = R"rawliteral(
|
||||
<body>
|
||||
<button id="button1" onclick="fetch('/button1');">Relay1</button>
|
||||
<script>
|
||||
const evtSource = new EventSource("/events");
|
||||
button1 = document.getElementById("button1");
|
||||
evtSource.addEventListener('state', (e) => {
|
||||
const data = JSON.parse(e.data);
|
||||
console.log('Event Source data: ', data);
|
||||
if (data.button1) {
|
||||
button1.style.backgroundColor = "green";
|
||||
}
|
||||
else {
|
||||
button1.style.backgroundColor = "red";
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
)rawliteral";
|
||||
|
||||
AsyncWebServer server(80);
|
||||
AsyncEventSource events("/events");
|
||||
|
||||
const uint32_t interval = 1000;
|
||||
const int button1Pin = 4;
|
||||
|
||||
uint32_t lastSend = 0;
|
||||
|
||||
void prepareJson(String& buffer) {
|
||||
buffer.reserve(512);
|
||||
buffer.concat("{\"button1\":");
|
||||
buffer.concat(digitalRead(button1Pin) == LOW);
|
||||
buffer.concat(",\"1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij\":");
|
||||
buffer.concat(random(0, 999999999));
|
||||
buffer.concat("}");
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
#if ARDUINO_USB_CDC_ON_BOOT
|
||||
Serial.setTxTimeoutMs(0);
|
||||
delay(100);
|
||||
#else
|
||||
while (!Serial)
|
||||
yield();
|
||||
#endif
|
||||
|
||||
randomSeed(micros());
|
||||
|
||||
pinMode(button1Pin, OUTPUT);
|
||||
digitalWrite(button1Pin, HIGH);
|
||||
|
||||
WiFi.softAP("esp-captive");
|
||||
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
request->send(200, "text/html", appWebPage);
|
||||
});
|
||||
|
||||
server.on("/button1", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
request->send(200, "text/plain", "OK");
|
||||
digitalWrite(button1Pin, digitalRead(button1Pin) == LOW ? HIGH : LOW);
|
||||
|
||||
String buffer;
|
||||
prepareJson(buffer);
|
||||
ESP_LOGI("async_tcp", "Sending from handler...");
|
||||
events.send(buffer.c_str(), "state", millis());
|
||||
ESP_LOGI("async_tcp", "Sent from handler!");
|
||||
});
|
||||
|
||||
events.onConnect([](AsyncEventSourceClient* client) {
|
||||
String buffer;
|
||||
prepareJson(buffer);
|
||||
ESP_LOGI("async_tcp", "Sending from onConnect...");
|
||||
client->send(buffer.c_str(), "state", millis(), 5000);
|
||||
ESP_LOGI("async_tcp", "Sent from onConnect!");
|
||||
});
|
||||
|
||||
server.addHandler(&events);
|
||||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
|
||||
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (millis() - lastSend >= interval) {
|
||||
String buffer;
|
||||
prepareJson(buffer);
|
||||
ESP_LOGI("loop", "Sending...");
|
||||
events.send(buffer.c_str(), "state", millis());
|
||||
ESP_LOGI("loop", "Sent!");
|
||||
lastSend = millis();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user