diff --git a/README.md b/README.md index 9e5799e..f4d5978 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ The Library runs on any kind of **ESP8266** and **ESP32** (NodeMCU, AI Thinker, * [Grouped controls](#grouped-controls) * [Wide controls](#wide-controls) * [Graph (Experimental)](#graph--experimental-) + * [Captive Portal](#captive-portal) - [Notes for Development](#notes-for-development) - [Contribute](#contribute) @@ -643,6 +644,17 @@ Graph points are saved in the browser in **localstorage** to be persistant, clea _There are many issues with the graph component currently and work is ongoing. Consider helping us out with development!_ +### Captive Portal + +ESPUI will redirect all unknown URLs it is asked for to the 'root' of the local HTTP server instead of responding with an HTTP code 404. This makes it act as a simple 'captive portal'. Note you must also set up the ESP to be a DNS server that responds to all DNS requests with the IP address of the ESP. This only effective when the ESP is acting as a WiFi hotspot in AP mode and assigning itself as the DNS server to connected clients. + +All the example sketches include the DNS related code and will work as captive portals when used as a hotspot. In the event you wish to disable this feature you can do so by removing the DNS server code and adding the code below. + +``` +ESPUI.captivePortal = false; +``` + + # Notes for Development If you want to work on the HTML/CSS/JS files, do make changes in the _data_ diff --git a/examples/gui/gui.ino b/examples/gui/gui.ino index c7d390c..2356fd6 100644 --- a/examples/gui/gui.ino +++ b/examples/gui/gui.ino @@ -256,7 +256,6 @@ void setup(void) * since it is transmitted in cleartext. Just add a string as username and * password, for example begin("ESPUI Control", "username", "password") */ - ESPUI.begin("ESPUI Control"); } diff --git a/keywords.txt b/keywords.txt index 1ce2287..84cd7eb 100644 --- a/keywords.txt +++ b/keywords.txt @@ -25,6 +25,7 @@ beginLITTLEFS KEYWORD2 print KEYWORD2 updateSwitcher KEYWORD2 updateSlider KEYWORD2 +captivePortal LITERAL1 ####################################### # Instances (KEYWORD2) diff --git a/src/ESPUI.cpp b/src/ESPUI.cpp index bacd91f..c6f3577 100644 --- a/src/ESPUI.cpp +++ b/src/ESPUI.cpp @@ -1351,7 +1351,16 @@ void ESPUIClass::beginLITTLEFS(const char* _title, const char* username, const c request->send(200, "text/plain", String(ESP.getFreeHeap()) + " In LITTLEFS mode"); }); - server->onNotFound([](AsyncWebServerRequest* request) { request->send(404); }); + server->onNotFound([this](AsyncWebServerRequest* request) { + if(captivePortal) + { + request->redirect("/"); + } + else + { + request->send(404); + } + }); server->begin(); @@ -1496,7 +1505,16 @@ void ESPUIClass::begin(const char* _title, const char* username, const char* pas request->send(200, "text/plain", String(ESP.getFreeHeap()) + " In Memorymode"); }); - server->onNotFound([](AsyncWebServerRequest* request) { request->send(404); }); + server->onNotFound([this](AsyncWebServerRequest* request) { + if(captivePortal) + { + request->redirect("/"); + } + else + { + request->send(404); + } + }); server->begin(); diff --git a/src/ESPUI.h b/src/ESPUI.h index 5016a83..668d02f 100644 --- a/src/ESPUI.h +++ b/src/ESPUI.h @@ -236,6 +236,7 @@ public: unsigned int jsonUpdateDocumentSize; unsigned int jsonInitialDocumentSize; bool sliderContinuous; + bool captivePortal = true; void setVerbosity(Verbosity verbosity); void begin(const char* _title, const char* username = nullptr, const char* password = nullptr,