This repository was archived by the owner on Mar 5, 2026. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include < WiFi.h>
2+ #include < AsyncTCP.h>
3+ #include < DNSServer.h>
4+ #include " ESPAsyncWebServer.h"
5+
6+ DNSServer dnsServer;
7+ AsyncWebServer server (80 );
8+
9+ class CaptiveRequestHandler : public AsyncWebHandler {
10+ public:
11+ CaptiveRequestHandler () {}
12+ virtual ~CaptiveRequestHandler () {}
13+
14+ bool canHandle (AsyncWebServerRequest *request){
15+ // request->addInterestingHeader("ANY");
16+ return true ;
17+ }
18+
19+ void handleRequest (AsyncWebServerRequest *request) {
20+ AsyncResponseStream *response = request->beginResponseStream (" text/html" );
21+ response->print (" <!DOCTYPE html><html><head><title>Captive Portal</title></head><body>" );
22+ response->print (" <p>This is out captive portal front page.</p>" );
23+ response->printf (" <p>You were trying to reach: http://%s%s</p>" , request->host ().c_str (), request->url ().c_str ());
24+ response->printf (" <p>Try opening <a href='http://%s'>this link</a> instead</p>" , WiFi.softAPIP ().toString ().c_str ());
25+ response->print (" </body></html>" );
26+ request->send (response);
27+ }
28+ };
29+
30+
31+ void setup (){
32+ // your other setup stuff...
33+ WiFi.softAP (" esp-captive" );
34+ dnsServer.start (53 , " *" , WiFi.softAPIP ());
35+ server.addHandler (new CaptiveRequestHandler ()).setFilter (ON_AP_FILTER);// only when requested from AP
36+ // more handlers...
37+ server.begin ();
38+ }
39+
40+ void loop (){
41+ dnsServer.processNextRequest ();
42+ }
You can’t perform that action at this time.
0 commit comments