@@ -77,55 +77,7 @@ static const int webSocketStackSize = 8192;
77
77
// These are useful:
78
78
// https://github.com/mo-thunderz/Esp32WifiPart2/blob/main/Arduino/ESP32WebserverWebsocket/ESP32WebserverWebsocket.ino
79
79
// https://www.youtube.com/watch?v=15X0WvGaVg8
80
-
81
- // ----------------------------------------
82
- // ===== Request Handler class used to answer more complex requests =====
83
80
// https://github.com/espressif/arduino-esp32/blob/master/libraries/WebServer/examples/WebServer/WebServer.ino
84
- // ----------------------------------------
85
- class CaptiveRequestHandler : public RequestHandler
86
- {
87
- public:
88
- // https://en.wikipedia.org/wiki/Captive_portal
89
- String urls[5 ] = {" /hotspot-detect.html" , " /library/test/success.html" , " /generate_204" , " /ncsi.txt" ,
90
- " /check_network_status.txt" };
91
- CaptiveRequestHandler ()
92
- {
93
- if (settings.debugWebServer == true )
94
- systemPrintln (" CaptiveRequestHandler is registered" );
95
- }
96
- virtual ~CaptiveRequestHandler ()
97
- {
98
- }
99
-
100
- bool canHandle (HTTPMethod requestMethod, String uri)
101
- {
102
- for (int i = 0 ; i < sizeof (urls); i++)
103
- {
104
- if (uri == urls[i])
105
- return true ;
106
- }
107
- return false ;
108
- }
109
-
110
- bool handle (WebServer &server, HTTPMethod requestMethod, String requestUri)
111
- {
112
- String logmessage = " Captive Portal Client:" + server.client ().remoteIP ().toString () + " " + requestUri;
113
- if (settings.debugWebServer == true )
114
- systemPrintln (logmessage);
115
- String response = " <!DOCTYPE html><html><head><title>RTK Config</title></head><body>" ;
116
- response += " <div class='container'>" ;
117
- response += " <div align='center' class='col-sm-12'><img src='http://" ;
118
- response += WiFi.softAPIP ().toString ();
119
- response += " /src/rtk-setup.png' alt='SparkFun RTK WiFi Setup'></div>" ;
120
- response += " <div align='center'><h3>Configure your RTK receiver <a href='http://" ;
121
- response += WiFi.softAPIP ().toString ();
122
- response += " /'>here</a></h3></div>" ;
123
- response += " </div></body></html>" ;
124
- server.send (200 , " text/html" , response);
125
-
126
- return true ;
127
- }
128
- };
129
81
130
82
// ----------------------------------------
131
83
// Create a csv string with the dynamic data to update (current coordinates, battery level, etc)
@@ -637,11 +589,55 @@ void handleUpload()
637
589
// ----------------------------------------
638
590
void notFound ()
639
591
{
640
- String logmessage = " notFound: Client:" + webServer->client ().remoteIP ().toString () + " " + webServer->uri ();
641
- systemPrintln (logmessage);
592
+ if (settings.enableCaptivePortal == true && knownCaptiveUrl (webServer->uri ()) == true )
593
+ {
594
+ String logmessage = " Known captive URI: " + webServer->client ().remoteIP ().toString () + " " + webServer->uri ();
595
+ Serial.println (logmessage);
596
+ webServer->sendHeader (" Location" , " /portal" );
597
+ webServer->send (302 , " text/plain" , " Redirect to captive portal" );
598
+ return ;
599
+ }
600
+
601
+ String logmessage = " notFound: " + webServer->client ().remoteIP ().toString () + " " + webServer->uri ();
602
+ Serial.println (logmessage);
642
603
webServer->send (404 , " text/plain" , " Not found" );
643
604
}
644
605
606
+ // These are the various files or endpoints that browsers will attempt to access to see if internet access is available
607
+ // If one is requested, redirect user to captive portal
608
+ String captiveUrls[] = {
609
+ " /hotspot-detect.html" , " /library/test/success.html" , " /generate_204" , " /ncsi.txt" , " /check_network_status.txt" ,
610
+ " /connecttest.txt" };
611
+
612
+ static const uint8_t captiveUrlCount = sizeof (captiveUrls) / sizeof (captiveUrls[0 ]);
613
+
614
+ // Check if given URI is a captive portal endpoint
615
+ bool knownCaptiveUrl (String uri)
616
+ {
617
+ for (int i = 0 ; i < captiveUrlCount; i++)
618
+ {
619
+ if (uri == captiveUrls[i])
620
+ return true ;
621
+ }
622
+ return false ;
623
+ }
624
+
625
+ void respondWithPortal ()
626
+ {
627
+ Serial.println (" \r\n Send portal page" );
628
+
629
+ String response = " <!DOCTYPE html><html><head><title>RTK Config</title></head><body>" ;
630
+ response += " <div class='container'>" ;
631
+ response += " <div align='center' class='col-sm-12'><img src='http://" ;
632
+ response += WiFi.softAPIP ().toString ();
633
+ response += " /src/rtk-setup.png' alt='SparkFun RTK WiFi Setup'></div>" ;
634
+ response += " <div align='center'><h3>Configure your RTK receiver <a href='http://" ;
635
+ response += WiFi.softAPIP ().toString ();
636
+ response += " /'>here</a></h3></div>" ;
637
+ response += " </div></body></html>" ;
638
+ webServer->send (200 , " text/html" , response);
639
+ }
640
+
645
641
// ----------------------------------------
646
642
// Break CSV into setting constituents
647
643
// Can't use strtok because we may have two commas next to each other, ie
@@ -834,7 +830,8 @@ bool webServerAssignResources(int httpPort = 80)
834
830
}
835
831
createSettingsString (settingsCSV);
836
832
837
- /* https://github.com/espressif/arduino-esp32/blob/master/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino */
833
+ /* https://github.com/espressif/arduino-esp32/blob/master/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino
834
+ */
838
835
839
836
webServer = new WebServer (httpPort);
840
837
if (!webServer)
@@ -843,11 +840,6 @@ bool webServerAssignResources(int httpPort = 80)
843
840
break ;
844
841
}
845
842
846
- if (settings.enableCaptivePortal == true )
847
- {
848
- webServer->addHandler (new CaptiveRequestHandler ());
849
- }
850
-
851
843
// * index.html (not gz'd)
852
844
// * favicon.ico
853
845
@@ -903,9 +895,12 @@ bool webServerAssignResources(int httpPort = 80)
903
895
GET_PAGE (" /src/fonts/icomoon.ttf" , " text/plain" , icomoon_ttf);
904
896
GET_PAGE (" /src/fonts/icomoon.woof" , " text/plain" , icomoon_woof);
905
897
906
- // https://lemariva.com/blog/2017/11/white-hacking-wemos-captive-portal-using-micropython
907
- webServer->on (" /connecttest.txt" , HTTP_GET,
908
- []() { webServer->send (200 , " text/plain" , " Microsoft Connect Test" ); });
898
+ // Handler for captive portal page
899
+ if (settings.enableCaptivePortal == true )
900
+ {
901
+ Serial.println (" \r\n Setting portal" );
902
+ webServer->on (" /portal" , []() { respondWithPortal (); });
903
+ }
909
904
910
905
// Handler for the /uploadFile form POST
911
906
webServer->on (
0 commit comments