improve flow for restarting in wifimanager mode
This commit is contained in:
12
Network.cpp
12
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<const char *> 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)
|
||||
|
||||
130
WebCfgServer.cpp
130
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("<HTML>\n");
|
||||
response.concat("<HEAD>\n");
|
||||
response.concat("<TITLE>NUKI Hub</TITLE>\n");
|
||||
response.concat("</HEAD>\n");
|
||||
response.concat("<BODY>\n");
|
||||
buildHtmlHeader(response);
|
||||
|
||||
response.concat("<br><h3>Info</h3>\n");
|
||||
|
||||
String version = " ";
|
||||
@@ -235,22 +231,14 @@ void WebCfgServer::buildHtml(String& response)
|
||||
response.concat("</FORM><BR><BR>");
|
||||
|
||||
response.concat("<h3>Credentials</h3>");
|
||||
|
||||
|
||||
response.concat("<form method=\"get\" action=\"/cred\">");
|
||||
response.concat("<button type=\"submit\">Edit</button>");
|
||||
response.concat("</form>");
|
||||
|
||||
response.concat("<FORM ACTION=method=get >");
|
||||
|
||||
response.concat("<BR><BR><h3>Wifi</h3>");
|
||||
response.concat("<table>");
|
||||
printInputField(response, "WIFICONF", "Type password to confirm", "", 20, true);
|
||||
response.concat("</table>");
|
||||
|
||||
response.concat("<br><INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Restart and configure WiFi\">");
|
||||
|
||||
response.concat("</FORM><BR><BR>");
|
||||
response.concat("<br><br><h3>WiFi</h3>");
|
||||
response.concat("<form method=\"get\" action=\"/wifi\">");
|
||||
response.concat("<button type=\"submit\">Restart and configure wifi</button>");
|
||||
response.concat("</form>");
|
||||
|
||||
response.concat("</BODY>\n");
|
||||
response.concat("</HTML>\n");
|
||||
@@ -259,11 +247,7 @@ void WebCfgServer::buildHtml(String& response)
|
||||
|
||||
void WebCfgServer::buildCredHtml(String &response)
|
||||
{
|
||||
response.concat("<HTML>\n");
|
||||
response.concat("<HEAD>\n");
|
||||
response.concat("<TITLE>NUKI Hub</TITLE>\n");
|
||||
response.concat("</HEAD>\n");
|
||||
response.concat("<BODY>\n");
|
||||
buildHtmlHeader(response);
|
||||
|
||||
response.concat("<FORM ACTION=method=get >");
|
||||
|
||||
@@ -277,25 +261,51 @@ void WebCfgServer::buildCredHtml(String &response)
|
||||
|
||||
response.concat("</FORM>");
|
||||
|
||||
response.concat("<BR>");
|
||||
//
|
||||
response.concat("</BODY>\n");
|
||||
response.concat("</HTML>\n");
|
||||
}
|
||||
|
||||
void WebCfgServer::buildConfirmHtml(String &response)
|
||||
void WebCfgServer::buildConfirmHtml(String &response, const String &message, uint32_t redirectDelay)
|
||||
{
|
||||
String delay(redirectDelay);
|
||||
|
||||
response.concat("<HTML>\n");
|
||||
response.concat("<HEAD>\n");
|
||||
response.concat("<TITLE>NUKI Hub</TITLE>\n");
|
||||
response.concat("<meta http-equiv=\"Refresh\" content=\"");
|
||||
response.concat(redirectDelay);
|
||||
response.concat("; url=/\" />");
|
||||
response.concat("\n</HEAD>\n");
|
||||
response.concat("<BODY>\n");
|
||||
response.concat(message);
|
||||
|
||||
response.concat("</BODY>\n");
|
||||
response.concat("</HTML>\n");
|
||||
}
|
||||
|
||||
|
||||
void WebCfgServer::buildConfigureWifiHtml(String &response)
|
||||
{
|
||||
buildHtmlHeader(response);
|
||||
|
||||
response.concat("<h3>WiFi</h3>");
|
||||
response.concat("Click confirm to restart ESP into WiFi configuration mode. After restart, connect to ESP access point to reconfigure WiFI.<br><br>");
|
||||
response.concat("<form method=\"get\" action=\"/wifimanager\">");
|
||||
response.concat("<button type=\"submit\">Confirm</button>");
|
||||
response.concat("</form>");
|
||||
|
||||
response.concat("</BODY>\n");
|
||||
response.concat("</HTML>\n");
|
||||
}
|
||||
|
||||
|
||||
void WebCfgServer::buildHtmlHeader(String &response)
|
||||
{
|
||||
response.concat("<HTML>\n");
|
||||
response.concat("<HEAD>\n");
|
||||
response.concat("<TITLE>NUKI Hub</TITLE>\n");
|
||||
response.concat("<meta http-equiv=\"Refresh\" content=\"5; url=/\" />");
|
||||
response.concat("\n</HEAD>\n");
|
||||
response.concat("</HEAD>\n");
|
||||
response.concat("<BODY>\n");
|
||||
|
||||
response.concat("Configuration saved ... restarting.\n");
|
||||
|
||||
response.concat("</BODY>\n");
|
||||
response.concat("</HTML>\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("</td>");
|
||||
response.concat("</tr>");
|
||||
}
|
||||
@@ -350,3 +360,13 @@ void WebCfgServer::printParameter(String& response, const char *description, con
|
||||
response.concat("</tr>");
|
||||
|
||||
}
|
||||
|
||||
void WebCfgServer::waitAndProcess(const uint32_t duration)
|
||||
{
|
||||
unsigned long timeout = millis() + duration;
|
||||
while(millis() < timeout)
|
||||
{
|
||||
server.handleClient();
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user