diff --git a/Network.cpp b/Network.cpp index 78824e8..54a2dc2 100644 --- a/Network.cpp +++ b/Network.cpp @@ -22,17 +22,19 @@ void Network::initialize() //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around WiFiManager wm; - // reset settings - wipe stored credentials for testing - // these are stored by the esp library - //wm.resetSettings(); + std::vector wm_menu; + wm_menu.push_back("wifi"); + wm_menu.push_back("exit"); + wm.setShowInfoUpdate(false); + wm.setMenu(wm_menu); bool res = false; if(_cookie.isSet()) { Serial.println(F("Opening WiFi configuration portal.")); - res = wm.startConfigPortal(); _cookie.clear(); + res = wm.startConfigPortal(); } else { @@ -166,8 +168,6 @@ void Network::update() } _mqttClient.loop(); - - vTaskDelay( 100 / portTICK_PERIOD_MS); } void Network::onMqttDataReceivedCallback(char *topic, byte *payload, unsigned int length) diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 6977677..054bfcb 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -20,9 +20,6 @@ WebCfgServer::WebCfgServer(NukiWrapper* nuki, Network* network, Preferences* pre str = _preferences->getString(preference_cred_password); const char *pass = str.c_str(); memcpy(&_credPassword, pass, str.length()); - -// Serial.print("##### user: "); Serial.println(_credUser); -// Serial.print("##### pass: "); Serial.println(_credPassword); } } @@ -45,6 +42,24 @@ void WebCfgServer::initialize() buildCredHtml(response); server.send(200, "text/html", response); }); + server.on("/wifi", [&]() { + if (_hasCredentials && !server.authenticate(_credUser, _credPassword)) { + return server.requestAuthentication(); + } + String response = ""; + buildConfigureWifiHtml(response); + server.send(200, "text/html", response); + }); + server.on("/wifimanager", [&]() { + if (_hasCredentials && !server.authenticate(_credUser, _credPassword)) { + return server.requestAuthentication(); + } + String response = ""; + buildConfirmHtml(response, "Restarting. Connect to ESP access point to reconfigure WiFi.", 0); + server.send(200, "text/html", response); + waitAndProcess(1000); + _network->restartAndConfigureWifi(); + }); server.on("/method=get", [&]() { if (_hasCredentials && !server.authenticate(_credUser, _credPassword)) { return server.requestAuthentication(); @@ -53,15 +68,11 @@ void WebCfgServer::initialize() if(configChanged) { String response = ""; - buildConfirmHtml(response); + buildConfirmHtml(response, "Configuration saved ... restarting."); server.send(200, "text/html", response); Serial.println(F("Restarting")); - unsigned long timeout = millis() + 1000; - while(millis() < timeout) - { - server.handleClient(); - delay(10); - } + + waitAndProcess(1000); ESP.restart(); } }); @@ -81,10 +92,6 @@ bool WebCfgServer::processArgs() String key = server.argName(index); String value = server.arg(index); -// Serial.print(key); -// Serial.print(" = "); -// Serial.println(value); - if(key == "MQTTSERVER") { _preferences->putString(preference_mqtt_broker, value); @@ -152,13 +159,6 @@ bool WebCfgServer::processArgs() _preferences->putString(preference_cred_password, value); configChanged = true; } - else if(key == "WIFICONF") - { - if(value == _preferences->getString(preference_cred_password)) - { - _network->restartAndConfigureWifi(); - } - } } if(clearMqttCredentials) @@ -189,16 +189,12 @@ void WebCfgServer::update() if(!_enabled) return; server.handleClient(); - vTaskDelay(200 / portTICK_PERIOD_MS); } void WebCfgServer::buildHtml(String& response) { - response.concat("\n"); - response.concat("\n"); - response.concat("NUKI Hub\n"); - response.concat("\n"); - response.concat("\n"); + buildHtmlHeader(response); + response.concat("

Info

\n"); String version = " "; @@ -235,22 +231,14 @@ void WebCfgServer::buildHtml(String& response) response.concat("

"); response.concat("

Credentials

"); - - response.concat("
"); response.concat(""); response.concat("
"); - response.concat("
"); - - response.concat("

Wifi

"); - response.concat(""); - printInputField(response, "WIFICONF", "Type password to confirm", "", 20, true); - response.concat("
"); - - response.concat("
"); - - response.concat("


"); + response.concat("

WiFi

"); + response.concat("
"); + response.concat(""); + response.concat("
"); response.concat("\n"); response.concat("\n"); @@ -259,11 +247,7 @@ void WebCfgServer::buildHtml(String& response) void WebCfgServer::buildCredHtml(String &response) { - response.concat("\n"); - response.concat("\n"); - response.concat("NUKI Hub\n"); - response.concat("\n"); - response.concat("\n"); + buildHtmlHeader(response); response.concat("
"); @@ -277,25 +261,51 @@ void WebCfgServer::buildCredHtml(String &response) response.concat("
"); - response.concat("
"); -// response.concat("\n"); response.concat("\n"); } -void WebCfgServer::buildConfirmHtml(String &response) +void WebCfgServer::buildConfirmHtml(String &response, const String &message, uint32_t redirectDelay) +{ + String delay(redirectDelay); + + response.concat("\n"); + response.concat("\n"); + response.concat("NUKI Hub\n"); + response.concat(""); + response.concat("\n\n"); + response.concat("\n"); + response.concat(message); + + response.concat("\n"); + response.concat("\n"); +} + + +void WebCfgServer::buildConfigureWifiHtml(String &response) +{ + buildHtmlHeader(response); + + response.concat("

WiFi

"); + response.concat("Click confirm to restart ESP into WiFi configuration mode. After restart, connect to ESP access point to reconfigure WiFI.

"); + response.concat("
"); + response.concat(""); + response.concat("
"); + + response.concat("\n"); + response.concat("\n"); +} + + +void WebCfgServer::buildHtmlHeader(String &response) { response.concat("\n"); response.concat("\n"); response.concat("NUKI Hub\n"); - response.concat(""); - response.concat("\n\n"); + response.concat("\n"); response.concat("\n"); - - response.concat("Configuration saved ... restarting.\n"); - - response.concat("\n"); - response.concat("\n"); } void WebCfgServer::printInputField(String& response, @@ -322,7 +332,7 @@ void WebCfgServer::printInputField(String& response, response.concat(token); response.concat("\" SIZE=\"25\" MAXLENGTH=\""); response.concat(maxLengthStr); - response.concat("\">"); + response.concat("\\\">"); response.concat(""); response.concat(""); } @@ -350,3 +360,13 @@ void WebCfgServer::printParameter(String& response, const char *description, con response.concat(""); } + +void WebCfgServer::waitAndProcess(const uint32_t duration) +{ + unsigned long timeout = millis() + duration; + while(millis() < timeout) + { + server.handleClient(); + delay(10); + } +} diff --git a/WebCfgServer.h b/WebCfgServer.h index e24c4ae..a832770 100644 --- a/WebCfgServer.h +++ b/WebCfgServer.h @@ -31,12 +31,17 @@ private: bool processArgs(); void buildHtml(String& response); void buildCredHtml(String& response); - void buildConfirmHtml(String& response); + void buildConfirmHtml(String& response, const String &message, uint32_t redirectDelay = 5); + void buildConfigureWifiHtml(String& response); + + void buildHtmlHeader(String& response); void printInputField(String& response, const char* token, const char* description, const char* value, const size_t maxLength, const bool isPassword = false); void printInputField(String& response, const char* token, const char* description, const int value, size_t maxLength); void printParameter(String& response, const char* description, const char* value); + void waitAndProcess(const uint32_t duration); + WebServer server; NukiWrapper* _nuki; Network* _network; diff --git a/main.cpp b/main.cpp index 6934e52..4a89ec6 100644 --- a/main.cpp +++ b/main.cpp @@ -20,6 +20,7 @@ void networkTask(void *pvParameters) { network->update(); webCfgServer->update(); + vTaskDelay(200 / portTICK_PERIOD_MS); } }