mirror of
https://github.com/me-no-dev/ESPAsyncWebServer.git
synced 2025-08-04 13:14:42 +02:00
Code cleanup
This commit is contained in:
@@ -1,37 +0,0 @@
|
|||||||
#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() {
|
|
||||||
}
|
|
@@ -116,10 +116,27 @@ AsyncCallbackJsonWebHandler* jsonHandler = new AsyncCallbackJsonWebHandler("/jso
|
|||||||
AsyncCallbackMessagePackWebHandler* msgPackHandler = new AsyncCallbackMessagePackWebHandler("/msgpack2");
|
AsyncCallbackMessagePackWebHandler* msgPackHandler = new AsyncCallbackMessagePackWebHandler("/msgpack2");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static const char characters[] = "0123456789ABCDEF";
|
||||||
|
static size_t charactersIndex = 0;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
assert(LittleFS.begin());
|
||||||
|
|
||||||
|
if (!LittleFS.exists("/index.txt")) {
|
||||||
|
File f = LittleFS.open("/index.txt", "w");
|
||||||
|
if (f) {
|
||||||
|
for (size_t c = 0; c < sizeof(characters); c++) {
|
||||||
|
for (size_t i = 0; i < 1024; i++) {
|
||||||
|
f.print(characters[c]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
||||||
// WiFi.mode(WIFI_STA);
|
// WiFi.mode(WIFI_STA);
|
||||||
// WiFi.begin("YOUR_SSID", "YOUR_PASSWORD");
|
// WiFi.begin("YOUR_SSID", "YOUR_PASSWORD");
|
||||||
@@ -217,7 +234,7 @@ void setup() {
|
|||||||
|
|
||||||
// global middleware
|
// global middleware
|
||||||
server.addMiddleware(&requestLogger);
|
server.addMiddleware(&requestLogger);
|
||||||
server.addMiddlewares({&rateLimit, &cors, &headerFilter});
|
// server.addMiddlewares({&rateLimit, &cors, &headerFilter});
|
||||||
|
|
||||||
cors.setOrigin("http://192.168.4.1");
|
cors.setOrigin("http://192.168.4.1");
|
||||||
cors.setMethods("POST, GET, OPTIONS, DELETE");
|
cors.setMethods("POST, GET, OPTIONS, DELETE");
|
||||||
@@ -304,21 +321,29 @@ void setup() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
server.on("/file", HTTP_GET, [](AsyncWebServerRequest* request) {
|
server.on("/file", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||||
request->send(LittleFS, "/index.html");
|
request->send(LittleFS, "/index.txt");
|
||||||
|
});
|
||||||
|
|
||||||
|
// Issue #14: assert failed: tcp_update_rcv_ann_wnd (needs help to test fix)
|
||||||
|
// > curl -v http://192.168.4.1/issue-14
|
||||||
|
pinMode(4, OUTPUT);
|
||||||
|
server.on("/issue-14", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||||
|
digitalWrite(4, HIGH);
|
||||||
|
request->send(LittleFS, "/index.txt", "text/pain");
|
||||||
|
delay(500);
|
||||||
|
digitalWrite(4, LOW);
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Chunked encoding test: sends 16k of characters.
|
Chunked encoding test: sends 16k of characters.
|
||||||
❯ curl -N -v -X GET -H "origin: http://192.168.4.1" http://192.168.4.1/chunk
|
❯ curl -N -v -X GET -H "origin: http://192.168.4.1" http://192.168.4.1/chunk
|
||||||
*/
|
*/
|
||||||
static const char characters[] = "1234567890";
|
|
||||||
static size_t charactersIndex = 0;
|
|
||||||
server.on("/chunk", HTTP_HEAD | HTTP_GET, [](AsyncWebServerRequest* request) {
|
server.on("/chunk", HTTP_HEAD | HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||||
AsyncWebServerResponse* response = request->beginChunkedResponse("text/html", [](uint8_t* buffer, size_t maxLen, size_t index) -> size_t {
|
AsyncWebServerResponse* response = request->beginChunkedResponse("text/html", [](uint8_t* buffer, size_t maxLen, size_t index) -> size_t {
|
||||||
if (index >= 16384)
|
if (index >= 16384)
|
||||||
return 0;
|
return 0;
|
||||||
memset(buffer, characters[charactersIndex], maxLen);
|
memset(buffer, characters[charactersIndex], maxLen);
|
||||||
charactersIndex = (charactersIndex + 1) % 10;
|
charactersIndex = (charactersIndex + 1) % sizeof(characters);
|
||||||
return maxLen;
|
return maxLen;
|
||||||
});
|
});
|
||||||
request->send(response);
|
request->send(response);
|
||||||
|
@@ -1,107 +0,0 @@
|
|||||||
#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();
|
|
||||||
}
|
|
||||||
}
|
|
@@ -5,8 +5,6 @@ lib_dir = .
|
|||||||
src_dir = examples/SimpleServer
|
src_dir = examples/SimpleServer
|
||||||
; src_dir = examples/StreamFiles
|
; src_dir = examples/StreamFiles
|
||||||
; src_dir = examples/Filters
|
; src_dir = examples/Filters
|
||||||
; src_dir = examples/Draft
|
|
||||||
; src_dir = examples/issues/Issue14
|
|
||||||
|
|
||||||
[env]
|
[env]
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
Reference in New Issue
Block a user