From 486ea7db2c769df83527619dc1356740fbb84b30 Mon Sep 17 00:00:00 2001 From: Alex Goris Date: Thu, 4 May 2017 21:59:17 +0200 Subject: [PATCH] Allow showing static IP config without predefined value Previously a valid predefined static IP had to be passed before the config page would show the IP, gateway and subnet fields. This did not allow the config page to show 'optional' static IP fields. Now a boolean can be passed to the autoConnect() function which will specify whether the static IP fields should be shown or not. When this boolean is true but no predefined values are passed and fields are left blank in config page, wifiManager will revert to DHCP. This allows user to choose between static IP or DHCP without changing code --- WiFiManager.cpp | 16 +++++++++------- WiFiManager.h | 7 ++++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 06e7481..8ac235c 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -131,15 +131,17 @@ void WiFiManager::setupConfigPortal() { } -boolean WiFiManager::autoConnect() { +boolean WiFiManager::autoConnect(boolean showStaticIPFields) { String ssid = "ESP" + String(ESP.getChipId()); - return autoConnect(ssid.c_str(), NULL); + return autoConnect(ssid.c_str(), NULL, showStaticIPFields); } -boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { +boolean WiFiManager::autoConnect(char const *apName, char const *apPassword, boolean showStaticIPFields) { DEBUG_WM(F("")); DEBUG_WM(F("AutoConnect")); + _sta_show_static_fields = showStaticIPFields; + // read eeprom for ssid and pass //String ssid = getSSID(); //String pass = getPassword(); @@ -498,14 +500,14 @@ void WiFiManager::handleWifi(boolean scan) { page += "
"; } - if (_sta_static_ip) { + if (_sta_show_static_fields) { String item = FPSTR(HTTP_FORM_PARAM); item.replace("{i}", "ip"); item.replace("{n}", "ip"); item.replace("{p}", "Static IP"); item.replace("{l}", "15"); - item.replace("{v}", _sta_static_ip.toString()); + item.replace("{v}", (_sta_static_ip ? _sta_static_ip.toString() : "")); page += item; @@ -514,7 +516,7 @@ void WiFiManager::handleWifi(boolean scan) { item.replace("{n}", "gw"); item.replace("{p}", "Static Gateway"); item.replace("{l}", "15"); - item.replace("{v}", _sta_static_gw.toString()); + item.replace("{v}", (_sta_static_gw ? _sta_static_gw.toString() : "")); page += item; @@ -523,7 +525,7 @@ void WiFiManager::handleWifi(boolean scan) { item.replace("{n}", "sn"); item.replace("{p}", "Subnet"); item.replace("{l}", "15"); - item.replace("{v}", _sta_static_sn.toString()); + item.replace("{v}", (_sta_static_sn ? _sta_static_sn.toString() : "")); page += item; diff --git a/WiFiManager.h b/WiFiManager.h index bd67c05..3edff0a 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -35,7 +35,7 @@ const char HTTP_SCAN_LINK[] PROGMEM = "