From 362a561a35960ea8ae8a67f2ab2daa5af2f242df Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Sun, 4 Jun 2017 06:41:27 -0700 Subject: [PATCH 01/29] use malloc/free for WiFiManagerParameter* array instead of hard coding. add alternate constructor to specifiy max parameter count origional default constructor uses old hard codded value --- README.md | 5 +++++ WiFiManager.cpp | 27 +++++++++++++++++++++++++-- WiFiManager.h | 6 +++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a329f2b..3e83d4b 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,11 @@ You should also take a look at adding custom HTML to your form. - Save and load custom parameters to file system in json form [AutoConnectWithFSParameters](https://github.com/tzapu/WiFiManager/tree/master/examples/AutoConnectWithFSParameters) - *Save and load custom parameters to EEPROM* (not done yet) +NOTE: if your need more than 10 (the default) custom parameters you will need to use the alternate constructor: +```cpp +WiFiManager wifiManager(15); // pass the number of params you will be adding +``` + #### Custom IP Configuration You can set a custom IP for both AP (access point, config mode) and STA (station mode, client mode, normal project state) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 4fba355..4be0105 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -60,14 +60,37 @@ const char* WiFiManagerParameter::getCustomHTML() { return _customHTML; } + WiFiManager::WiFiManager() { + init(WIFI_MANAGER_MAX_PARAMS); +} + +WiFiManager::WiFiManager(int max_params) { + init(max_params); +} + +void WiFiManager::init(int max_params) +{ + DEBUG_WM(F("setting max_params to:")); + DEBUG_WM(max_params); + _max_params = max_params; + _params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); +} + +WiFiManager::~WiFiManager() +{ + if (_params != NULL) + { + DEBUG_WM(F("freeing allocated params!")); + free(_params); + } } void WiFiManager::addParameter(WiFiManagerParameter *p) { - if(_paramsCount + 1 > WIFI_MANAGER_MAX_PARAMS) + if(_paramsCount + 1 > _max_params) { //Max parameters exceeded! - DEBUG_WM("WIFI_MANAGER_MAX_PARAMS exceeded, increase number (in WiFiManager.h) before adding more parameters!"); + DEBUG_WM("max_params value exceeded, use WiFiManager(int max_params) constructor with a larger value!"); DEBUG_WM("Skipping parameter with ID:"); DEBUG_WM(p->getID()); return; diff --git a/WiFiManager.h b/WiFiManager.h index d86b904..2c8dece 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -65,6 +65,8 @@ class WiFiManager { public: WiFiManager(); + WiFiManager(int max_params); + ~WiFiManager(); boolean autoConnect(); boolean autoConnect(char const *apName, char const *apPassword = NULL); @@ -119,6 +121,7 @@ class WiFiManager //const String HTTP_HEAD = "{v}"; + void init(int max_params); void setupConfigPortal(); void startWPS(); @@ -176,7 +179,8 @@ class WiFiManager void (*_apcallback)(WiFiManager*) = NULL; void (*_savecallback)(void) = NULL; - WiFiManagerParameter* _params[WIFI_MANAGER_MAX_PARAMS]; + int _max_params; + WiFiManagerParameter** _params; template void DEBUG_WM(Generic text); From 3950a3b859f9a3c85dbcce21165f71ff1ff2b6c5 Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Tue, 27 Jun 2017 16:24:23 -0700 Subject: [PATCH 02/29] use single constructor with default value for size --- WiFiManager.cpp | 4 ---- WiFiManager.h | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 4be0105..081c714 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -61,10 +61,6 @@ const char* WiFiManagerParameter::getCustomHTML() { } -WiFiManager::WiFiManager() { - init(WIFI_MANAGER_MAX_PARAMS); -} - WiFiManager::WiFiManager(int max_params) { init(max_params); } diff --git a/WiFiManager.h b/WiFiManager.h index 2c8dece..ee1521e 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -65,7 +65,7 @@ class WiFiManager { public: WiFiManager(); - WiFiManager(int max_params); + WiFiManager(int max_params=10); ~WiFiManager(); boolean autoConnect(); From c4905c23b789df4f687d18923ebcd0855ded7968 Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Tue, 27 Jun 2017 16:27:37 -0700 Subject: [PATCH 03/29] remove init function as its not needed with one constructor --- WiFiManager.cpp | 5 ----- WiFiManager.h | 1 - 2 files changed, 6 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 081c714..a24cbca 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -62,11 +62,6 @@ const char* WiFiManagerParameter::getCustomHTML() { WiFiManager::WiFiManager(int max_params) { - init(max_params); -} - -void WiFiManager::init(int max_params) -{ DEBUG_WM(F("setting max_params to:")); DEBUG_WM(max_params); _max_params = max_params; diff --git a/WiFiManager.h b/WiFiManager.h index ee1521e..6b59ecd 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -121,7 +121,6 @@ class WiFiManager //const String HTTP_HEAD = "{v}"; - void init(int max_params); void setupConfigPortal(); void startWPS(); From 898768092cab07c748c70b00dbbb6a627e50a00a Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Tue, 27 Jun 2017 16:35:48 -0700 Subject: [PATCH 04/29] removed un-needed constructor declaration --- WiFiManager.h | 1 - 1 file changed, 1 deletion(-) diff --git a/WiFiManager.h b/WiFiManager.h index 6b59ecd..5b7893c 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -64,7 +64,6 @@ class WiFiManagerParameter { class WiFiManager { public: - WiFiManager(); WiFiManager(int max_params=10); ~WiFiManager(); From 6465d72f6e76cea610496240c682bdbce30b0ea3 Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Thu, 26 Oct 2017 15:23:47 -0700 Subject: [PATCH 05/29] Refactored to use only a default constructor and automatically resize the parameter array each time the size limit is reached. I used the initial size (WIFI_MANAGER_MAX_PARAMS) as the increment when resizing. Existing data is coppied to the new array and the old one is free'd. --- WiFiManager.cpp | 21 ++++++++++++--------- WiFiManager.h | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 16d9f5f..cf5d801 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -61,10 +61,8 @@ const char* WiFiManagerParameter::getCustomHTML() { } -WiFiManager::WiFiManager(int max_params) { - DEBUG_WM(F("setting max_params to:")); - DEBUG_WM(max_params); - _max_params = max_params; +WiFiManager::WiFiManager() { + _max_params = WIFI_MANAGER_MAX_PARAMS; _params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); } @@ -80,12 +78,17 @@ WiFiManager::~WiFiManager() void WiFiManager::addParameter(WiFiManagerParameter *p) { if(_paramsCount + 1 > _max_params) { - //Max parameters exceeded! - DEBUG_WM("max_params value exceeded, use WiFiManager(int max_params) constructor with a larger value!"); - DEBUG_WM("Skipping parameter with ID:"); - DEBUG_WM(p->getID()); - return; + // rezise the params array + _max_params += WIFI_MANAGER_MAX_PARAMS; + DEBUG_WM(F("Increasing _max_params to:")); + DEBUG_WM(_max_params); + WiFiManagerParameter** new_params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); + // copy old data + memcpy(new_params, _params, _paramsCount * sizeof(WiFiManagerParameter*)); + free(_params); + _params = new_params; } + _params[_paramsCount] = p; _paramsCount++; DEBUG_WM("Adding parameter"); diff --git a/WiFiManager.h b/WiFiManager.h index 364b7b7..f95ce1b 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -64,7 +64,7 @@ class WiFiManagerParameter { class WiFiManager { public: - WiFiManager(int max_params=10); + WiFiManager(); ~WiFiManager(); boolean autoConnect(); From 0b6341bea431767d95008f9a36c9b5d5c538ff08 Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Thu, 26 Oct 2017 16:10:34 -0700 Subject: [PATCH 06/29] cleanup readme (removed hint about alternate constructor) --- README.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/README.md b/README.md index 3f8996f..10da7f6 100644 --- a/README.md +++ b/README.md @@ -207,11 +207,6 @@ You should also take a look at adding custom HTML to your form. - Save and load custom parameters to file system in json form [AutoConnectWithFSParameters](https://github.com/tzapu/WiFiManager/tree/master/examples/AutoConnectWithFSParameters) - *Save and load custom parameters to EEPROM* (not done yet) -NOTE: if your need more than 10 (the default) custom parameters you will need to use the alternate constructor: -```cpp -WiFiManager wifiManager(15); // pass the number of params you will be adding -``` - #### Custom IP Configuration You can set a custom IP for both AP (access point, config mode) and STA (station mode, client mode, normal project state) From eb560629d6c22f8e77a2b6aed94a9143d596447f Mon Sep 17 00:00:00 2001 From: Luke Palmer Date: Mon, 4 Dec 2017 19:54:59 -0500 Subject: [PATCH 07/29] Document parameters --- WiFiManager.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/WiFiManager.h b/WiFiManager.h index 5b37e44..6c46b1e 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -39,6 +39,10 @@ const char HTTP_END[] PROGMEM = ""; class WiFiManagerParameter { public: + /** + Create custom parameters that can be added to the WiFiManager setup web page + @id is used for HTTP queries and must not contain spaces nor other special characters + */ WiFiManagerParameter(const char *custom); WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length); WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom); From d66cd47df87f2b478e7f7568b7c8e141c02dd53a Mon Sep 17 00:00:00 2001 From: Christopher Liebman Date: Sat, 9 Dec 2017 06:04:07 -0800 Subject: [PATCH 08/29] use realloc() when increasing the size of _params. addParameter() now returns bool indicating the status of adding the parameter where false indicates it failed to increase the size of _params and did not add the parameter. --- WiFiManager.cpp | 15 +++++++++------ WiFiManager.h | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index cf5d801..a82480a 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -75,24 +75,27 @@ WiFiManager::~WiFiManager() } } -void WiFiManager::addParameter(WiFiManagerParameter *p) { +bool WiFiManager::addParameter(WiFiManagerParameter *p) { if(_paramsCount + 1 > _max_params) { // rezise the params array _max_params += WIFI_MANAGER_MAX_PARAMS; DEBUG_WM(F("Increasing _max_params to:")); DEBUG_WM(_max_params); - WiFiManagerParameter** new_params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); - // copy old data - memcpy(new_params, _params, _paramsCount * sizeof(WiFiManagerParameter*)); - free(_params); - _params = new_params; + WiFiManagerParameter** new_params = (WiFiManagerParameter**)realloc(_params, _max_params * sizeof(WiFiManagerParameter*)); + if (new_params != NULL) { + _params = new_params; + } else { + DEBUG_WM("ERROR: failed to realloc params, size not increased!"); + return false; + } } _params[_paramsCount] = p; _paramsCount++; DEBUG_WM("Adding parameter"); DEBUG_WM(p->getID()); + return true; } void WiFiManager::setupConfigPortal() { diff --git a/WiFiManager.h b/WiFiManager.h index f95ce1b..9a22573 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -100,8 +100,8 @@ class WiFiManager void setAPCallback( void (*func)(WiFiManager*) ); //called when settings have been changed and connection was successful void setSaveConfigCallback( void (*func)(void) ); - //adds a custom parameter - void addParameter(WiFiManagerParameter *p); + //adds a custom parameter, returns false on failure + bool addParameter(WiFiManagerParameter *p); //if this is set, it will exit after config, even if connection is unsuccessful. void setBreakAfterConfig(boolean shouldBreak); //if this is set, try WPS setup when starting (this will delay config portal for up to 2 mins) From a5ff5cb9e4bffeb841b3d5e172bd4f408a690f9a Mon Sep 17 00:00:00 2001 From: Christopher Liebman Date: Sat, 9 Dec 2017 06:24:15 -0800 Subject: [PATCH 09/29] Add WiFiManagerParameter destructor to free new'd _value, it was allocated but never free'd --- WiFiManager.cpp | 6 ++++++ WiFiManager.h | 1 + 2 files changed, 7 insertions(+) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 97c9eea..9d6dc3e 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -44,6 +44,12 @@ void WiFiManagerParameter::init(const char *id, const char *placeholder, const c _customHTML = custom; } +WiFiManagerParameter::~WiFiManagerParameter() { + if (_value != NULL) { + delete _value; + } +} + const char* WiFiManagerParameter::getValue() { return _value; } diff --git a/WiFiManager.h b/WiFiManager.h index 5b37e44..9a599db 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -42,6 +42,7 @@ class WiFiManagerParameter { WiFiManagerParameter(const char *custom); WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length); WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom); + ~WiFiManagerParameter(); const char *getID(); const char *getValue(); From 5c6252fe45144a6fcd5d67cc9c47e27fde33daa2 Mon Sep 17 00:00:00 2001 From: Christopher Liebman Date: Sat, 9 Dec 2017 08:37:35 -0800 Subject: [PATCH 10/29] should use delete[] not delete --- WiFiManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 9d6dc3e..ae04fa9 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -46,7 +46,7 @@ void WiFiManagerParameter::init(const char *id, const char *placeholder, const c WiFiManagerParameter::~WiFiManagerParameter() { if (_value != NULL) { - delete _value; + delete[] _value; } } From 8c906af56303f3125ac3b69b54059e87f1203ae1 Mon Sep 17 00:00:00 2001 From: Tiutiu Marius Cristian Date: Mon, 22 Jan 2018 12:42:23 +0200 Subject: [PATCH 11/29] make WIFI_MANAGER_MAX_PARAMS customizable --- WiFiManager.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/WiFiManager.h b/WiFiManager.h index 5b37e44..3da6da4 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -35,7 +35,9 @@ const char HTTP_SCAN_LINK[] PROGMEM = "
Date: Tue, 30 Jan 2018 13:47:13 -0600 Subject: [PATCH 12/29] clean up parameters --- WiFiManager.cpp | 21 ++++++++------------- WiFiManager.h | 6 ++++-- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 00af662..cabc7b5 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -36,6 +36,10 @@ WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placehold } void WiFiManagerParameter::init(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom, int labelPlacement) { + + _max_params = WIFI_MANAGER_MAX_PARAMS; + _params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); + _id = id; _placeholder = placeholder; _length = length; @@ -55,6 +59,10 @@ WiFiManagerParameter::~WiFiManagerParameter() { if (_value != NULL) { delete[] _value; } + if (_params != NULL){ + DEBUG_WM(F("freeing allocated params!")); + free(_params); + } } const char* WiFiManagerParameter::getValue() { @@ -76,7 +84,6 @@ const char* WiFiManagerParameter::getCustomHTML() { return _customHTML; } - bool WiFiManager::addParameter(WiFiManagerParameter *p) { if(_paramsCount + 1 > _max_params){ // rezise the params array @@ -107,24 +114,12 @@ WiFiManager::WiFiManager():_debugPort(Serial) { if(_debug) debugPlatformInfo(); _usermode = WiFi.getMode(); WiFi.persistent(false); // disable persistent so scannetworks and mode switching do not cause overwrites - - // parameters - _max_params = WIFI_MANAGER_MAX_PARAMS; - _params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); - } // destructor WiFiManager::~WiFiManager() { if(_userpersistent) WiFi.persistent(true); // reenable persistent, there is no getter we rely on _userpersistent WiFi.mode(_usermode); - - // parameters - if (_params != NULL){ - DEBUG_WM(F("freeing allocated params!")); - free(_params); - } - DEBUG_WM(F("unloading")); } diff --git a/WiFiManager.h b/WiFiManager.h index 15ae705..40977e3 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -201,7 +201,6 @@ class WiFiManager boolean _sta_show_static_fields = false; // option parameters - int _paramsCount = 0; int _minimumQuality = -1; boolean _removeDuplicateAPs = true; boolean _shouldBreakAfterConfig = false; @@ -271,7 +270,10 @@ class WiFiManager void (*_savecallback)(void) = NULL; Stream& _debugPort; // debug output stream ref - int _max_params; + + //WiFiManagerParameter + int _paramsCount = 0; + int _max_params; WiFiManagerParameter** _params; template From c8d89db529f4f59b5f369666f71c93ef15ec9bae Mon Sep 17 00:00:00 2001 From: Shawn A Date: Tue, 30 Jan 2018 14:00:39 -0600 Subject: [PATCH 13/29] oops --- WiFiManager.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index cabc7b5..28d77b2 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -36,10 +36,6 @@ WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placehold } void WiFiManagerParameter::init(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom, int labelPlacement) { - - _max_params = WIFI_MANAGER_MAX_PARAMS; - _params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); - _id = id; _placeholder = placeholder; _length = length; @@ -59,10 +55,6 @@ WiFiManagerParameter::~WiFiManagerParameter() { if (_value != NULL) { delete[] _value; } - if (_params != NULL){ - DEBUG_WM(F("freeing allocated params!")); - free(_params); - } } const char* WiFiManagerParameter::getValue() { @@ -114,12 +106,22 @@ WiFiManager::WiFiManager():_debugPort(Serial) { if(_debug) debugPlatformInfo(); _usermode = WiFi.getMode(); WiFi.persistent(false); // disable persistent so scannetworks and mode switching do not cause overwrites + + //parameters + _max_params = WIFI_MANAGER_MAX_PARAMS; + _params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); } // destructor WiFiManager::~WiFiManager() { if(_userpersistent) WiFi.persistent(true); // reenable persistent, there is no getter we rely on _userpersistent WiFi.mode(_usermode); + + // parameters + if (_params != NULL){ + DEBUG_WM(F("freeing allocated params!")); + free(_params); + } DEBUG_WM(F("unloading")); } From 8697cab0ae9d1402904a781edc2ab680e4de9fe0 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Tue, 30 Jan 2018 14:02:01 -0600 Subject: [PATCH 14/29] clean up .h file --- WiFiManager.h | 83 ++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/WiFiManager.h b/WiFiManager.h index 40977e3..ebe9410 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -167,38 +167,26 @@ class WiFiManager std::unique_ptr dnsServer; std::unique_ptr server; - //const int WM_DONE = 0; - //const int WM_WAIT = 10; - - //const String HTTP_HEAD = "{v}"; - - void setupConfigPortal(); - void startWPS(); - bool startAP(); - - // typedef enum WiFiMode - // { - // WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 - // } WiFiMode_t; - - String _apName = "no-net"; - String _apPassword = ""; - String _ssid = ""; - String _pass = ""; - unsigned long _configPortalTimeout = 0; - unsigned long _connectTimeout = 0; - unsigned long _configPortalStart = 0; - boolean _userpersistent = true; - WiFiMode_t _usermode = WIFI_OFF; - boolean _enableCaptivePortal = true; - + // ip configs IPAddress _ap_static_ip; IPAddress _ap_static_gw; IPAddress _ap_static_sn; IPAddress _sta_static_ip; IPAddress _sta_static_gw; IPAddress _sta_static_sn; - boolean _sta_show_static_fields = false; + + // defaults + const byte DNS_PORT = 53; + String _apName = "no-net"; + String _apPassword = ""; + String _ssid = ""; + String _pass = ""; + + // options flags + unsigned long _configPortalTimeout = 0; + unsigned long _connectTimeout = 0; + unsigned long _configPortalStart = 0; + WiFiMode_t _usermode = WIFI_OFF; // option parameters int _minimumQuality = -1; @@ -206,18 +194,22 @@ class WiFiManager boolean _shouldBreakAfterConfig = false; boolean _tryWPS = false; boolean _configPortalIsBlocking = true; + boolean _sta_show_static_fields = false; + boolean _enableCaptivePortal = true; + boolean _userpersistent = true; const char* _customHeadElement = ""; - //String getEEPROMString(int start, int len); - //void setEEPROMString(int start, int len, String string); + void setupConfigPortal(); + void startWPS(); + bool startAP(); int status = WL_IDLE_STATUS; int connectWifi(String ssid, String pass); uint8_t waitForConnectResult(); uint8_t waitForConnectResult(uint16_t timeout); - String getWLStatusString(uint8_t status); + // webserver handlers void handleRoot(); void handleWifi(boolean scan); void handleWifiSave(); @@ -231,6 +223,8 @@ class WiFiManager boolean configPortalHasTimeout(); boolean stopConfigPortal(); uint8_t handleConfigPortal(); + + // wifi platform abstractions bool WiFi_Mode(WiFiMode_t m); bool WiFi_Mode(WiFiMode_t m,bool persistent); bool WiFi_Disconnect(); @@ -239,14 +233,13 @@ class WiFiManager bool WiFi_eraseConfig(); void debugSoftAPConfig(); + // output helpers String getParamOut(); String getScanItemOut(); String getStaticOut(); - // DNS server - const byte DNS_PORT = 53; - //helpers + String getWLStatusString(uint8_t status); int getRSSIasQuality(int RSSI); boolean isIp(String str); String toStringIp(IPAddress ip); @@ -255,31 +248,33 @@ class WiFiManager void reportStatus(String &page); void debugPlatformInfo(); + // flags boolean connect; boolean abort; - boolean reset = false; + boolean reset = false; boolean configPortalActive = false; boolean webPortalActive = false; - boolean _debug = true; boolean portalTimeoutResult = false; boolean portalAbortResult = false; boolean storeSTAmode = true; // option store persistent STA mode in connectwifi int timer = 0; - - void (*_apcallback)(WiFiManager*) = NULL; - void (*_savecallback)(void) = NULL; - - Stream& _debugPort; // debug output stream ref - //WiFiManagerParameter - int _paramsCount = 0; - int _max_params; + // WiFiManagerParameter + int _paramsCount = 0; + int _max_params; WiFiManagerParameter** _params; + // debugging + boolean _debug = true; + Stream& _debugPort; // debug output stream ref template - void DEBUG_WM(Generic text); + void DEBUG_WM(Generic text); template - void DEBUG_WM(Generic text,Genericb textb); + void DEBUG_WM(Generic text,Genericb textb); + + // callbacks + void (*_apcallback)(WiFiManager*) = NULL; + void (*_savecallback)(void) = NULL; template auto optionalIPFromString(T *obj, const char *s) -> decltype( obj->fromString(s) ) { From fa04972cb137a0d7016182b2aa683be85b670de2 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Tue, 30 Jan 2018 14:07:21 -0600 Subject: [PATCH 15/29] fix tabs --- WiFiManager.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WiFiManager.h b/WiFiManager.h index ebe9410..ac5cd21 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -260,13 +260,13 @@ class WiFiManager int timer = 0; // WiFiManagerParameter - int _paramsCount = 0; - int _max_params; + int _paramsCount = 0; + int _max_params; WiFiManagerParameter** _params; // debugging boolean _debug = true; - Stream& _debugPort; // debug output stream ref + Stream& _debugPort; // debug output stream ref template void DEBUG_WM(Generic text); template From 5f78d38f461832c0d2a2f01abc49d595f6010702 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Wed, 31 Jan 2018 09:30:37 -0600 Subject: [PATCH 16/29] fix up parameters --- WiFiManager.cpp | 10 ++++++---- WiFiManager.h | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 28d77b2..a0bed58 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -77,12 +77,14 @@ const char* WiFiManagerParameter::getCustomHTML() { } bool WiFiManager::addParameter(WiFiManagerParameter *p) { - if(_paramsCount + 1 > _max_params){ - // rezise the params array + if(_paramsCount + 1 == _max_params){ + // resize the params array by increment of WIFI_MANAGER_MAX_PARAMS _max_params += WIFI_MANAGER_MAX_PARAMS; - DEBUG_WM(F("Increasing _max_params to:")); - DEBUG_WM(_max_params); + DEBUG_WM(F("Updated _max_params:"),_max_params); WiFiManagerParameter** new_params = (WiFiManagerParameter**)realloc(_params, _max_params * sizeof(WiFiManagerParameter*)); + // DEBUG_WM(WIFI_MANAGER_MAX_PARAMS); + // DEBUG_WM(_paramsCount); + // DEBUG_WM(_max_params); if (new_params != NULL) { _params = new_params; } else { diff --git a/WiFiManager.h b/WiFiManager.h index ac5cd21..0f7839c 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -66,7 +66,7 @@ const char HTTP_SCAN_LINK[] PROGMEM = "
Saving Credentials
Trying to connect ESP to network.
If it fails reconnect to AP to try again
"; const char HTTP_END[] PROGMEM = ""; -#define WIFI_MANAGER_MAX_PARAMS 10 +#define WIFI_MANAGER_MAX_PARAMS 5 #define WFM_LABEL_BEFORE 1 #define WFM_LABEL_AFTER 2 From e015efe8d0bbb10571f56ab41ad51039909e84c4 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Wed, 31 Jan 2018 09:30:46 -0600 Subject: [PATCH 17/29] fixup debugging --- WiFiManager.cpp | 63 ++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index a0bed58..35e824e 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -92,10 +92,10 @@ bool WiFiManager::addParameter(WiFiManagerParameter *p) { return false; } } + _params[_paramsCount] = p; _paramsCount++; - DEBUG_WM("Adding parameter"); - DEBUG_WM(p->getID()); + DEBUG_WM("Added Parameter:",p->getID()); return true; } @@ -134,7 +134,6 @@ boolean WiFiManager::autoConnect() { } boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { - DEBUG_WM(F("")); DEBUG_WM(F("AutoConnect")); // attempt to connect using saved settings, on fail fallback to AP config portal @@ -143,8 +142,7 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { // if already connected, or try stored connect if (WiFi.status() == WL_CONNECTED || connectWifi("", "") == WL_CONNECTED) { //connected - DEBUG_WM(F("IP Address:")); - DEBUG_WM(WiFi.localIP()); + DEBUG_WM(F("IP Address:"),WiFi.localIP()); return true; } // not connected start configportal @@ -153,12 +151,11 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { // CONFIG PORTAL bool WiFiManager::startAP(){ - DEBUG_WM(F("Configuring access point with with SSID of ... ")); - DEBUG_WM(_apName); + DEBUG_WM(F("StartAP with SSID of"),_apName); // setup optional soft AP static ip config if (_ap_static_ip) { - DEBUG_WM(F("Custom AP IP/GW/Subnet")); + DEBUG_WM(F("Custom AP IP/GW/Subnet:")); WiFi.softAPConfig(_ap_static_ip, _ap_static_gw, _ap_static_sn); } @@ -166,7 +163,6 @@ bool WiFiManager::startAP(){ // start soft AP with password or anonymous if (_apPassword != "") { - Serial.println((String)_apPassword.c_str()); ret = WiFi.softAP(_apName.c_str(), _apPassword.c_str());//password option } else { DEBUG_WM(F("AP has anonymous access")); @@ -178,8 +174,7 @@ bool WiFiManager::startAP(){ if(!ret) DEBUG_WM("There was an error starting the AP"); // @bug startAP returns unreliable success status delay(500); // slight delay to make sure we get an AP IP - DEBUG_WM(F("AP IP address: ")); - DEBUG_WM(WiFi.softAPIP()); + DEBUG_WM(F("AP IP address:"),WiFi.softAPIP()); // do AP callback if set if ( _apcallback != NULL) { @@ -417,9 +412,8 @@ int WiFiManager::connectWifi(String ssid, String pass) { // Setup static IP config if provided if (_sta_static_ip) { - DEBUG_WM(F("Custom STA IP/GW/Subnet")); WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn); - DEBUG_WM(WiFi.localIP()); + DEBUG_WM(F("Custom STA IP/GW/Subnet:"),WiFi.localIP()); } // make sure sta is on before `begin` so it does not call enablesta->mode while persistent is ON ( which would save WM AP state to eeprom !) @@ -446,8 +440,7 @@ int WiFiManager::connectWifi(String ssid, String pass) { } uint8_t connRes = waitforconx ? waitForConnectResult() : WL_NO_SSID_AVAIL; - DEBUG_WM ("Connection result: "); - DEBUG_WM ( getWLStatusString(connRes) ); + DEBUG_WM ("Connection result:",getWLStatusString(connRes)); // do WPS, if WPS options enabled and not connected and no password was supplied // @todo this seems like wrong place for this, is it a fallback or option? @@ -655,7 +648,7 @@ String WiFiManager::getScanItemOut(){ cssid = WiFi.SSID(indices[i]); for (int j = i + 1; j < n; j++) { if (cssid == WiFi.SSID(indices[j])) { - DEBUG_WM("DUP AP: " + WiFi.SSID(indices[j])); + DEBUG_WM("DUP AP:",WiFi.SSID(indices[j])); indices[j] = -1; // set dup aps to index -1 } } @@ -838,37 +831,37 @@ void WiFiManager::handleWifiSave() { _pass = server->arg("p").c_str(); //parameters - for (int i = 0; i < _paramsCount; i++) { - if (_params[i] == NULL) { - break; + if(_paramsCount > 0){ + DEBUG_WM("Parameters"); + DEBUG_WM("-----------"); + for (int i = 0; i < _paramsCount; i++) { + if (_params[i] == NULL) { + break; + } + //read parameter from server + String value = server->arg(_params[i]->getID()).c_str(); + //store it in array + value.toCharArray(_params[i]->_value, _params[i]->_length+1); // length+1 null terminated + DEBUG_WM((String)_params[i]->getID() + ":",value); } - //read parameter - String value = server->arg(_params[i]->getID()).c_str(); - //store it in array - value.toCharArray(_params[i]->_value, _params[i]->_length+1); // length+1 null terminated - DEBUG_WM(F("Parameter")); - DEBUG_WM(_params[i]->getID()); - DEBUG_WM(value); + DEBUG_WM("-----------"); } if (server->arg("ip") != "") { - DEBUG_WM(F("static ip")); - DEBUG_WM(server->arg("ip")); //_sta_static_ip.fromString(server->arg("ip")); String ip = server->arg("ip"); optionalIPFromString(&_sta_static_ip, ip.c_str()); + DEBUG_WM(F("static ip:"),ip); } if (server->arg("gw") != "") { - DEBUG_WM(F("static gateway")); - DEBUG_WM(server->arg("gw")); String gw = server->arg("gw"); optionalIPFromString(&_sta_static_gw, gw.c_str()); + DEBUG_WM(F("static gateway:"),gw); } if (server->arg("sn") != "") { - DEBUG_WM(F("static netmask")); - DEBUG_WM(server->arg("sn")); String sn = server->arg("sn"); optionalIPFromString(&_sta_static_sn, sn.c_str()); + DEBUG_WM(F("static netmask:"),sn); } String page = FPSTR(HTTP_HEAD); @@ -1311,9 +1304,9 @@ void WiFiManager::debugSoftAPConfig(){ void WiFiManager::debugPlatformInfo(){ system_print_meminfo(); - DEBUG_WM("getCoreVersion(): ",ESP.getCoreVersion()); - DEBUG_WM("system_get_sdk_version(): ",system_get_sdk_version()); - DEBUG_WM("system_get_boot_version(): ",system_get_boot_version()); + DEBUG_WM("getCoreVersion():",ESP.getCoreVersion()); + DEBUG_WM("system_get_sdk_version():",system_get_sdk_version()); + DEBUG_WM("system_get_boot_version():",system_get_boot_version()); } int WiFiManager::getRSSIasQuality(int RSSI) { From e5c263c4146d8b1f4f85ac1e73ca293709815f41 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Wed, 31 Jan 2018 12:23:25 -0600 Subject: [PATCH 18/29] fixes paramid issues paramid must be alphanumeric and adds {I} token support to use ids as input names if we make that optional. --- WiFiManager.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 35e824e..bcc50ee 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -77,6 +77,15 @@ const char* WiFiManagerParameter::getCustomHTML() { } bool WiFiManager::addParameter(WiFiManagerParameter *p) { + + // check param id is valid + for (int i = 0; i < strlen(p->getID()); i++){ + if(!isAlphaNumeric(p->getID()[i])){ + DEBUG_WM("[ERROR] parameter IDs can only contain aplha numberic chars"); + return false; + } + } + if(_paramsCount + 1 == _max_params){ // resize the params array by increment of WIFI_MANAGER_MAX_PARAMS _max_params += WIFI_MANAGER_MAX_PARAMS; @@ -92,7 +101,6 @@ bool WiFiManager::addParameter(WiFiManagerParameter *p) { return false; } } - _params[_paramsCount] = p; _paramsCount++; DEBUG_WM("Added Parameter:",p->getID()); @@ -762,6 +770,7 @@ String WiFiManager::getParamOut(){ String HTTP_PARAM_temp = FPSTR(HTTP_FORM_LABEL); HTTP_PARAM_temp += FPSTR(HTTP_FORM_PARAM); + bool tok_I = HTTP_PARAM_temp.indexOf("{I}") > 0; bool tok_i = HTTP_PARAM_temp.indexOf("{i}") > 0; bool tok_n = HTTP_PARAM_temp.indexOf("{n}") > 0; bool tok_p = HTTP_PARAM_temp.indexOf("{p}") > 0; @@ -798,6 +807,7 @@ String WiFiManager::getParamOut(){ // if no ID use customhtml for item, else generate from param string if (_params[i]->getID() != NULL) { + if(tok_I)pitem.replace("{I}", "param_"+(String)i); if(tok_i)pitem.replace("{i}", _params[i]->getID()); if(tok_n)pitem.replace("{n}", _params[i]->getID()); if(tok_p)pitem.replace("{p}", "{t}"); @@ -839,7 +849,10 @@ void WiFiManager::handleWifiSave() { break; } //read parameter from server - String value = server->arg(_params[i]->getID()).c_str(); + String value; + if(server->arg("param_"+(String)i) != NULL) value = server->arg("param_"+(String)i).c_str(); + else value = server->arg(_params[i]->getID()).c_str(); + //store it in array value.toCharArray(_params[i]->_value, _params[i]->_length+1); // length+1 null terminated DEBUG_WM((String)_params[i]->getID() + ":",value); From 53c8addfdc04e46b491ee35a20222c05c04c86fd Mon Sep 17 00:00:00 2001 From: Shawn A Date: Wed, 31 Jan 2018 12:48:11 -0600 Subject: [PATCH 19/29] debugging clean --- WiFiManager.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index bcc50ee..cae66ca 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -97,7 +97,7 @@ bool WiFiManager::addParameter(WiFiManagerParameter *p) { if (new_params != NULL) { _params = new_params; } else { - DEBUG_WM("ERROR: failed to realloc params, size not increased!"); + DEBUG_WM("[ERROR] failed to realloc params, size not increased!"); return false; } } @@ -179,7 +179,7 @@ bool WiFiManager::startAP(){ debugSoftAPConfig(); - if(!ret) DEBUG_WM("There was an error starting the AP"); // @bug startAP returns unreliable success status + if(!ret) DEBUG_WM("[ERROR] There was a problem starting the AP"); // @bug startAP returns unreliable success status delay(500); // slight delay to make sure we get an AP IP DEBUG_WM(F("AP IP address:"),WiFi.softAPIP()); @@ -834,7 +834,10 @@ String WiFiManager::getParamOut(){ * HTTPD CALLBACK save form and redirect to WLAN config page again */ void WiFiManager::handleWifiSave() { - DEBUG_WM(F("HTTP WiFi save")); + DEBUG_WM(F("<- HTTP WiFi save ")); + + DEBUG_WM(server->method()); + DEBUG_WM(F("Method:"),server->method() == HTTP_GET ? "GET" : "POST"); //SAVE/connect here _ssid = server->arg("s").c_str(); @@ -898,7 +901,7 @@ void WiFiManager::handleWifiSave() { * HTTPD CALLBACK info page */ void WiFiManager::handleInfo() { - DEBUG_WM(F("HTTP Info")); + DEBUG_WM(F("<- HTTP Info")); String page = FPSTR(HTTP_HEAD); page.replace("{v}", "Info"); @@ -1149,22 +1152,19 @@ void WiFiManager::handleErase() { page += FPSTR(HTTP_HEAD_END); bool ret = WiFi_eraseConfig(); - if(!ret) DEBUG_WM(F("ERASE FAILED")); - - // bool ret = reset = true; - + if(ret) page += F("Module will reset in a few seconds."); else { page += F("An Error Occured"); - DEBUG_WM(F("WiFi EraseConfig reported an error")); + DEBUG_WM(F("[ERROR] WiFi EraseConfig failed")); } page += FPSTR(HTTP_END); server->sendHeader("Content-Length", String(page.length())); server->send(200, "text/html", page); - delay(2000); if(ret){ + delay(2000); DEBUG_WM(F("RESETTING ESP")); reboot(); } From ccfddefaa138aed0e8a9012abe1d16ac6e6583a2 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Wed, 31 Jan 2018 12:57:49 -0600 Subject: [PATCH 20/29] oops --- WiFiManager.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index cae66ca..9bb0eab 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -836,7 +836,6 @@ String WiFiManager::getParamOut(){ void WiFiManager::handleWifiSave() { DEBUG_WM(F("<- HTTP WiFi save ")); - DEBUG_WM(server->method()); DEBUG_WM(F("Method:"),server->method() == HTTP_GET ? "GET" : "POST"); //SAVE/connect here From 19d533b7d5ceaaf62a27996a6b41acaf42211308 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Wed, 31 Jan 2018 13:07:34 -0600 Subject: [PATCH 21/29] adds exit to readme --- README.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 10da7f6..4c2adcf 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,12 @@ void loop() { ``` See example for a more complex version. [OnDemandConfigPortal](https://github.com/tzapu/WiFiManager/tree/master/examples/OnDemandConfigPortal) +#### Exiting from the Configuration Portal +Normally, once entered, the configuration portal will continue to loop until WiFi credentials have been successfully entered or a timeout is reached. +If you'd prefer to exit without joining a WiFi network, say becuase you're going to put the ESP into AP mode, then press the "Exit" button +on the main webpage. +If started via `autoConnect` or `startConfigPortal` then it will return `false (portalAbortResult)` + #### Custom Parameters You can use WiFiManager to collect more parameters than just SSID and password. This could be helpful for configuring stuff like MQTT host and port, [blynk](http://www.blynk.cc) or [emoncms](http://emoncms.org) tokens, just to name a few. @@ -189,18 +195,18 @@ This could be helpful for configuring stuff like MQTT host and port, [blynk](htt Usage scenario would be: - load values from somewhere (EEPROM/FS) or generate some defaults - add the custom parameters to WiFiManager using - ```cpp +```cpp // id/name, placeholder/prompt, default, length WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40); wifiManager.addParameter(&custom_mqtt_server); - ``` +``` - if connection to AP fails, configuration portal starts and you can set /change the values (or use on demand configuration portal) - once configuration is done and connection is established [save config callback]() is called - once WiFiManager returns control to your application, read and save the new values using the `WiFiManagerParameter` object. - ```cpp +```cpp mqtt_server = custom_mqtt_server.getValue(); - ``` +``` This feature is a lot more involved than all the others, so here are some examples to fully show how it is done. You should also take a look at adding custom HTML to your form. From a21f8cbc39b9926b04663b4bf6a3568db922c52c Mon Sep 17 00:00:00 2001 From: Shawn A Date: Wed, 31 Jan 2018 13:54:54 -0600 Subject: [PATCH 22/29] moved http strings to include, added token flags --- WiFiManager.cpp | 105 ++++++++++++++++++------------------------------ WiFiManager.h | 38 +----------------- strings_en.h | 56 ++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 102 deletions(-) create mode 100644 strings_en.h diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 9bb0eab..4af51ea 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -11,6 +11,7 @@ **************************************************************/ #include "WiFiManager.h" +#include "strings_en.h" // PARAMETERS @@ -585,7 +586,7 @@ void WiFiManager::handleWifi(boolean scan) { DEBUG_WM("<- HTTP Wifi"); String page = FPSTR(HTTP_HEAD); - page.replace("{v}", "Config ESP"); + page.replace("{v}", "Config ESP"); // @token titlewifi page += FPSTR(HTTP_SCRIPT); page += FPSTR(HTTP_STYLE); page += _customHeadElement; @@ -623,7 +624,7 @@ String WiFiManager::getScanItemOut(){ DEBUG_WM(F("Scan done")); if (n == 0) { DEBUG_WM(F("No networks found")); - page += F("No networks found. Refresh to scan again."); + page += F("No networks found. Refresh to scan again."); // @token scannotfound } else { //sort networks @@ -720,7 +721,7 @@ String WiFiManager::getStaticOut(){ item.replace("{i}", "ip"); item.replace("{n}", "ip"); item.replace("{p}", "{t}"); - item.replace("{t}", "Static IP"); + item.replace("{t}", "Static IP"); // @token staticip item.replace("{l}", "15"); item.replace("{v}", (_sta_static_ip ? _sta_static_ip.toString() : "")); item.replace("{c}", ""); @@ -735,7 +736,7 @@ String WiFiManager::getStaticOut(){ item.replace("{i}", "gw"); item.replace("{n}", "gw"); item.replace("{p}", "{t}"); - item.replace("{t}", "Static Gateway"); + item.replace("{t}", "Static Gateway"); // @token staticgateway item.replace("{l}", "15"); item.replace("{v}", (_sta_static_gw ? _sta_static_gw.toString() : "")); item.replace("{c}", ""); @@ -750,7 +751,7 @@ String WiFiManager::getStaticOut(){ item.replace("{i}", "sn"); item.replace("{n}", "sn"); item.replace("{p}", "{t}"); - item.replace("{t}", "Subnet"); + item.replace("{t}", "Subnet"); // @token subnet item.replace("{l}", "15"); item.replace("{v}", (_sta_static_sn ? _sta_static_sn.toString() : "")); item.replace("{c}", ""); @@ -880,7 +881,7 @@ void WiFiManager::handleWifiSave() { } String page = FPSTR(HTTP_HEAD); - page.replace("{v}", "Credentials Saved"); + page.replace("{v}", "Credentials Saved"); // @token titlewifisaved page += FPSTR(HTTP_SCRIPT); page += FPSTR(HTTP_STYLE); page += _customHeadElement; @@ -903,7 +904,7 @@ void WiFiManager::handleInfo() { DEBUG_WM(F("<- HTTP Info")); String page = FPSTR(HTTP_HEAD); - page.replace("{v}", "Info"); + page.replace("{v}", "Info"); // @token titleinfo page += FPSTR(HTTP_SCRIPT); page += FPSTR(HTTP_STYLE); page += _customHeadElement; @@ -916,39 +917,39 @@ void WiFiManager::handleInfo() { page += F("

esp8266


"); // subject to rollover! - page += F("
Uptime
"); - page += (String)(system_get_time() / 1000000 / 60) + " mins "; - page += (String)((system_get_time() / 1000000)%60) + " secs"; + page += F("
Uptime
"); // @token uptime + page += (String)(system_get_time() / 1000000 / 60) + " mins "; // @token mins + page += (String)((system_get_time() / 1000000)%60) + " secs"; // @token secs - page += F("
Chip ID
"); + page += F("
Chip ID
"); // @token chipid page += WIFI_getChipId(); page += F("
"); - page += F("
Flash Chip ID
"); + page += F("
Flash Chip ID
"); // @token flashid page += ESP.getFlashChipId(); page += F("
"); - page += F("
IDE Flash Size
"); + page += F("
IDE Flash Size
"); // @token flashsize page += ESP.getFlashChipSize(); page += F(" bytes
"); - page += F("
Actual Flash Size
"); + page += F("
Actual Flash Size
"); // @token actualflashsize page += ESP.getFlashChipRealSize(); page += F(" bytes
"); - page += F("
SDK Version
"); + page += F("
SDK Version
"); // @token sdkver page += system_get_sdk_version(); page += F("
"); - page += F("
Core Version
"); + page += F("
Core Version
"); // @token corever page += ESP.getCoreVersion(); page += F("
"); - page += F("
Boot Version
"); + page += F("
Boot Version
"); // @token boot ver page += system_get_boot_version(); page += F("
"); - page += F("
CPU Frequency
"); + page += F("
CPU Frequency
"); // @token cpufreq page += ESP.getCpuFreqMHz(); page += F("MHz
"); @@ -962,12 +963,12 @@ void WiFiManager::handleInfo() { // page += "
"; // page += F(""); - page += F("
Memory - Free Heap
"); + page += F("
Memory - Free Heap
"); // @token freeheap page += ESP.getFreeHeap(); - page += F(" bytes available
"); + page += F(" bytes available"); // @token bytesavail - page += F("
Memory - Sketch Size
"); - page += F("Used/Total bytes
"); + page += F("
Memory - Sketch Size
"); // @token sketchsize + page += F("Used/Total bytes
"); // @token usedtotal page += (ESP.getSketchSize()+ESP.getFreeSketchSpace())-ESP.getFreeSketchSpace(); page += "/"; page += (ESP.getSketchSize()+ESP.getFreeSketchSpace()); @@ -977,15 +978,15 @@ void WiFiManager::handleInfo() { page += (String)(ESP.getSketchSize()+ESP.getFreeSketchSpace()); page += F("'>
"); - page += F("
Last reset reason
"); + page += F("
Last reset reason
"); // @token resetreason page += ESP.getResetReason(); page += F("
"); - page += F("

WiFi


Access Point IP
"); + page += F("

WiFi


Access Point IP
"); // @token apip page += WiFi.softAPIP().toString(); page += F("
"); - page += F("
Access Point MAC
"); + page += F("
Access Point MAC
"); // @token apmac page += WiFi.softAPmacAddress(); page += F("
"); @@ -997,71 +998,45 @@ void WiFiManager::handleInfo() { page += WiFi.BSSIDstr(); page += F(""); - page += F("
Station IP
"); + page += F("
Station IP
"); // @token staip page += WiFi.localIP().toString(); page += F("
"); - page += F("
Station Gateway
"); + page += F("
Station Gateway
"); // @token stagw page += WiFi.gatewayIP().toString(); page += F("
"); - page += F("
Station Subnet
"); + page += F("
Station Subnet
"); // @token stasub page += WiFi.subnetMask().toString(); page += F("
"); - page += F("
DNS Server
"); + page += F("
DNS Server
"); // @token dnss page += WiFi.dnsIP().toString(); page += F("
"); - page += F("
Hostname
"); + page += F("
Hostname
"); // @token hostname page += WiFi.hostname(); page += F("
"); - page += F("
Station MAC
"); + page += F("
Station MAC
"); // @stamac page += WiFi.macAddress(); page += F("
"); - page += F("
Connected
"); - page += WiFi.isConnected() ? "Yes" : "No"; + page += F("
Connected
"); // @token connected + page += WiFi.isConnected() ? "Yes" : "No"; //@token Y,N page += F("
"); page += F("
Autoconnect
"); - page += WiFi.getAutoConnect() ? "Enabled" : "Disabled"; + page += WiFi.getAutoConnect() ? "Enabled" : "Disabled"; // @token enabled,disabled page += F("
"); page += F("
"); - page += "
"; + page += "
"; + + page += FPSTR(HTTP_HELP); - page += F("

Available Pages


"); - page += F(""); - page += F(""); - - page += F(""); - page += F(""); - - page += F(""); - page += F(""); - - page += F(""); - page += F(""); - - page += F(""); - page += F(""); - - page += F(""); - page += F(""); - - page += F(""); - page += F(""); - - page += F(""); - page += F(""); - - page += F(""); - page += F(""); - // @todo add callback here - page += F("
PageFunction
/Menu page.
/wifiShow WiFi scan results and enter WiFi configuration.
/wifisaveSave WiFi configuration information and configure device. Needs variables supplied.
/closeClose the configuration server and configuration WiFi network.
/iInformation page
/exitExit Config Portal, configportal will close
/rReboot the device
/eraseErase WiFi configuration and reboot Device. Device will not reconnect to a network until new WiFi configuration data is entered.
"); - page += F("

More information about WiFiManager at https://github.com/tzapu/WiFiManager."); page += FPSTR(HTTP_END); server->sendHeader("Content-Length", String(page.length())); diff --git a/WiFiManager.h b/WiFiManager.h index a3e415b..4c0065a 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -30,43 +30,7 @@ #define WIFI_getChipId() ESP.getChipId() #define WIFI_AUTH_OPEN ENC_TYPE_NONE -const char HTTP_HEAD[] PROGMEM = "{v}"; -// const char HTTP_STYLE[] PROGMEM = ""; -const char HTTP_STYLE[] PROGMEM = ""; - -const char HTTP_SCRIPT[] PROGMEM = ""; -const char HTTP_HEAD_END[] PROGMEM = "

"; -const char HTTP_PORTAL_OPTIONS[] PROGMEM = "




"; -const char HTTP_ITEM[] PROGMEM = ""; -// const char HTTP_ITEM[] PROGMEM = "
{v} {R} {r}% {q} {e}
"; // test all tokens -//const char HTTP_ITEM_PADLOCK[] PROGMEM = ""; -const char HTTP_FORM_START[] PROGMEM = "


"; -const char HTTP_FORM_END[] PROGMEM = "
"; -const char HTTP_FORM_LABEL[] PROGMEM = ""; -const char HTTP_FORM_PARAM_START[] PROGMEM = "
"; -const char HTTP_FORM_PARAM[] PROGMEM = "
"; -const char HTTP_FORM_PARAM_END[] PROGMEM = "
"; -const char HTTP_SCAN_LINK[] PROGMEM = "
"; -const char HTTP_SAVED[] PROGMEM = "
Saving Credentials
Trying to connect ESP to network.
If it fails reconnect to AP to try again
"; -const char HTTP_END[] PROGMEM = "
"; - -#define WIFI_MANAGER_MAX_PARAMS 5 +#define WIFI_MANAGER_MAX_PARAMS 5 // params will autoincrement and realloc by this amount when max is reached #define WFM_LABEL_BEFORE 1 #define WFM_LABEL_AFTER 2 diff --git a/strings_en.h b/strings_en.h new file mode 100644 index 0000000..abda51c --- /dev/null +++ b/strings_en.h @@ -0,0 +1,56 @@ +const char HTTP_HEAD[] PROGMEM = "{v}"; +const char HTTP_SCRIPT[] PROGMEM = ""; +const char HTTP_HEAD_END[] PROGMEM = "
"; +const char HTTP_PORTAL_OPTIONS[] PROGMEM = "




"; +const char HTTP_ITEM[] PROGMEM = ""; +// const char HTTP_ITEM[] PROGMEM = "
{v} {R} {r}% {q} {e}
"; // test all tokens +const char HTTP_FORM_START[] PROGMEM = "


"; +const char HTTP_FORM_END[] PROGMEM = "
"; +const char HTTP_FORM_LABEL[] PROGMEM = ""; +const char HTTP_FORM_PARAM_START[] PROGMEM = "
"; +const char HTTP_FORM_PARAM[] PROGMEM = "
"; +const char HTTP_FORM_PARAM_END[] PROGMEM = "
"; +const char HTTP_SCAN_LINK[] PROGMEM = "
"; +const char HTTP_SAVED[] PROGMEM = "
Saving Credentials
Trying to connect ESP to network.
If it fails reconnect to AP to try again
"; +const char HTTP_END[] PROGMEM = "
"; + +const char HTTP_STYLE[] PROGMEM = ""; + +const char HTTP_HELP[] PROGMEM = + "

Available Pages


" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "
PageFunction
/Menu page.
/wifiShow WiFi scan results and enter WiFi configuration.(/0wifi noscan)
/wifisaveSave WiFi configuration information and configure device. Needs variables supplied.
/closeClose the configuration server and configuration WiFi network.
/iInformation page
/exitExit Config Portal, configportal will close
/rReboot the device
/eraseErase WiFi configuration and reboot Device. Device will not reconnect to a network until new WiFi configuration data is entered.
" + "

More information about WiFiManager at https://github.com/tzapu/WiFiManager."; \ No newline at end of file From 68762e1741b6c8d0b268b869e04a10bac7d6e145 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Wed, 31 Jan 2018 14:03:33 -0600 Subject: [PATCH 23/29] typo --- WiFiManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 4af51ea..95e3d7e 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -82,7 +82,7 @@ bool WiFiManager::addParameter(WiFiManagerParameter *p) { // check param id is valid for (int i = 0; i < strlen(p->getID()); i++){ if(!isAlphaNumeric(p->getID()[i])){ - DEBUG_WM("[ERROR] parameter IDs can only contain aplha numberic chars"); + DEBUG_WM("[ERROR] parameter IDs can only contain alpha numeric chars"); return false; } } From b0c61688d660f55dadc316ea7f43cf6d3061665f Mon Sep 17 00:00:00 2001 From: Shawn A Date: Wed, 31 Jan 2018 14:07:38 -0600 Subject: [PATCH 24/29] cleanup --- WiFiManager.cpp | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 95e3d7e..5620da1 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -713,7 +713,7 @@ String WiFiManager::getStaticOut(){ String page; if (_sta_show_static_fields || _sta_static_ip) { - // @todo how do we get these settings from memory , wifi_get_ip_info does not seem to reveal if struct ip_info is static or not + // @todo how can we get these settings from memory , wifi_get_ip_info does not seem to reveal if struct ip_info is static or not // @todo move titles to params for i18n String item = FPSTR(HTTP_FORM_LABEL); @@ -725,7 +725,8 @@ String WiFiManager::getStaticOut(){ item.replace("{l}", "15"); item.replace("{v}", (_sta_static_ip ? _sta_static_ip.toString() : "")); item.replace("{c}", ""); - + + // actuals as placeholder // IPAddress sta_ip = WiFi.localIP(); // item.replace("{p}", sta_ip.toString()); @@ -741,6 +742,7 @@ String WiFiManager::getStaticOut(){ item.replace("{v}", (_sta_static_gw ? _sta_static_gw.toString() : "")); item.replace("{c}", ""); + // actuals as placeholder // IPAddress sta_gateway = WiFi.gatewayIP(); // item.replace("{p}", sta_gateway.toString()); @@ -756,6 +758,7 @@ String WiFiManager::getStaticOut(){ item.replace("{v}", (_sta_static_sn ? _sta_static_sn.toString() : "")); item.replace("{c}", ""); + // actuals as placeholder // IPAddress sta_subnet = WiFi.subnetMask(); // item.replace("{p}", sta_subnet.toString()); page += item; @@ -1250,28 +1253,6 @@ void WiFiManager::DEBUG_WM(Generic text,Genericb textb) { } } -/* - String WiFiManager::getSSID() { - if (_ssid == "") { - DEBUG_WM(F("Reading SSID")); - _ssid = WiFi.SSID(); - DEBUG_WM(F("SSID: ")); - DEBUG_WM(_ssid); - } - return _ssid; - } - - String WiFiManager::getPassword() { - if (_pass == "") { - DEBUG_WM(F("Reading Password")); - _pass = WiFi.psk(); - DEBUG_WM("Password: " + _pass); - //DEBUG_WM(_pass); - } - return _pass; - } -*/ - void WiFiManager::debugSoftAPConfig(){ softap_config config; wifi_softap_get_config(&config); From 6894c89441daa8686a393dbbb30056dea3a79ec2 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Wed, 31 Jan 2018 16:37:53 -0600 Subject: [PATCH 25/29] moved all html strings out of code --- WiFiManager.cpp | 119 +++++++++++++++--------------------------------- WiFiManager.h | 1 + strings_en.h | 6 +++ 3 files changed, 44 insertions(+), 82 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 5620da1..a841b8b 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -547,6 +547,17 @@ void WiFiManager::setBreakAfterConfig(boolean shouldBreak) { _shouldBreakAfterConfig = shouldBreak; } +String WiFiManager::getHTTPHead(String title){ + String page; + page += FPSTR(HTTP_HEAD); + page.replace("{v}", title); + page += FPSTR(HTTP_SCRIPT); + page += FPSTR(HTTP_STYLE); + page += _customHeadElement; + page += FPSTR(HTTP_HEAD_END); + return page; +} + /** * HTTPD CALLBACK root or redirect to captive portal */ @@ -556,26 +567,17 @@ void WiFiManager::handleRoot() { if (captivePortal()) { // If captive portal redirect instead of displaying the page. return; } - - String page = FPSTR(HTTP_HEAD); - page.replace("{v}", "Options"); - page += FPSTR(HTTP_SCRIPT); - page += FPSTR(HTTP_STYLE); - page += _customHeadElement; - page += FPSTR(HTTP_HEAD_END); - page += "

"; - page += configPortalActive ? _apName : WiFi.localIP().toString(); - page += "

"; - page += F("

WiFiManager

"); + String page = getHTTPHead("options"); // @token options + String str = FPSTR(HTTP_ROOT_MAIN); + str.replace("{v}",configPortalActive ? _apName : WiFi.localIP().toString()); // use ip if ap is not active for heading + page += str; page += FPSTR(HTTP_PORTAL_OPTIONS); reportStatus(page); page += FPSTR(HTTP_END); - server->sendHeader("Content-Length", String(page.length())); server->send(200, "text/html", page); // server->close(); // testing reliability fix for content length mismatches during mutiple flood hits - } /** @@ -584,18 +586,10 @@ void WiFiManager::handleRoot() { void WiFiManager::handleWifi(boolean scan) { DEBUG_WM("<- HTTP Wifi"); - - String page = FPSTR(HTTP_HEAD); - page.replace("{v}", "Config ESP"); // @token titlewifi - page += FPSTR(HTTP_SCRIPT); - page += FPSTR(HTTP_STYLE); - page += _customHeadElement; - page += FPSTR(HTTP_HEAD_END); - + String page = getHTTPHead("Config ESP"); // @token titlewifi if (scan) { page += getScanItemOut(); } - String pitem = FPSTR(HTTP_FORM_START); pitem.replace("{v}", WiFi.SSID()); page += pitem; @@ -883,12 +877,7 @@ void WiFiManager::handleWifiSave() { DEBUG_WM(F("static netmask:"),sn); } - String page = FPSTR(HTTP_HEAD); - page.replace("{v}", "Credentials Saved"); // @token titlewifisaved - page += FPSTR(HTTP_SCRIPT); - page += FPSTR(HTTP_STYLE); - page += _customHeadElement; - page += FPSTR(HTTP_HEAD_END); + String page = getHTTPHead("Credentials Saved"); // @token titlewifisaved page += FPSTR(HTTP_SAVED); page += FPSTR(HTTP_END); @@ -905,14 +894,7 @@ void WiFiManager::handleWifiSave() { */ void WiFiManager::handleInfo() { DEBUG_WM(F("<- HTTP Info")); - - String page = FPSTR(HTTP_HEAD); - page.replace("{v}", "Info"); // @token titleinfo - page += FPSTR(HTTP_SCRIPT); - page += FPSTR(HTTP_STYLE); - page += _customHeadElement; - page += FPSTR(HTTP_HEAD_END); - + String page = getHTTPHead("Info"); // @token titleinfo reportStatus(page); // @todo add versioning here @@ -1033,13 +1015,8 @@ void WiFiManager::handleInfo() { page += WiFi.getAutoConnect() ? "Enabled" : "Disabled"; // @token enabled,disabled page += F(""); page += F(""); - - page += "
"; - + page += FPSTR(HTTP_ERASEBTN); page += FPSTR(HTTP_HELP); - page += FPSTR(HTTP_END); server->sendHeader("Content-Length", String(page.length())); @@ -1054,42 +1031,30 @@ void WiFiManager::handleInfo() { */ void WiFiManager::handleExit() { DEBUG_WM(F("<- HTTP Exit")); - - String page = FPSTR(HTTP_HEAD); - page.replace("{v}", "Exit"); - page += FPSTR(HTTP_SCRIPT); - page += FPSTR(HTTP_STYLE); - page += _customHeadElement; - page += FPSTR(HTTP_HEAD_END); - - page += "Exiting"; + String page = getHTTPHead("Exit"); // @token exit + page += "Exiting"; // @token exiting server->sendHeader("Content-Length", String(page.length())); server->send(200, "text/html", page); abort = true; } void WiFiManager::reportStatus(String &page){ - + String str; if (WiFi.SSID() != ""){ if (WiFi.status()==WL_CONNECTED){ - page += "
"; - page += F("Connected to "); - page += WiFi.SSID(); - page += F("
with IP "); - page += WiFi.localIP().toString(); - page += F(""); - } else { - page += "
"; - page += "Not Connected to "; - page += WiFi.SSID(); - } - } else { - page += "
"; - page += "No AP set"; + str = FPSTR(HTTP_STATUS_ON); + str.replace("{u}",WiFi.localIP().toString()); + str.replace("{s}",WiFi.SSID()); + } + else { + str = FPSTR(HTTP_STATUS_OFF); + str.replace("{s}",WiFi.SSID()); + } } - page += "
"; + else { + str = FPSTR(HTTP_STATUS_NONE); + } + page += str; } /** @@ -1098,12 +1063,7 @@ void WiFiManager::reportStatus(String &page){ void WiFiManager::handleReset() { DEBUG_WM(F("<- HTTP Reset")); - String page = FPSTR(HTTP_HEAD); - page.replace("{v}", "Reset"); - page += FPSTR(HTTP_SCRIPT); - page += FPSTR(HTTP_STYLE); - page += _customHeadElement; - page += FPSTR(HTTP_HEAD_END); + String page = getHTTPHead("Reset"); page += F("Module will reset in a few seconds."); page += FPSTR(HTTP_END); @@ -1121,15 +1081,10 @@ void WiFiManager::handleReset() { void WiFiManager::handleErase() { DEBUG_WM(F("<- HTTP Erase")); - String page = FPSTR(HTTP_HEAD); - page.replace("{v}", "Erase"); - page += FPSTR(HTTP_SCRIPT); - page += FPSTR(HTTP_STYLE); - page += _customHeadElement; + String page = getHTTPHead("Erase"); page += FPSTR(HTTP_HEAD_END); - bool ret = WiFi_eraseConfig(); - + if(ret) page += F("Module will reset in a few seconds."); else { page += F("An Error Occured"); diff --git a/WiFiManager.h b/WiFiManager.h index 4c0065a..d0b90be 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -205,6 +205,7 @@ class WiFiManager String getParamOut(); String getScanItemOut(); String getStaticOut(); + String getHTTPHead(String title); //helpers String getWLStatusString(uint8_t status); diff --git a/strings_en.h b/strings_en.h index abda51c..bf5f112 100644 --- a/strings_en.h +++ b/strings_en.h @@ -1,6 +1,7 @@ const char HTTP_HEAD[] PROGMEM = "{v}"; const char HTTP_SCRIPT[] PROGMEM = ""; const char HTTP_HEAD_END[] PROGMEM = "
"; +const char HTTP_ROOT_MAIN[] PROGMEM = "

{v}

WiFiManager

"; const char HTTP_PORTAL_OPTIONS[] PROGMEM = "




"; const char HTTP_ITEM[] PROGMEM = ""; // const char HTTP_ITEM[] PROGMEM = "
{v} {R} {r}% {q} {e}
"; // test all tokens @@ -13,6 +14,11 @@ const char HTTP_FORM_PARAM_END[] PROGMEM = "
"; const char HTTP_SCAN_LINK[] PROGMEM = "
"; const char HTTP_SAVED[] PROGMEM = "
Saving Credentials
Trying to connect ESP to network.
If it fails reconnect to AP to try again
"; const char HTTP_END[] PROGMEM = "
"; +const char HTTP_ERASEBTN[] PROGMEM = "
"; + +const char HTTP_STATUS_ON[] PROGMEM = "
Connected to {s}
with IP {u}
"; +const char HTTP_STATUS_OFF[] PROGMEM = "
Not Connected to {s}
"; +const char HTTP_STATUS_NONE[] PROGMEM = "
No AP set
"; const char HTTP_STYLE[] PROGMEM = "