From fea3b73f54e657cd18e3f78146206b9d27139e82 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Mon, 3 Jun 2019 00:07:12 +0200 Subject: [PATCH] WebServer: improve Setup Keep password. Fix ip mode previous state --- WifiControlSensor/WebServer.ino | 107 ++++++++++++++++---------------- 1 file changed, 55 insertions(+), 52 deletions(-) diff --git a/WifiControlSensor/WebServer.ino b/WifiControlSensor/WebServer.ino index f998fc8..03b5ac6 100644 --- a/WifiControlSensor/WebServer.ino +++ b/WifiControlSensor/WebServer.ino @@ -6,10 +6,10 @@ String gpioControlHTML = ""; String pwmControlHTML = ""; -void WebBuildGpioObserved(String &html){ - if (NB_ELEMENTS(gpioObserved) > 0){ +void WebBuildGpioObserved(String &html) { + if (NB_ELEMENTS(gpioObserved) > 0) { html += "
" - "Detector"; + "Detector"; for (uint i = 0 ; i < NB_ELEMENTS(gpioObserved) ; i++) { html += "Sensor " + String(gpioObserved[i]) + ": " + digitalRead(gpioObserved[i]) + "
"; } @@ -22,7 +22,7 @@ void WebHandleRoot() { String optimiseConfig = ""; WebBuildGpioObserved(gpioObserved); - if(WiFi.status() == WL_CONNECTED){ + if (WiFi.status() == WL_CONNECTED) { optimiseConfig = "Optimize Config
"; } @@ -40,7 +40,7 @@ void WebHandleRoot() { "Humidity DHT " + String(dhtHumidity, 0) + "%
" #endif #ifdef CONFIG_ENABLE_DRY_SENSOR - "Dryness " + String((dryness*100)/1024) + "%
" + "Dryness " + String((dryness * 100) / 1024) + "%
" #endif "
" + gpioControlHTML + gpioObserved + pwmControlHTML + "
" "Settings" @@ -62,7 +62,7 @@ void WebSendError(const char *error) { server.send(500, "text/plain", error); } -void WebBuildSSIDList(String &datalist){ +void WebBuildSSIDList(String &datalist) { int n = WiFi.scanNetworks(); datalist = ""; // sort by RSSI @@ -77,27 +77,29 @@ void WebBuildSSIDList(String &datalist){ } } } - for (int i = 0; i < n; ++i){ - datalist += ""; } void WebHandleSetupPreConfig() { - conf.bssid =strdup( WiFi.BSSIDstr().c_str()); - conf.channel = WiFi.channel(); - conf.ip_mode = 1; - conf.ip = WiFi.localIP(); - conf.mask = WiFi.subnetMask(); - conf.gw = WiFi.gatewayIP(); - conf.dns = WiFi.dnsIP(); - conf.dns2 = WiFi.dnsIP(1); - WebHandleSetup(); + conf.bssid = strdup( WiFi.BSSIDstr().c_str()); + conf.channel = WiFi.channel(); + conf.ip_mode = 1; + conf.ip = WiFi.localIP(); + conf.mask = WiFi.subnetMask(); + conf.gw = WiFi.gatewayIP(); + conf.dns = WiFi.dnsIP(); + conf.dns2 = WiFi.dnsIP(1); + WebHandleSetup(); } void WebHandleSetup() { String ssidlist; WebBuildSSIDList(ssidlist); + String dhcpChecked = conf.ip_mode == 0 ? "checked" : ""; + String staticChecked = conf.ip_mode == 1 ? "checked" : ""; server.send(200, "text/html", "
" @@ -105,25 +107,25 @@ void WebHandleSetup() { "Wifi configuration:" "

" "" + ssidlist + "" - "

" + "

" "

" "

" "

" "
" "
" "IP Configuration" - "
DHCP Static
" - "

" - "

" - "

" - "

" - "

" + "
DHCP Static
" + "

" + "

" + "

" + "

" + "

" "
" "
" "MQTT:" "

" "

" - "

" + "

" "

(8883 for secure Mqtts)
" "
" "
" @@ -185,23 +187,24 @@ void WebHandleSave() { } productConfig newConf = {BOOTMODE_NORMAL, strdup(server.arg("ssid").c_str()), strdup(server.arg("password").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(), - server.arg("ip_config").toInt(), static_cast(ip), static_cast(gw), - static_cast(mask), static_cast(dns), static_cast(dns2), server.arg("channel").toInt(), strdup(server.arg("bssid").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(), + server.arg("ip_config").toInt(), static_cast(ip), static_cast(gw), + static_cast(mask), static_cast(dns), static_cast(dns2), server.arg("channel").toInt(), strdup(server.arg("bssid").c_str()) + }; if (EepromSaveConfig(newConf) < 0) { WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); return; } - if(WiFi.softAPIP() != IPAddress((uint32_t)0)){ + if (WiFi.softAPIP() != IPAddress((uint32_t)0)) { //In STA mode, we can test the AP connection WiFi.begin(server.arg("ssid").c_str(), server.arg("password").c_str()); server.send(200, "text/html", "

Configuration Saved


" - "Check Wifi Configuration
" - "Reboot
"); - }else{ + "Check Wifi Configuration
" + "Reboot
"); + } else { server.send(200, "text/html", "

Configuration Saved


" - "Reboot
"); + "Reboot
"); } } @@ -235,7 +238,7 @@ void WebHandleReboot() { } String statusToString(wl_status_t status) { - switch(status){ + switch (status) { case WL_IDLE_STATUS: return String("Idle"); case WL_NO_SSID_AVAIL: return String("Wifi not found"); case WL_CONNECTED: return String("Connected"); @@ -249,7 +252,7 @@ String statusToString(wl_status_t status) { void WebHandleWifiStatus() { String message; - if(WiFi.status() == WL_DISCONNECTED) + if (WiFi.status() == WL_DISCONNECTED) message += ""; message += "

Wifi Connection Status


"; message += "Connection to "; @@ -257,21 +260,21 @@ void WebHandleWifiStatus() { message += ":
"; message += statusToString(WiFi.status()); message += "
"; - if(mode == BOOTMODE_SETUP && WiFi.status() == WL_CONNECTED){ + if (mode == BOOTMODE_SETUP && WiFi.status() == WL_CONNECTED) { message += "Wifi correctly setup! You can reboot now
"; message += "Reboot
"; } - 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
"; message += "Enter Setup
"; } server.send(200, "text/html", message); } -void WebBuildGpioControl(){ - if (NB_ELEMENTS(gpioControlled) > 0){ +void WebBuildGpioControl() { + if (NB_ELEMENTS(gpioControlled) > 0) { gpioControlHTML += "
" - "Relay"; + "Relay"; for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled) ; i++) { gpioControlHTML += "Relay " + String(gpioControlled[i]) + " " + "ON/"; gpioControlHTML += "OFF
"; @@ -280,10 +283,10 @@ void WebBuildGpioControl(){ } } -void WebBuildPwmControl(){ - if (NB_ELEMENTS(pwmControlled) > 0){ +void WebBuildPwmControl() { + if (NB_ELEMENTS(pwmControlled) > 0) { pwmControlHTML += "
" - "PWM"; + "PWM"; for (uint i = 0 ; i < NB_ELEMENTS(pwmControlled) ; i++) { pwmControlHTML += "PWM " + String(pwmControlled[i]) + "
"; pwmControlHTML += "