Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit e952f95

Browse files
hermme-no-dev
authored andcommitted
Add captive portal example from me-no-dev#228. (me-no-dev#241)
* Add captive portal example from me-no-dev#228. * Add missing headers. * Update CaptivePortal.ino
1 parent 2dc705c commit e952f95

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

0 commit comments

Comments
 (0)