mirror of
https://github.com/me-no-dev/ESPAsyncWebServer.git
synced 2025-08-16 11:00:59 +02:00
Added Filters example to reproduce https://github.com/mathieucarbou/ESPAsyncWebServer/issues/26
This commit is contained in:
5
.github/workflows/ci.yml
vendored
5
.github/workflows/ci.yml
vendored
@@ -59,6 +59,9 @@ jobs:
|
|||||||
- name: Build SimpleServer
|
- name: Build SimpleServer
|
||||||
run: arduino-cli compile --library . --warnings none -b ${{ matrix.board }} "examples/SimpleServer/SimpleServer.ino"
|
run: arduino-cli compile --library . --warnings none -b ${{ matrix.board }} "examples/SimpleServer/SimpleServer.ino"
|
||||||
|
|
||||||
|
- name: Build Filters
|
||||||
|
run: arduino-cli compile --library . --warnings none -b ${{ matrix.board }} "examples/Filters/Filters.ino"
|
||||||
|
|
||||||
platformio:
|
platformio:
|
||||||
name: pio ${{ matrix.name }}
|
name: pio ${{ matrix.name }}
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -112,3 +115,5 @@ jobs:
|
|||||||
run: platformio ci "examples/CaptivePortal/CaptivePortal.ino" -l '.' -b ${{ matrix.board }} ${{ matrix.opts }}
|
run: platformio ci "examples/CaptivePortal/CaptivePortal.ino" -l '.' -b ${{ matrix.board }} ${{ matrix.opts }}
|
||||||
- name: Build SimpleServer
|
- name: Build SimpleServer
|
||||||
run: platformio ci "examples/SimpleServer/SimpleServer.ino" -l '.' -b ${{ matrix.board }} ${{ matrix.opts }}
|
run: platformio ci "examples/SimpleServer/SimpleServer.ino" -l '.' -b ${{ matrix.board }} ${{ matrix.opts }}
|
||||||
|
- name: Build Filters
|
||||||
|
run: platformio ci "examples/Filters/Filters.ino" -l '.' -b ${{ matrix.board }} ${{ matrix.opts }}
|
||||||
|
110
examples/Filters/Filters.ino
Normal file
110
examples/Filters/Filters.ino
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
// Reproduced issue https://github.com/mathieucarbou/ESPAsyncWebServer/issues/26
|
||||||
|
// The issue is present when using mathieucarbou/Async TCP @ ^3.1.2 (which is based on ESPHome fork)
|
||||||
|
// The issue is not present when directly using https://github.com/me-no-dev/AsyncTCP
|
||||||
|
|
||||||
|
#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());
|
||||||
|
|
||||||
|
while (!hit2) {
|
||||||
|
delay(10);
|
||||||
|
}
|
||||||
|
delay(1000); // Wait for the client to process the response
|
||||||
|
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
}
|
@@ -7,15 +7,18 @@ build_flags =
|
|||||||
lib_deps =
|
lib_deps =
|
||||||
bblanchon/ArduinoJson @ 7.0.4
|
bblanchon/ArduinoJson @ 7.0.4
|
||||||
mathieucarbou/Async TCP @ ^3.1.2
|
mathieucarbou/Async TCP @ ^3.1.2
|
||||||
|
; https://github.com/me-no-dev/AsyncTCP
|
||||||
|
esphome/ESPAsyncTCP-esphome @ 2.0.0
|
||||||
upload_protocol = esptool
|
upload_protocol = esptool
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
monitor_filters = esp32_exception_decoder, log2file
|
monitor_filters = esp32_exception_decoder, log2file
|
||||||
|
|
||||||
[platformio]
|
[platformio]
|
||||||
lib_dir = .
|
lib_dir = .
|
||||||
src_dir = examples/CaptivePortal
|
; src_dir = examples/CaptivePortal
|
||||||
; src_dir = examples/SimpleServer
|
; src_dir = examples/SimpleServer
|
||||||
; src_dir = examples/StreamFiles
|
; src_dir = examples/StreamFiles
|
||||||
|
src_dir = examples/Filters
|
||||||
|
|
||||||
[env:arduino]
|
[env:arduino]
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
|
Reference in New Issue
Block a user