WebServer: improve Setup

Keep password.
Fix ip mode previous state
This commit is contained in:
Mathieu Maret 2019-06-03 00:07:12 +02:00
parent ac6ab8969c
commit fea3b73f54

View File

@ -6,8 +6,8 @@ String gpioControlHTML = "";
String pwmControlHTML = ""; String pwmControlHTML = "";
void WebBuildGpioObserved(String &html){ void WebBuildGpioObserved(String &html) {
if (NB_ELEMENTS(gpioObserved) > 0){ if (NB_ELEMENTS(gpioObserved) > 0) {
html += "<fieldset>" html += "<fieldset>"
"<legend>Detector</legend>"; "<legend>Detector</legend>";
for (uint i = 0 ; i < NB_ELEMENTS(gpioObserved) ; i++) { for (uint i = 0 ; i < NB_ELEMENTS(gpioObserved) ; i++) {
@ -22,7 +22,7 @@ void WebHandleRoot() {
String optimiseConfig = ""; String optimiseConfig = "";
WebBuildGpioObserved(gpioObserved); WebBuildGpioObserved(gpioObserved);
if(WiFi.status() == WL_CONNECTED){ if (WiFi.status() == WL_CONNECTED) {
optimiseConfig = "<a href=\"/setupPreConfig\">Optimize Config</a><br/>"; optimiseConfig = "<a href=\"/setupPreConfig\">Optimize Config</a><br/>";
} }
@ -40,7 +40,7 @@ void WebHandleRoot() {
"Humidity DHT " + String(dhtHumidity, 0) + "%<br/>" "Humidity DHT " + String(dhtHumidity, 0) + "%<br/>"
#endif #endif
#ifdef CONFIG_ENABLE_DRY_SENSOR #ifdef CONFIG_ENABLE_DRY_SENSOR
"Dryness " + String((dryness*100)/1024) + "%<br/>" "Dryness " + String((dryness * 100) / 1024) + "%<br/>"
#endif #endif
"</fieldset>" + gpioControlHTML + gpioObserved + pwmControlHTML + "<fieldset>" "</fieldset>" + gpioControlHTML + gpioObserved + pwmControlHTML + "<fieldset>"
"<legend>Settings</legend>" "<legend>Settings</legend>"
@ -62,7 +62,7 @@ void WebSendError(const char *error) {
server.send(500, "text/plain", error); server.send(500, "text/plain", error);
} }
void WebBuildSSIDList(String &datalist){ void WebBuildSSIDList(String &datalist) {
int n = WiFi.scanNetworks(); int n = WiFi.scanNetworks();
datalist = "<datalist id=\"scan_ssid\">"; datalist = "<datalist id=\"scan_ssid\">";
// sort by RSSI // sort by RSSI
@ -77,14 +77,14 @@ void WebBuildSSIDList(String &datalist){
} }
} }
} }
for (int i = 0; i < n; ++i){ for (int i = 0; i < n; ++i) {
datalist += "<option value=\""+WiFi.SSID(indices[i])+"\">"; datalist += "<option value=\"" + WiFi.SSID(indices[i]) + "\">";
} }
datalist += "</datalist>"; datalist += "</datalist>";
} }
void WebHandleSetupPreConfig() { void WebHandleSetupPreConfig() {
conf.bssid =strdup( WiFi.BSSIDstr().c_str()); conf.bssid = strdup( WiFi.BSSIDstr().c_str());
conf.channel = WiFi.channel(); conf.channel = WiFi.channel();
conf.ip_mode = 1; conf.ip_mode = 1;
conf.ip = WiFi.localIP(); conf.ip = WiFi.localIP();
@ -98,6 +98,8 @@ void WebHandleSetupPreConfig() {
void WebHandleSetup() { void WebHandleSetup() {
String ssidlist; String ssidlist;
WebBuildSSIDList(ssidlist); WebBuildSSIDList(ssidlist);
String dhcpChecked = conf.ip_mode == 0 ? "checked" : "";
String staticChecked = conf.ip_mode == 1 ? "checked" : "";
server.send(200, "text/html", "<form action=\"/save\" method=\"get\">" server.send(200, "text/html", "<form action=\"/save\" method=\"get\">"
@ -105,25 +107,25 @@ void WebHandleSetup() {
"<legend>Wifi configuration:</legend>" "<legend>Wifi configuration:</legend>"
"<div><label for=\"ssid\">Wifi SSID: </label> <br/><input list=\"scan_ssid\" type=\"text\" name=\"ssid\" value=\"" + String(conf.ssid) + "\" /></div>" "<div><label for=\"ssid\">Wifi SSID: </label> <br/><input list=\"scan_ssid\" type=\"text\" name=\"ssid\" value=\"" + String(conf.ssid) + "\" /></div>"
"" + ssidlist + "" "" + ssidlist + ""
"<div><label for=\"password\">Wifi Password: </label><br/><input type=\"password\" name=\"password\" style=\"border-color:red\" /> </div>" "<div><label for=\"password\">Wifi Password: </label><br/><input type=\"password\" name=\"password\" style=\"border-color:red\" value=\"" + String(conf.password) + "\"/> </div>"
"<div><label for=\"host\">Hostname: </label><br/><input type=\"text\" name=\"host\" value=\"" + String(conf.host) + "\" /> </div>" "<div><label for=\"host\">Hostname: </label><br/><input type=\"text\" name=\"host\" value=\"" + String(conf.host) + "\" /> </div>"
"<div><label for=\"channel\">Channel (0 for auto): </label><br/><input type=\"text\" name=\"channel\" value=\"" + String(conf.channel) + "\" /> </div>" "<div><label for=\"channel\">Channel (0 for auto): </label><br/><input type=\"text\" name=\"channel\" value=\"" + String(conf.channel) + "\" /> </div>"
"<div><label for=\"bssid\">BSSID (Empty for auto): </label><br/><input type=\"text\" name=\"bssid\" value=\"" + String(conf.bssid) + "\" /> </div>" "<div><label for=\"bssid\">BSSID (Empty for auto): </label><br/><input type=\"text\" name=\"bssid\" value=\"" + String(conf.bssid) + "\" /> </div>"
"</fieldset>" "</fieldset>"
"<fieldset>" "<fieldset>"
"<legend>IP Configuration</legend>" "<legend>IP Configuration</legend>"
"<div><input type=\"radio\" name=\"ip_config\" value=\"0\" checked>DHCP <input type=\"radio\" name=\"ip_config\" value=\"1\">Static</div>" "<div><input type=\"radio\" name=\"ip_config\" value=\"0\" " + dhcpChecked + ">DHCP <input type=\"radio\" name=\"ip_config\" value=\"1\" " + staticChecked + ">Static</div>"
"<div><label for=\"ip\">Ip :</label><br/><input type=\"text\" name=\"ip\" value=\"" + (conf.ip == 0 ? "192.168.0.123": IPAddress(conf.ip).toString()) + "\" /> </div>" "<div><label for=\"ip\">Ip :</label><br/><input type=\"text\" name=\"ip\" value=\"" + (conf.ip == 0 ? WiFi.localIP().toString() : IPAddress(conf.ip).toString()) + "\" /> </div>"
"<div><label for=\"gw\">Gateway :</label><br/><input type=\"text\" name=\"gw\" value=\"" + (conf.gw == 0 ? "192.168.0.250": IPAddress(conf.gw).toString()) + "\" /> </div>" "<div><label for=\"gw\">Gateway :</label><br/><input type=\"text\" name=\"gw\" value=\"" + (conf.gw == 0 ? WiFi.gatewayIP().toString() : IPAddress(conf.gw).toString()) + "\" /> </div>"
"<div><label for=\"mask\">Netmask :</label><br/><input type=\"text\" name=\"mask\" value=\"" + (conf.mask == 0 ? "255.255.255.0": IPAddress(conf.mask).toString()) + "\" /> </div>" "<div><label for=\"mask\">Netmask :</label><br/><input type=\"text\" name=\"mask\" value=\"" + (conf.mask == 0 ? WiFi.subnetMask().toString() : IPAddress(conf.mask).toString()) + "\" /> </div>"
"<div><label for=\"mask\">DNS :</label><br/><input type=\"text\" name=\"dns\" value=\"" + (conf.dns == 0 ? "192.168.0.250": IPAddress(conf.dns).toString()) + "\" /> </div>" "<div><label for=\"mask\">DNS :</label><br/><input type=\"text\" name=\"dns\" value=\"" + (conf.dns == 0 ? WiFi.dnsIP().toString() : IPAddress(conf.dns).toString()) + "\" /> </div>"
"<div><label for=\"mask\">DNS2 :</label><br/><input type=\"text\" name=\"dns2\" value=\"" + (conf.dns2 == 0 ? "": IPAddress(conf.dns2).toString()) + "\" /> </div>" "<div><label for=\"mask\">DNS2 :</label><br/><input type=\"text\" name=\"dns2\" value=\"" + (conf.dns2 == 0 ? WiFi.dnsIP(1).toString() : IPAddress(conf.dns2).toString()) + "\" /> </div>"
"</fieldset>" "</fieldset>"
"<fieldset>" "<fieldset>"
"<legend>MQTT:</legend>" "<legend>MQTT:</legend>"
"<div><label for=\"mqttServer\">Server :</label><br/><input type=\"text\" name=\"mqttServer\" value=\"" + String(conf.mqttServer) + "\" /> </div>" "<div><label for=\"mqttServer\">Server :</label><br/><input type=\"text\" name=\"mqttServer\" value=\"" + String(conf.mqttServer) + "\" /> </div>"
"<div><label for=\"mqttUser\">Username :</label><br/><input type=\"text\" name=\"mqttUser\" value=\"" + String(conf.mqttUser) + "\" /> </div>" "<div><label for=\"mqttUser\">Username :</label><br/><input type=\"text\" name=\"mqttUser\" value=\"" + String(conf.mqttUser) + "\" /> </div>"
"<div><label for=\"mqttPasswd\">Password :</label><br/><input type=\"password\" name=\"mqttPasswd\" style=\"border-color:red\" /> </div>" "<div><label for=\"mqttPasswd\">Password :</label><br/><input type=\"password\" name=\"mqttPasswd\" style=\"border-color:red\" value=\"" + String(conf.mqttPasswd) + "\" /> </div>"
"<div><label for=\"mqttPort\">Port :</label><br/><input type=\"text\" name=\"mqttPort\" value=\"" + String(conf.mqttPort) + "\" /> (8883 for secure Mqtts) </div>" "<div><label for=\"mqttPort\">Port :</label><br/><input type=\"text\" name=\"mqttPort\" value=\"" + String(conf.mqttPort) + "\" /> (8883 for secure Mqtts) </div>"
"</fieldset>" "</fieldset>"
"<div class=\"button\"> <button type=\"submit\">Save</button></div>" "<div class=\"button\"> <button type=\"submit\">Save</button></div>"
@ -188,18 +190,19 @@ void WebHandleSave() {
strdup(server.arg("host").c_str()), strdup(server.arg("mqttServer").c_str()), strdup(server.arg("mqttUser").c_str()), strdup(server.arg("host").c_str()), strdup(server.arg("mqttServer").c_str()), strdup(server.arg("mqttUser").c_str()),
strdup(server.arg("mqttPasswd").c_str()), server.arg("mqttPort").toInt(), strdup(server.arg("mqttPasswd").c_str()), server.arg("mqttPort").toInt(),
server.arg("ip_config").toInt(), static_cast<uint32_t>(ip), static_cast<uint32_t>(gw), server.arg("ip_config").toInt(), static_cast<uint32_t>(ip), static_cast<uint32_t>(gw),
static_cast<uint32_t>(mask), static_cast<uint32_t>(dns), static_cast<uint32_t>(dns2), server.arg("channel").toInt(), strdup(server.arg("bssid").c_str())}; static_cast<uint32_t>(mask), static_cast<uint32_t>(dns), static_cast<uint32_t>(dns2), server.arg("channel").toInt(), strdup(server.arg("bssid").c_str())
};
if (EepromSaveConfig(newConf) < 0) { if (EepromSaveConfig(newConf) < 0) {
WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
return; return;
} }
if(WiFi.softAPIP() != IPAddress((uint32_t)0)){ if (WiFi.softAPIP() != IPAddress((uint32_t)0)) {
//In STA mode, we can test the AP connection //In STA mode, we can test the AP connection
WiFi.begin(server.arg("ssid").c_str(), server.arg("password").c_str()); WiFi.begin(server.arg("ssid").c_str(), server.arg("password").c_str());
server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>" server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>"
"<a href=\"/wifiStatus\">Check Wifi Configuration</a><br/>" "<a href=\"/wifiStatus\">Check Wifi Configuration</a><br/>"
"<a href=\"/reboot\">Reboot</a><br/>"); "<a href=\"/reboot\">Reboot</a><br/>");
}else{ } else {
server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>" server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>"
"<a href=\"/reboot\">Reboot</a><br/>"); "<a href=\"/reboot\">Reboot</a><br/>");
} }
@ -235,7 +238,7 @@ void WebHandleReboot() {
} }
String statusToString(wl_status_t status) { String statusToString(wl_status_t status) {
switch(status){ switch (status) {
case WL_IDLE_STATUS: return String("Idle"); case WL_IDLE_STATUS: return String("Idle");
case WL_NO_SSID_AVAIL: return String("Wifi not found"); case WL_NO_SSID_AVAIL: return String("Wifi not found");
case WL_CONNECTED: return String("Connected"); case WL_CONNECTED: return String("Connected");
@ -249,7 +252,7 @@ String statusToString(wl_status_t status) {
void WebHandleWifiStatus() { void WebHandleWifiStatus() {
String message; String message;
if(WiFi.status() == WL_DISCONNECTED) if (WiFi.status() == WL_DISCONNECTED)
message += "<head><meta http-equiv=\"refresh\" content=\"1\" ></head>"; message += "<head><meta http-equiv=\"refresh\" content=\"1\" ></head>";
message += "<h1>Wifi Connection Status</h1><br/>"; message += "<h1>Wifi Connection Status</h1><br/>";
message += "Connection to "; message += "Connection to ";
@ -257,19 +260,19 @@ void WebHandleWifiStatus() {
message += ":<br/>"; message += ":<br/>";
message += statusToString(WiFi.status()); message += statusToString(WiFi.status());
message += "<br/>"; message += "<br/>";
if(mode == BOOTMODE_SETUP && WiFi.status() == WL_CONNECTED){ if (mode == BOOTMODE_SETUP && WiFi.status() == WL_CONNECTED) {
message += "Wifi correctly setup! You can reboot now<br/>"; message += "Wifi correctly setup! You can reboot now<br/>";
message += "<a href=\"/reboot\">Reboot</a><br/>"; message += "<a href=\"/reboot\">Reboot</a><br/>";
} }
if(WiFi.status() == WL_CONNECT_FAILED || WiFi.status() == WL_NO_SSID_AVAIL){ if (WiFi.status() == WL_CONNECT_FAILED || WiFi.status() == WL_NO_SSID_AVAIL) {
message += "Try to reconfigure you WiFi details<br/>"; message += "Try to reconfigure you WiFi details<br/>";
message += "<a href=\"/setup\">Enter Setup</a><br/>"; message += "<a href=\"/setup\">Enter Setup</a><br/>";
} }
server.send(200, "text/html", message); server.send(200, "text/html", message);
} }
void WebBuildGpioControl(){ void WebBuildGpioControl() {
if (NB_ELEMENTS(gpioControlled) > 0){ if (NB_ELEMENTS(gpioControlled) > 0) {
gpioControlHTML += "<fieldset>" gpioControlHTML += "<fieldset>"
"<legend>Relay</legend>"; "<legend>Relay</legend>";
for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled) ; i++) { for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled) ; i++) {
@ -280,8 +283,8 @@ void WebBuildGpioControl(){
} }
} }
void WebBuildPwmControl(){ void WebBuildPwmControl() {
if (NB_ELEMENTS(pwmControlled) > 0){ if (NB_ELEMENTS(pwmControlled) > 0) {
pwmControlHTML += "<fieldset>" pwmControlHTML += "<fieldset>"
"<legend>PWM</legend>"; "<legend>PWM</legend>";
for (uint i = 0 ; i < NB_ELEMENTS(pwmControlled) ; i++) { for (uint i = 0 ; i < NB_ELEMENTS(pwmControlled) ; i++) {
@ -321,12 +324,12 @@ void WebSetupServer(int ) {
} }
#else #else
void WebHandleRoot(){} void WebHandleRoot() {}
void WebHandleSetupPreConfig(){} void WebHandleSetupPreConfig() {}
void WebHandleSetup(){} void WebHandleSetup() {}
void WebHandleGpio(){} void WebHandleGpio() {}
void WebHandleSave(){} void WebHandleSave() {}
void WebHandleOTA(){} void WebHandleOTA() {}
void WebHandleNotFound(){} void WebHandleNotFound() {}
void WebSetupServer(int bootmode){} void WebSetupServer(int bootmode) {}
#endif #endif