Aktualizace na verzi 2.10.1

This commit is contained in:
2024-05-30 08:41:06 +02:00
parent e2bc8b4dc8
commit fef6d6acb4
12 changed files with 290 additions and 27 deletions

View File

@ -0,0 +1,108 @@
// 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>
#endif
#include "ESPAsyncWebServer.h"
DNSServer dnsServer;
AsyncWebServer server(80);
class CaptiveRequestHandler : public AsyncWebHandler {
public:
CaptiveRequestHandler() {}
virtual ~CaptiveRequestHandler() {}
bool canHandle(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() {
}

View File

@ -0,0 +1,33 @@
#pragma once
#include <Stream.h>
class StreamConcat : public Stream {
public:
StreamConcat(Stream* s1, Stream* s2) : _s1(s1), _s2(s2) {}
size_t write(const uint8_t* p, size_t n) override { return 0; }
size_t write(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();
}
size_t readBytes(char* buffer, size_t length) override {
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;
};

View File

@ -0,0 +1,73 @@
#include <Arduino.h>
#include <DNSServer.h>
#ifdef ESP32
#include <AsyncTCP.h>
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.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;
content.printf("FreeHeap: %" PRIu32, ESP.getFreeHeap());
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) {
Serial.printf("FreeHeap: %" PRIu32, ESP.getFreeHeap());
last = millis();
}
}

View File

@ -0,0 +1,36 @@
#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;
}
size_t readBytes(char* buffer, size_t length) override {
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;
};