#include #ifdef ESP32 #include #include #elif defined(ESP8266) #include #include #elif defined(TARGET_RP2040) #include #include #endif #include "ESPAsyncWebServer.h" DNSServer dnsServer; AsyncWebServer server(80); class CaptiveRequestHandler : public AsyncWebHandler { public: CaptiveRequestHandler() {} virtual ~CaptiveRequestHandler() {} bool canHandle(__unused AsyncWebServerRequest* request) { 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()); #ifndef CONFIG_IDF_TARGET_ESP32H2 response->printf("

Try opening this link instead

", WiFi.softAPIP().toString().c_str()); #endif response->print(""); request->send(response); } }; void setup() { Serial.begin(115200); Serial.println(); Serial.println("Configuring access point..."); #ifndef CONFIG_IDF_TARGET_ESP32H2 if (!WiFi.softAP("esp-captive")) { Serial.println("Soft AP creation failed."); while (1) ; } dnsServer.start(53, "*", WiFi.softAPIP()); #endif server.addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER); // only when requested from AP // more handlers... server.begin(); } void loop() { dnsServer.processNextRequest(); }