From 7a57a1395dba8fbac0044d5e94713d5e1397f368 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Tue, 20 Mar 2018 12:00:59 -0500 Subject: [PATCH 1/7] change setmenu to take vector or array of strnig tokens --- WiFiManager.cpp | 45 ++++++++++++++++++++++++++----------------- WiFiManager.h | 51 +++++++++++++++++++++++++------------------------ 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index d6c93f2..7df48f6 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -152,7 +152,9 @@ WiFiManager::WiFiManager():WiFiManager(Serial) { } void WiFiManager::WiFiManagerInit(){ + setMenu(_menuIdsDefault); if(_debug && _debugLevel > DEBUG_DEV) debugPlatformInfo(); + _usermode = WiFi.getMode(); WiFi.persistent(false); // disable persistent so scannetworks and mode switching do not cause overwrites @@ -820,10 +822,8 @@ void WiFiManager::handleParam(){ String WiFiManager::getMenuOut(){ String page; - for(menu_page_t& v :_menuIds_v ){ - // DEBUG_WM("menuid:",(String)v); - // if(v == MENU_END) break; // MENU_END - if((v == MENU_PARAM) && (_paramsCount == 0)) continue; // no params set, omit params + for(auto& v :_menuIds ){ + if(((String)v == "param") && (_paramsCount == 0)) continue; // no params set, omit params page += HTTP_PORTAL_MENU[v]; } @@ -1913,22 +1913,33 @@ void WiFiManager::setShowInfoErase(boolean enabled){ * @since $dev * @param uint8_t menu[] array of menu ids */ -void WiFiManager::setMenu(menu_page_t menu[], uint8_t size){ - int i; - size_t n = size; - for(i=0;i= n) _menuIds[i] = MENU_END; - else _menuIds[i] = menu[i]; +void WiFiManager::setMenu(const char * menu[], uint8_t size){ + _menuIds.clear(); + for(size_t i = 0; i < size; i++){ + for(size_t j = 0; j < _menuids_cnt; j++){ + if(menu[i] == _menutokens[j]){ + if((String)menu[i] == "param") _paramsInWifi = false; // param auto flag + _menuIds.push_back(j); + } + } } - - // @todo copy into vector if we keep this.. + // DEBUG_WM(getMenuOut()); } -void WiFiManager::setMenu(std::vector& menu){ - _menuIds_v = menu; +void WiFiManager::setMenu(std::vector& menu){ + _menuIds.clear(); + for(auto v : menu ){ + for(size_t j = 0; j<_menuids_cnt; j++){ + if(v == _menutokens[j]){ + if((String)v == "param") _paramsInWifi = false; // param auto flag + _menuIds.push_back(j); + } + } + } + // DEBUG_WM(getMenuOut()); } + // GETTERS /** @@ -1988,12 +1999,12 @@ void WiFiManager::DEBUG_WM(wm_debuglevel_t level,Generic text,Genericb textb) { if(_debugLevel < level) return; if (_debug) { - if(_debugLevel > 3){ + if(_debugLevel >= DEBUG_MAX){ _debugPort.print("MEM: "); _debugPort.println((String)ESP.getFreeHeap()); } _debugPort.print("*WM: "); - // _debugPort.print("["+(String)level+"] "); + if(_debugLevel == DEBUG_DEV) _debugPort.print("["+(String)level+"] "); _debugPort.print(text); if(textb){ _debugPort.print(" "); diff --git a/WiFiManager.h b/WiFiManager.h index b9636d6..b1b2abf 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -128,19 +128,6 @@ class WiFiManager ~WiFiManager(); void WiFiManagerInit(); - typedef enum { - MENU_WIFI = 0, - MENU_WIFINOSCAN = 1, - MENU_INFO = 2, - MENU_PARAM = 3, - MENU_CLOSE = 4, - MENU_RESTART = 5, - MENU_EXIT = 6, - MENU_ERASE = 7, - MENU_SEP = 8, - MENU_END = 255 - } menu_page_t; - // auto connect to saved wifi, or custom, and start config portal on failures boolean autoConnect(); boolean autoConnect(char const *apName, char const *apPassword = NULL); @@ -223,8 +210,9 @@ class WiFiManager void setShowInfoErase(boolean enabled); // set custom menu - void setMenu(std::vector& menu); - void setMenu(menu_page_t menu[], uint8_t size); + // void setMenu(std::vector& menu); + void setMenu(std::vector& menu); + void setMenu(const char* menu[], uint8_t size); // get last connection result, includes autoconnect and wifisave uint8_t getLastConxResult(); @@ -247,16 +235,21 @@ class WiFiManager #endif std::unique_ptr server; - typedef enum { - DEBUG_ERROR = 0, - DEBUG_NOTIFY = 1, // default - DEBUG_VERBOSE = 2, - DEBUG_DEV = 3, - DEBUG_MAX = 4 - } wm_debuglevel_t; + int _menuids_cnt = 9; + const char * const _menutokens[9] PROGMEM = { + "wifi", + "wifinoscan", + "info", + "param", + "close", + "restart", + "exit", + "erase", + "sep" + }; - menu_page_t _menuIds[10] = {MENU_WIFI,MENU_INFO,MENU_EXIT,MENU_END}; - std::vector _menuIds_v = {MENU_WIFI,MENU_INFO,MENU_EXIT}; + std::vector _menuIds; + std::vector _menuIdsDefault = {"wifi","info","exit"}; // ip configs @todo struct ? IPAddress _ap_static_ip; @@ -402,8 +395,16 @@ class WiFiManager WiFiManagerParameter** _params = NULL; // debugging + typedef enum { + DEBUG_ERROR = 0, + DEBUG_NOTIFY = 1, // default + DEBUG_VERBOSE = 2, + DEBUG_DEV = 3, + DEBUG_MAX = 4 + } wm_debuglevel_t; + boolean _debug = true; - uint8_t _debugLevel = DEBUG_NOTIFY; + uint8_t _debugLevel = DEBUG_DEV; Stream& _debugPort; // debug output stream ref template From 88d2cfbff4a3e5c1c8f7d640980af57678aec5a7 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Tue, 20 Mar 2018 12:05:46 -0500 Subject: [PATCH 2/7] change varnames --- WiFiManager.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 7df48f6..2707f77 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -822,9 +822,9 @@ void WiFiManager::handleParam(){ String WiFiManager::getMenuOut(){ String page; - for(auto& v :_menuIds ){ - if(((String)v == "param") && (_paramsCount == 0)) continue; // no params set, omit params - page += HTTP_PORTAL_MENU[v]; + for(auto& menuId :_menuIds ){ + if(((String)menuId == "param") && (_paramsCount == 0)) continue; // no params set, omit params + page += HTTP_PORTAL_MENU[menuId]; } return page; @@ -1928,10 +1928,10 @@ void WiFiManager::setMenu(const char * menu[], uint8_t size){ void WiFiManager::setMenu(std::vector& menu){ _menuIds.clear(); - for(auto v : menu ){ + for(auto menuitem : menu ){ for(size_t j = 0; j<_menuids_cnt; j++){ - if(v == _menutokens[j]){ - if((String)v == "param") _paramsInWifi = false; // param auto flag + if(menuitem == _menutokens[j]){ + if((String)menuitem == "param") _paramsInWifi = false; // param auto flag _menuIds.push_back(j); } } From 941865ec85f60e172afd31a89692e5087f6936c1 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Tue, 20 Mar 2018 12:24:28 -0500 Subject: [PATCH 3/7] datatype fixes --- WiFiManager.cpp | 18 ++++++++++-------- WiFiManager.h | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 2707f77..2052120 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -99,7 +99,7 @@ bool WiFiManager::addParameter(WiFiManagerParameter *p) { // check param id is valid, unless null if(p->getID()){ - for (int i = 0; i < strlen(p->getID()); i++){ + for (size_t i = 0; i < strlen(p->getID()); i++){ if(!isAlphaNumeric(p->getID()[i])){ DEBUG_WM(DEBUG_ERROR,"[ERROR] parameter IDs can only contain alpha numeric chars"); return false; @@ -688,7 +688,7 @@ uint8_t WiFiManager::waitForConnectResult() { DEBUG_WM (F("connectTimeout set, waiting for connect....")); uint8_t status = WiFi.status(); - int timeout = millis() + _connectTimeout; + unsigned long timeout = millis() + _connectTimeout; while(millis() < timeout) { status = WiFi.status(); @@ -750,7 +750,7 @@ void WiFiManager::handleRoot() { server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length())); server->send(200, FPSTR(HTTP_HEAD_CT), page); // server->close(); // testing reliability fix for content length mismatches during mutiple flood hits WiFi_scanNetworks(); // preload wifiscan - if(_preloadwifiscan) WiFi_scanNetworks(10000); // preload wifiscan throttled + if(_preloadwifiscan) WiFi_scanNetworks((unsigned)10000); // preload wifiscan throttled } /** @@ -833,7 +833,7 @@ String WiFiManager::getMenuOut(){ bool WiFiManager::WiFi_scanNetworks(){ return WiFi_scanNetworks(false); } -bool WiFiManager::WiFi_scanNetworks(int cachetime){ +bool WiFiManager::WiFi_scanNetworks(unsigned int cachetime){ return WiFi_scanNetworks(millis()-_lastscan > cachetime); } bool WiFiManager::WiFi_scanNetworks(bool force){ @@ -1186,7 +1186,7 @@ void WiFiManager::handleInfo() { F("autoconx") }; - for(int i=0; i<27;i++){ + for(size_t i=0; i<27;i++){ if(infoids[i] != NULL) page += getInfoData(infoids[i]); } page += F(""); @@ -1224,7 +1224,7 @@ void WiFiManager::handleInfo() { F("autoconx") }; - for(int i=0; i<23;i++){ + for(size_t i=0; i<23;i++){ if(infoids[i] != NULL) page += getInfoData(infoids[i]); } page += F(""); @@ -1625,7 +1625,9 @@ bool WiFiManager::erase(bool opt){ err=nvs_flash_erase(); DEBUG_WM(DEBUG_VERBOSE,"nvs_flash_erase: ", err ? (String)err : "Success"); return err; - } + } + #else + (void)opt; #endif return WiFi_eraseConfig(); @@ -1883,7 +1885,7 @@ void WiFiManager::setWebPortalClientCheck(boolean enabled){ * @param boolean enabled [false] */ void WiFiManager::setScanDispPerc(boolean enabled){ - _scanDispOptions = true; + _scanDispOptions = enabled; } /** diff --git a/WiFiManager.h b/WiFiManager.h index b1b2abf..3b17420 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -356,7 +356,7 @@ class WiFiManager String WiFi_SSID(); bool WiFi_scanNetworks(); bool WiFi_scanNetworks(bool force); - bool WiFi_scanNetworks(int cachetime); + bool WiFi_scanNetworks(unsigned int cachetime); #ifdef ESP32 static void WiFiEvent(WiFiEvent_t event, system_event_info_t info); From 57fc15a20aeca2f2f80a7dabb4178c5f3c1c7592 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Tue, 20 Mar 2018 12:31:49 -0500 Subject: [PATCH 4/7] fix section compile error --- WiFiManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WiFiManager.h b/WiFiManager.h index 3b17420..bc7e0de 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -236,7 +236,7 @@ class WiFiManager std::unique_ptr server; int _menuids_cnt = 9; - const char * const _menutokens[9] PROGMEM = { + const char * const _menutokens[9] = { "wifi", "wifinoscan", "info", From 881f50fb7e3d2cbd1366bf61ae0c8ab978ca3052 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Tue, 20 Mar 2018 12:35:01 -0500 Subject: [PATCH 5/7] fix compiler warnings --- WiFiManager.cpp | 5 +++-- WiFiManager.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 2052120..7dc92c0 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -2169,6 +2169,7 @@ bool WiFiManager::WiFi_Disconnect() { return esp_wifi_disconnect() == ESP_OK; // return WiFi.disconnect(); #endif + return false; } // toggle STA without persistent @@ -2267,6 +2268,6 @@ void WiFiManager::WiFi_autoReconnect(){ if(_wifiAutoReconnect){ DEBUG_WM(DEBUG_VERBOSE,"ESP32 event handler enabled"); WiFi.onEvent(WiFiEvent); - } + } #endif -} +} \ No newline at end of file diff --git a/WiFiManager.h b/WiFiManager.h index bc7e0de..c0cacec 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -235,7 +235,7 @@ class WiFiManager #endif std::unique_ptr server; - int _menuids_cnt = 9; + uint8_t _menuids_cnt = 9; const char * const _menutokens[9] = { "wifi", "wifinoscan", From d090dd3dd7a0f77d91043221dd64eed206aee741 Mon Sep 17 00:00:00 2001 From: Shawn A Date: Tue, 20 Mar 2018 13:14:50 -0500 Subject: [PATCH 6/7] move menutokens --- WiFiManager.cpp | 4 ++-- WiFiManager.h | 13 ------------- strings_en.h | 13 +++++++++++++ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 7dc92c0..41207cd 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -1918,7 +1918,7 @@ void WiFiManager::setShowInfoErase(boolean enabled){ void WiFiManager::setMenu(const char * menu[], uint8_t size){ _menuIds.clear(); for(size_t i = 0; i < size; i++){ - for(size_t j = 0; j < _menuids_cnt; j++){ + for(size_t j = 0; j < _nummenutokens; j++){ if(menu[i] == _menutokens[j]){ if((String)menu[i] == "param") _paramsInWifi = false; // param auto flag _menuIds.push_back(j); @@ -1931,7 +1931,7 @@ void WiFiManager::setMenu(const char * menu[], uint8_t size){ void WiFiManager::setMenu(std::vector& menu){ _menuIds.clear(); for(auto menuitem : menu ){ - for(size_t j = 0; j<_menuids_cnt; j++){ + for(size_t j = 0; j < _nummenutokens; j++){ if(menuitem == _menutokens[j]){ if((String)menuitem == "param") _paramsInWifi = false; // param auto flag _menuIds.push_back(j); diff --git a/WiFiManager.h b/WiFiManager.h index c0cacec..0c95ca1 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -235,19 +235,6 @@ class WiFiManager #endif std::unique_ptr server; - uint8_t _menuids_cnt = 9; - const char * const _menutokens[9] = { - "wifi", - "wifinoscan", - "info", - "param", - "close", - "restart", - "exit", - "erase", - "sep" - }; - std::vector _menuIds; std::vector _menuIdsDefault = {"wifi","info","exit"}; diff --git a/strings_en.h b/strings_en.h index 66dec3d..a5e5347 100644 --- a/strings_en.h +++ b/strings_en.h @@ -210,6 +210,19 @@ const char D_HR[] PROGMEM = "--------------------"; // ----------------------------------------------------------------------------------------------- // DO NOT EDIT BELOW THIS LINE +const uint8_t _nummenutokens = 9; +const char * const _menutokens[9] PROGMEM = { + "wifi", + "wifinoscan", + "info", + "param", + "close", + "restart", + "exit", + "erase", + "sep" +}; + const char R_root[] PROGMEM = "/"; const char R_wifi[] PROGMEM = "/wifi"; const char R_wifinoscan[] PROGMEM = "/0wifi"; From 7f6bd107e698973f04137c816278706897cd60ef Mon Sep 17 00:00:00 2001 From: Shawn A Date: Tue, 20 Mar 2018 13:22:43 -0500 Subject: [PATCH 7/7] fix debug levels --- WiFiManager.cpp | 13 ++++++------- WiFiManager.h | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 41207cd..803d62a 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -133,7 +133,7 @@ bool WiFiManager::addParameter(WiFiManagerParameter *p) { _params[_paramsCount] = p; _paramsCount++; - DEBUG_WM("Added Parameter:",p->getID()); + DEBUG_WM(DEBUG_VERBOSE,"Added Parameter:",p->getID()); return true; } @@ -277,7 +277,7 @@ bool WiFiManager::startAP(){ #ifdef ESP8266 // @bug workaround for bug #4372 https://github.com/esp8266/Arduino/issues/4372 if(!WiFi.enableAP(true)) { - DEBUG_WM("enableAP failed!"); + DEBUG_WM(DEBUG_ERROR,"[ERROR] enableAP failed!"); return false; } delay(500); @@ -304,7 +304,7 @@ bool WiFiManager::startAP(){ if(ret && (String)_hostname != ""){ DEBUG_WM(DEBUG_VERBOSE,"setting softAP Hostname:",_hostname); bool res = WiFi.softAPsetHostname(_hostname); - if(!res)DEBUG_WM(F("hostname: AP set failed!")); + if(!res)DEBUG_WM(DEBUG_ERROR,F("[ERROR] hostname: AP set failed!")); DEBUG_WM(DEBUG_DEV,F("hostname: AP"),WiFi.softAPgetHostname()); } #endif @@ -397,7 +397,6 @@ void WiFiManager::setupConfigPortal() { server->on((String)FPSTR(R_close), std::bind(&WiFiManager::handleClose, this)); server->on((String)FPSTR(R_erase), std::bind(&WiFiManager::handleErase, this)); server->on((String)FPSTR(R_status), std::bind(&WiFiManager::handleWiFiStatus, this)); - //server->on("/fwlink", std::bind(&WiFiManager::handleRoot, this)); //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler. server->onNotFound (std::bind(&WiFiManager::handleNotFound, this)); server->begin(); // Web server start @@ -453,7 +452,7 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo startAP(); // init configportal - DEBUG_WM(DEBUG_VERBOSE,F("setupConfigPortal")); + DEBUG_WM(DEBUG_DEV,F("setupConfigPortal")); setupConfigPortal(); if(!_configPortalIsBlocking){ @@ -580,7 +579,7 @@ boolean WiFiManager::stopConfigPortal(){ DEBUG_WM(DEBUG_VERBOSE,F("disconnect configportal")); bool ret = false; ret = WiFi.softAPdisconnect(false); - if(!ret)DEBUG_WM(F("disconnect configportal - softAPdisconnect FAILED")); + if(!ret)DEBUG_WM(,DEBUG_ERROR,F("[ERROR] disconnect configportal - softAPdisconnect FAILED")); // WiFi_Mode(_usermode); // restore users wifi mode, BUG https://github.com/esp8266/Arduino/issues/4372 // DEBUG_WM("usermode",_usermode); // DEBUG_WM(getWLStatusString(WiFi.status())); @@ -1988,7 +1987,7 @@ void WiFiManager::DEBUG_WM(Generic text) { template void WiFiManager::DEBUG_WM(wm_debuglevel_t level,Generic text) { - if(_debugLevel >= level) DEBUG_WM(DEBUG_NOTIFY,text,""); + if(_debugLevel >= level) DEBUG_WM(level,text,""); } template diff --git a/WiFiManager.h b/WiFiManager.h index 0c95ca1..5db44e2 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -391,7 +391,7 @@ class WiFiManager } wm_debuglevel_t; boolean _debug = true; - uint8_t _debugLevel = DEBUG_DEV; + uint8_t _debugLevel = DEBUG_VERBOSE; Stream& _debugPort; // debug output stream ref template