diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 0082190..17358ce 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -301,7 +301,7 @@ int WiFiManager::connectWifi(String ssid, String pass) { DEBUG_WM(WiFi.localIP()); } //fix for auto connect racing issue - if (WiFi.status() == WL_CONNECTED) { + if (WiFi.status() == WL_CONNECTED && (WiFi.SSID() == ssid)) { DEBUG_WM(F("Already connected. Bailing out.")); return WL_CONNECTED; } @@ -322,7 +322,7 @@ int WiFiManager::connectWifi(String ssid, String pass) { DEBUG_WM(res); } } else { - if (WiFi.SSID()) { + if (WiFi.SSID() != "") { DEBUG_WM(F("Using last saved values, should be faster")); //trying to fix connection in progress hanging ETS_UART_INTR_DISABLE(); @@ -362,7 +362,7 @@ uint8_t WiFiManager::waitForConnectResult() { keepConnecting = false; DEBUG_WM (F("Connection timed out")); } - if (status == WL_CONNECTED || status == WL_CONNECT_FAILED) { + if (status == WL_CONNECTED) { keepConnecting = false; } delay(100); @@ -450,12 +450,12 @@ void WiFiManager::handleRoot() { return; } - String page = FPSTR(HTTP_HEAD); + String page = FPSTR(HTTP_HEADER); page.replace("{v}", "Options"); page += FPSTR(HTTP_SCRIPT); page += FPSTR(HTTP_STYLE); page += _customHeadElement; - page += FPSTR(HTTP_HEAD_END); + page += FPSTR(HTTP_HEADER_END); page += String(F("

")); page += _apName; page += String(F("

")); @@ -471,12 +471,12 @@ void WiFiManager::handleRoot() { /** Wifi config page handler */ void WiFiManager::handleWifi(boolean scan) { - String page = FPSTR(HTTP_HEAD); + String page = FPSTR(HTTP_HEADER); page.replace("{v}", "Config ESP"); page += FPSTR(HTTP_SCRIPT); page += FPSTR(HTTP_STYLE); page += _customHeadElement; - page += FPSTR(HTTP_HEAD_END); + page += FPSTR(HTTP_HEADER_END); if (scan) { int n = WiFi.scanNetworks(); @@ -666,12 +666,12 @@ void WiFiManager::handleWifiSave() { optionalIPFromString(&_sta_static_sn, sn.c_str()); } - String page = FPSTR(HTTP_HEAD); + String page = FPSTR(HTTP_HEADER); page.replace("{v}", "Credentials Saved"); page += FPSTR(HTTP_SCRIPT); page += FPSTR(HTTP_STYLE); page += _customHeadElement; - page += FPSTR(HTTP_HEAD_END); + page += FPSTR(HTTP_HEADER_END); page += FPSTR(HTTP_SAVED); page += FPSTR(HTTP_END); @@ -687,12 +687,12 @@ void WiFiManager::handleWifiSave() { void WiFiManager::handleInfo() { DEBUG_WM(F("Info")); - String page = FPSTR(HTTP_HEAD); + String page = FPSTR(HTTP_HEADER); page.replace("{v}", "Info"); page += FPSTR(HTTP_SCRIPT); page += FPSTR(HTTP_STYLE); page += _customHeadElement; - page += FPSTR(HTTP_HEAD_END); + page += FPSTR(HTTP_HEADER_END); page += F("
"); page += F("
Chip ID
"); page += ESP.getChipId(); @@ -728,12 +728,12 @@ void WiFiManager::handleInfo() { void WiFiManager::handleReset() { DEBUG_WM(F("Reset")); - String page = FPSTR(HTTP_HEAD); + String page = FPSTR(HTTP_HEADER); page.replace("{v}", "Info"); page += FPSTR(HTTP_SCRIPT); page += FPSTR(HTTP_STYLE); page += _customHeadElement; - page += FPSTR(HTTP_HEAD_END); + page += FPSTR(HTTP_HEADER_END); page += F("Module will reset in a few seconds."); page += FPSTR(HTTP_END); diff --git a/WiFiManager.h b/WiFiManager.h index 64bb96e..136dde0 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -22,10 +22,10 @@ extern "C" { #include "user_interface.h" } -const char HTTP_HEAD[] PROGMEM = "{v}"; +const char HTTP_HEADER[] PROGMEM = "{v}"; const char HTTP_STYLE[] PROGMEM = ""; const char HTTP_SCRIPT[] PROGMEM = ""; -const char HTTP_HEAD_END[] PROGMEM = "
"; +const char HTTP_HEADER_END[] PROGMEM = "
"; const char HTTP_PORTAL_OPTIONS[] PROGMEM = "



"; const char HTTP_ITEM[] PROGMEM = "
{v} {r}%
"; const char HTTP_FORM_START[] PROGMEM = "


"; @@ -125,7 +125,7 @@ class WiFiManager //const int WM_DONE = 0; //const int WM_WAIT = 10; - //const String HTTP_HEAD = "{v}"; + //const String HTTP_HEADER = "{v}"; void setupConfigPortal(); void startWPS(); diff --git a/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino b/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino index 7e6c373..c62ca7b 100644 --- a/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino +++ b/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino @@ -1,7 +1,5 @@ #include //this needs to be first, or it all crashes and burns... - #include //https://github.com/esp8266/Arduino - //needed for library #include #include @@ -23,7 +21,6 @@ void saveConfigCallback () { shouldSaveConfig = true; } - void setup() { // put your setup code here, to run once: Serial.begin(115200); @@ -48,16 +45,22 @@ void setup() { std::unique_ptr buf(new char[size]); configFile.readBytes(buf.get(), size); + +#ifdef ARDUINOJSON_VERSION_MAJOR >= 6 + DynamicJsonDocument json(1024); + auto deserializeError = deserializeJson(json, buf.get()); + serializeJson(json, Serial); + if ( ! deserializeError ) { +#else DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.parseObject(buf.get()); json.printTo(Serial); if (json.success()) { +#endif Serial.println("\nparsed json"); - strcpy(mqtt_server, json["mqtt_server"]); strcpy(mqtt_port, json["mqtt_port"]); strcpy(blynk_token, json["blynk_token"]); - } else { Serial.println("failed to load json config"); } @@ -69,8 +72,6 @@ void setup() { } //end read - - // The extra parameters to be configured (can be either global or just in the setup) // After connecting, parameter.getValue() will get you the configured value // id/name placeholder/prompt default length @@ -86,8 +87,8 @@ void setup() { wifiManager.setSaveConfigCallback(saveConfigCallback); //set static ip - wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); - + wifiManager.setSTAStaticIPConfig(IPAddress(10, 0, 1, 99), IPAddress(10, 0, 1, 1), IPAddress(255, 255, 255, 0)); + //add all your parameters here wifiManager.addParameter(&custom_mqtt_server); wifiManager.addParameter(&custom_mqtt_port); @@ -99,7 +100,7 @@ void setup() { //set minimu quality of signal so it ignores AP's under that quality //defaults to 8% //wifiManager.setMinimumSignalQuality(); - + //sets timeout until configuration portal gets turned off //useful to make it all retry or go to sleep //in seconds @@ -124,12 +125,20 @@ void setup() { strcpy(mqtt_server, custom_mqtt_server.getValue()); strcpy(mqtt_port, custom_mqtt_port.getValue()); strcpy(blynk_token, custom_blynk_token.getValue()); + Serial.println("The values in the file are: "); + Serial.println("\tmqtt_server : " + String(mqtt_server)); + Serial.println("\tmqtt_port : " + String(mqtt_port)); + Serial.println("\tblynk_token : " + String(blynk_token)); //save the custom parameters to FS if (shouldSaveConfig) { Serial.println("saving config"); +#ifdef ARDUINOJSON_VERSION_MAJOR >= 6 + DynamicJsonDocument json(1024); +#else DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.createObject(); +#endif json["mqtt_server"] = mqtt_server; json["mqtt_port"] = mqtt_port; json["blynk_token"] = blynk_token; @@ -139,19 +148,22 @@ void setup() { Serial.println("failed to open config file for writing"); } +#ifdef ARDUINOJSON_VERSION_MAJOR >= 6 + serializeJson(json, Serial); + serializeJson(json, configFile); +#else json.printTo(Serial); json.printTo(configFile); +#endif configFile.close(); //end save } Serial.println("local ip"); Serial.println(WiFi.localIP()); - } void loop() { // put your main code here, to run repeatedly: - } diff --git a/examples/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino b/examples/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino index 3f391ae..e510a74 100644 --- a/examples/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino +++ b/examples/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino @@ -10,10 +10,10 @@ #include //https://github.com/bblanchon/ArduinoJson //define your default values here, if there are different values in config.json, they are overwritten. -//length should be max size + 1 +//length should be max size + 1 char mqtt_server[40]; char mqtt_port[6] = "8080"; -char blynk_token[33] = "YOUR_BLYNK_TOKEN"; +char blynk_token[34] = "YOUR_BLYNK_TOKEN"; //default custom static IP char static_ip[16] = "10.0.1.56"; char static_gw[16] = "10.0.1.1"; @@ -52,29 +52,29 @@ void setup() { std::unique_ptr buf(new char[size]); configFile.readBytes(buf.get(), size); +#ifdef ARDUINOJSON_VERSION_MAJOR >= 6 + DynamicJsonDocument json(1024); + auto deserializeError = deserializeJson(json, buf.get()); + serializeJson(json, Serial); + if ( ! deserializeError ) { +#else DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.parseObject(buf.get()); json.printTo(Serial); if (json.success()) { +#endif Serial.println("\nparsed json"); strcpy(mqtt_server, json["mqtt_server"]); strcpy(mqtt_port, json["mqtt_port"]); strcpy(blynk_token, json["blynk_token"]); - if(json["ip"]) { + if (json["ip"]) { Serial.println("setting custom ip from config"); - //static_ip = json["ip"]; strcpy(static_ip, json["ip"]); strcpy(static_gw, json["gateway"]); strcpy(static_sn, json["subnet"]); - //strcat(static_ip, json["ip"]); - //static_gw = json["gateway"]; - //static_sn = json["subnet"]; Serial.println(static_ip); -/* Serial.println("converting ip"); - IPAddress ip = ipFromCharArray(static_ip); - Serial.println(ip);*/ } else { Serial.println("no custom ip in config"); } @@ -107,13 +107,13 @@ void setup() { wifiManager.setSaveConfigCallback(saveConfigCallback); //set static ip - IPAddress _ip,_gw,_sn; + IPAddress _ip, _gw, _sn; _ip.fromString(static_ip); _gw.fromString(static_gw); _sn.fromString(static_sn); wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn); - + //add all your parameters here wifiManager.addParameter(&custom_mqtt_server); wifiManager.addParameter(&custom_mqtt_port); @@ -125,7 +125,7 @@ void setup() { //set minimu quality of signal so it ignores AP's under that quality //defaults to 8% wifiManager.setMinimumSignalQuality(); - + //sets timeout until configuration portal gets turned off //useful to make it all retry or go to sleep //in seconds @@ -154,8 +154,12 @@ void setup() { //save the custom parameters to FS if (shouldSaveConfig) { Serial.println("saving config"); +#ifdef ARDUINOJSON_VERSION_MAJOR >= 6 + DynamicJsonDocument json(1024); +#else DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.createObject(); +#endif json["mqtt_server"] = mqtt_server; json["mqtt_port"] = mqtt_port; json["blynk_token"] = blynk_token; @@ -169,8 +173,13 @@ void setup() { Serial.println("failed to open config file for writing"); } - json.prettyPrintTo(Serial); +#ifdef ARDUINOJSON_VERSION_MAJOR >= 6 + serializeJson(json, Serial); + serializeJson(json, configFile); +#else + json.printTo(Serial); json.printTo(configFile); +#endif configFile.close(); //end save } @@ -183,6 +192,4 @@ void setup() { void loop() { // put your main code here, to run repeatedly: - - } diff --git a/library.json b/library.json index 8f0830a..d8b1d5e 100644 --- a/library.json +++ b/library.json @@ -9,5 +9,5 @@ }, "frameworks": "arduino", "platforms": "espressif8266", - "version": "0.14" + "version": "0.15.0-beta" } diff --git a/library.properties b/library.properties index e0420ad..743922e 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=WiFiManager -version=0.14 +version=0.15.0-beta author=tzapu maintainer=tzapu sentence=ESP8266 WiFi Connection manager with fallback web configuration portal