Skip to content

Commit ce21dab

Browse files
author
dberschauer
committed
init captive portal
1 parent 73839c5 commit ce21dab

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

Firmware/RTK_Surveyor/Form.ino

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,36 @@ Form.ino
1111

1212
bool websocketConnected = false;
1313

14+
class CaptiveRequestHandler : public AsyncWebHandler {
15+
public:
16+
//https://en.wikipedia.org/wiki/Captive_portal
17+
String urls[5] = {"/hotspot-detect.html", "/library/test/success.html", "/generate_204", "/ncsi.txt", "/check_network_status.txt"};
18+
CaptiveRequestHandler() {}
19+
virtual ~CaptiveRequestHandler() {}
20+
21+
bool canHandle(AsyncWebServerRequest *request){
22+
for(int i = 0; i<5; i++){
23+
if(request->url().equals(urls[i]))
24+
return true;
25+
}
26+
return false;
27+
}
28+
29+
//Provide a custom small site for redirecting the user to the config site
30+
//HTTP redirect does not work and the relative links on the default config site do not work, because the phone is requesting a different server
31+
void handleRequest(AsyncWebServerRequest *request) {
32+
String logmessage = "Captive Portal Client:" + request->client()->remoteIP().toString() + " " + request->url();
33+
systemPrintln(logmessage);
34+
AsyncResponseStream *response = request->beginResponseStream("text/html");
35+
response->print("<!DOCTYPE html><html><head><title>RTK Config</title></head><body>");
36+
response->print("<div class='container'>");
37+
response->printf("<div align='center' class='col-sm-12'><img src='http://%s/src/rtk-setup.png' alt='SparkFun RTK WiFi Setup'></div>", WiFi.softAPIP().toString().c_str());
38+
response->printf("<div align='center'><h3>Configure your RTK receiver <a href='http://%s/'>here</a></h3></div>", WiFi.softAPIP().toString().c_str());
39+
response->print("</div></body></html>");
40+
request->send(response);
41+
}
42+
};
43+
1444
// Start webserver in AP mode
1545
bool startWebServer(bool startWiFi = true, int httpPort = 80)
1646
{
@@ -62,6 +92,7 @@ bool startWebServer(bool startWiFi = true, int httpPort = 80)
6292
}
6393

6494
websocket->onEvent(onWsEvent);
95+
webserver->addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER);//only when requested from AP
6596
webserver->addHandler(websocket);
6697

6798
// * index.html (not gz'd)

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ unsigned int binBytesSent = 0; // Tracks firmware bytes sent over WiFi O
221221
#include <WiFi.h> //Built-in.
222222
#include <WiFiClientSecure.h> //Built-in.
223223
#include <WiFiMulti.h> //Built-in.
224+
#include <DNSServer.h> //Built-in.
224225

225226
#include "esp_wifi.h" //Needed for esp_wifi_set_protocol()
226227

Firmware/RTK_Surveyor/WiFi.ino

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ static unsigned long wifiDisplayTimer;
6060
// Last time the WiFi state was displayed
6161
static uint32_t lastWifiState;
6262

63+
//DNS server for Captive Portal
64+
static DNSServer dnsServer;
65+
66+
6367
//----------------------------------------
6468
// WiFi Routines
6569
//----------------------------------------
@@ -242,6 +246,12 @@ bool wifiStartAP()
242246
}
243247
systemPrint("WiFi AP Started with IP: ");
244248
systemPrintln(WiFi.softAPIP());
249+
250+
// Start DNS Server
251+
if(dnsServer.start(53, "*", WiFi.softAPIP()) == false){
252+
systemPrintln("WiFi DNS Server failed to start");
253+
return (false);
254+
};
245255
}
246256
else
247257
{
@@ -355,6 +365,9 @@ void wifiUpdate()
355365
wifiStop();
356366
break;
357367
}
368+
//Process the next DNS request
369+
dnsServer.processNextRequest();
370+
358371
}
359372

360373
// Starts the WiFi connection state machine (moves from WIFI_OFF to WIFI_CONNECTING)
@@ -407,6 +420,9 @@ void wifiStop()
407420
systemPrintln("TCP Server offline");
408421
}
409422

423+
//Stop the DNS server
424+
dnsServer.stop();
425+
410426
// Stop the other network clients and then WiFi
411427
networkStop(NETWORK_TYPE_WIFI);
412428
}

0 commit comments

Comments
 (0)