@@ -11,6 +11,36 @@ Form.ino
1111
1212bool 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
1545bool 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)
0 commit comments