#include "ESPAsyncWebServer.h" #include #include #include 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("Captive Portal"); response->print("

This is out captive portal front page.

"); response->printf("

You were trying to reach: http://%s%s

", request->host().c_str(), request->url().c_str()); response->printf("

Try opening this link instead

", WiFi.softAPIP().toString().c_str()); response->print(""); request->send(response); } }; void setup() { // your other setup stuff... WiFi.softAP("esp-captive"); 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(); }