-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathCaptivePortal.ino
64 lines (53 loc) · 1.88 KB
/
CaptivePortal.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
This example enables catch-all Captive portal for ESP32 Access-Point
It will allow modern devices/OSes to detect that WiFi connection is
limited and offer a user to access a banner web-page.
There is no need to find and open device's IP address/URL, i.e. http://192.168.4.1/
This works for Android, Ubuntu, FireFox, Windows, maybe others...
*/
#include <Arduino.h>
#include <WiFi.h>
#include <DNSServer.h>
#include <WebServer.h>
DNSServer dnsServer;
WebServer server(80);
static const char responsePortal[] = R"===(
<!DOCTYPE html><html><head><title>ESP32 CaptivePortal</title></head><body>
<h1>Hello World!</h1><p>This is a captive portal example page. All unknown http requests will
be redirected here.</p></body></html>
)===";
// index page handler
void handleRoot() {
server.send(200, "text/plain", "Hello from esp32!");
}
// this will redirect unknown http req's to our captive portal page
// based on this redirect various systems could detect that WiFi AP has a captive portal page
void handleNotFound() {
server.sendHeader("Location", "/portal");
server.send(302, "text/plain", "redirect to captive portal");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAP("ESP32-DNSServer");
// by default DNSServer is started serving any "*" domain name. It will reply
// AccessPoint's IP to all DNS request (this is required for Captive Portal detection)
if (dnsServer.start()) {
Serial.println("Started DNS server in captive portal-mode");
} else {
Serial.println("Err: Can't start DNS server!");
}
// serve a simple root page
server.on("/", handleRoot);
// serve portal page
server.on("/portal", []() {
server.send(200, "text/html", responsePortal);
});
// all unknown pages are redirected to captive portal
server.onNotFound(handleNotFound);
server.begin();
}
void loop() {
server.handleClient();
delay(5); // give CPU some idle time
}