8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 44f83b0 commit d912710Copy full SHA for d912710
libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino
@@ -1,52 +1,59 @@
1
+/*
2
+This example enables catch-all Captive portal for ESP32 Access-Point
3
+It will allow modern devices/OSes to detect that WiFi connection is
4
+limited and offer a user to access a banner web-page.
5
+There is no need to find and open device's IP address/URL, i.e. http://192.168.4.1/
6
+This works for Android, Ubuntu, FireFox, Windows, maybe others...
7
+*/
8
+
9
+#include <Arduino.h>
10
#include <WiFi.h>
11
#include <DNSServer.h>
12
+#include <WebServer.h>
13
14
-const byte DNS_PORT = 53;
-IPAddress apIP(8,8,4,4); // The default android DNS
15
DNSServer dnsServer;
-WiFiServer server(80);
16
+WebServer server(80);
17
18
+static const char responsePortal[] = R"===(
19
+<!DOCTYPE html><html><head><title>ESP32 CaptivePortal</title></head><body>
20
+<h1>Hello World!</h1><p>This is a captive portal example page. All unknown http requests will
21
+be redirected here.</p></body></html>
22
+)===";
23
24
+// index page handler
25
+void handleRoot() {
26
+ server.send(200, "text/plain", "Hello from esp32!");
27
+}
28
-String responseHTML = ""
- "<!DOCTYPE html><html><head><title>CaptivePortal</title></head><body>"
- "<h1>Hello World!</h1><p>This is a captive portal example. All requests will "
- "be redirected here.</p></body></html>";
29
+// this will redirect unknown http req's to our captive portal page
30
+// based on this redirect various systems could detect that WiFi AP has a captive portal page
31
+void handleNotFound() {
32
+ server.sendHeader("Location", "/portal");
33
+ server.send(302, "text/plain", "redirect to captive portal");
34
35
-void setup() {
36
+void setup() {
37
+ Serial.begin(115200);
38
WiFi.mode(WIFI_AP);
39
WiFi.softAP("ESP32-DNSServer");
- WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
40
- // if DNSServer is started with "*" for domain name, it will reply with
- // provided IP to all DNS request
- dnsServer.start(DNS_PORT, "*", apIP);
41
+ // by default DNSServer is started serving any "*" domain name. It will reply
42
+ // AccessPoint's IP to all DNS request (this is requred for Captive Portal detection)
43
+ dnsServer.start();
44
45
+ // serve a simple root page
46
+ server.on("/", handleRoot);
47
48
+ // serve portal page
49
+ server.on("/portal",[](){server.send(200, "text/html", responsePortal);});
50
51
+ // all unknown pages are redirected to captive portal
52
+ server.onNotFound(handleNotFound);
53
server.begin();
54
}
55
56
void loop() {
- dnsServer.processNextRequest();
- WiFiClient client = server.available(); // listen for incoming clients
-
- if (client) {
- String currentLine = "";
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- if (c == '\n') {
- if (currentLine.length() == 0) {
- client.println("HTTP/1.1 200 OK");
- client.println("Content-type:text/html");
- client.println();
- client.print(responseHTML);
- break;
- } else {
- currentLine = "";
- }
- } else if (c != '\r') {
- currentLine += c;
- client.stop();
57
+ server.handleClient();
58
+ delay(5); // give CPU some idle time
59