diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 38d208d..4168559 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -128,7 +128,9 @@ bool WiFiManager::addParameter(WiFiManagerParameter *p) { if(p->getID()){ for (size_t i = 0; i < strlen(p->getID()); i++){ if(!(isAlphaNumeric(p->getID()[i])) && !(p->getID()[i]=='_')){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_ERROR,"[ERROR] parameter IDs can only contain alpha numeric chars"); + #endif return false; } } @@ -136,23 +138,37 @@ bool WiFiManager::addParameter(WiFiManagerParameter *p) { // init params if never malloc if(_params == NULL){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,"allocating params bytes:",_max_params * sizeof(WiFiManagerParameter*)); + #endif _params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); } // resize the params array by increment of WIFI_MANAGER_MAX_PARAMS if(_paramsCount == _max_params){ _max_params += WIFI_MANAGER_MAX_PARAMS; + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("Updated _max_params:"),_max_params); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,"re-allocating params bytes:",_max_params * sizeof(WiFiManagerParameter*)); + #endif WiFiManagerParameter** new_params = (WiFiManagerParameter**)realloc(_params, _max_params * sizeof(WiFiManagerParameter*)); + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(WIFI_MANAGER_MAX_PARAMS); + #endif + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(_paramsCount); + #endif + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(_max_params); + #endif if (new_params != NULL) { _params = new_params; } else { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_ERROR,"[ERROR] failed to realloc params, size not increased!"); + #endif return false; } } @@ -160,7 +176,9 @@ bool WiFiManager::addParameter(WiFiManagerParameter *p) { _params[_paramsCount] = p; _paramsCount++; + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"Added Parameter:",p->getID()); + #endif return true; } @@ -207,7 +225,9 @@ WiFiManager::~WiFiManager() { // parameters // @todo below belongs to wifimanagerparameter if (_params != NULL){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("freeing allocated params!")); + #endif free(_params); _params = NULL; } @@ -218,7 +238,9 @@ WiFiManager::~WiFiManager() { WiFi.removeEvent(wm_event_id); #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("unloading")); + #endif } void WiFiManager::_begin(){ @@ -252,7 +274,9 @@ boolean WiFiManager::autoConnect() { * @return {[type]} [description] */ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("AutoConnect")); + #endif if(getWiFiIsSaved()){ _begin(); @@ -260,7 +284,9 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { // attempt to connect using saved settings, on fail fallback to AP config portal if(!WiFi.enableSTA(true)){ // handle failure mode Brownout detector etc. + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_ERROR,"[FATAL] Unable to enable wifi!"); + #endif return false; } @@ -288,7 +314,9 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { bool connected = false; if (WiFi.status() == WL_CONNECTED){ connected = true; + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("AutoConnect: ESP Already Connected")); + #endif setSTAConfig(); // @todo not sure if this check makes sense, causes dup setSTAConfig in connectwifi, // and we have no idea WHAT we are connected to @@ -296,15 +324,23 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { if(connected || connectWifi("", "") == WL_CONNECTED){ //connected + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("AutoConnect: SUCCESS")); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("STA IP Address:"),WiFi.localIP()); + #endif _lastconxresult = WL_CONNECTED; if((String)_hostname != ""){ #ifdef ESP8266 + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,"hostname: STA: ",WiFi.hostname()); + #endif #elif defined(ESP32) + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,"hostname: STA: ",WiFi.getHostname()); + #endif #endif } @@ -316,10 +352,14 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { return false; // not connected and not cp } + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("AutoConnect: FAILED")); + #endif } else { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("No Credentials are Saved, skipping connect")); + #endif } // not connected start configportal @@ -329,19 +369,27 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { bool WiFiManager::setupHostname(bool restart){ if((String)_hostname == "") { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,"No Hostname to set"); + #endif return false; } else { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,"setupHostname: ",_hostname); + #endif } bool res = true; #ifdef ESP8266 + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"Setting WiFi hostname"); + #endif res = WiFi.hostname(_hostname); // #ifdef ESP8266MDNS_H #ifdef WM_MDNS + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname, tcp 80"); + #endif if(MDNS.begin(_hostname)){ MDNS.addService("http", "tcp", 80); } @@ -352,17 +400,23 @@ bool WiFiManager::setupHostname(bool restart){ res = WiFi.setHostname(_hostname); // #ifdef ESP32MDNS_H #ifdef WM_MDNS + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname, tcp 80"); + #endif if(MDNS.begin(_hostname)){ MDNS.addService("http", "tcp", 80); } #endif #endif + #ifdef WM_DEBUG_LEVEL if(!res)DEBUG_WM(DEBUG_ERROR,F("[ERROR] hostname: set failed!")); + #endif if(restart && (WiFi.status() == WL_CONNECTED)){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("reconnecting to set new hostname")); + #endif // WiFi.reconnect(); // This does not reset dhcp WiFi_Disconnect(); delay(200); // do not remove, need a delay for disconnect to change status() @@ -374,12 +428,16 @@ bool WiFiManager::setupHostname(bool restart){ // CONFIG PORTAL bool WiFiManager::startAP(){ bool ret = true; + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("StartAP with SSID: "),_apName); + #endif #ifdef ESP8266 // @bug workaround for bug #4372 https://github.com/esp8266/Arduino/issues/4372 if(!WiFi.enableAP(true)) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_ERROR,"[ERROR] enableAP failed!"); + #endif return false; } delay(500); // workaround delay @@ -387,9 +445,13 @@ bool WiFiManager::startAP(){ // setup optional soft AP static ip config if (_ap_static_ip) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("Custom AP IP/GW/Subnet:")); + #endif if(!WiFi.softAPConfig(_ap_static_ip, _ap_static_gw, _ap_static_sn)){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_ERROR,"[ERROR] softAPConfig failed!"); + #endif } } @@ -401,7 +463,9 @@ bool WiFiManager::startAP(){ else channel = _apChannel; if(channel>0){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"Starting AP on channel:",channel); + #endif } // start soft AP with password or anonymous @@ -414,7 +478,9 @@ bool WiFiManager::startAP(){ ret = WiFi.softAP(_apName.c_str(), _apPassword.c_str(),1,_apHidden);//password option } } else { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("AP has anonymous access!")); + #endif if(channel>0){ ret = WiFi.softAP(_apName.c_str(),"",channel,_apHidden); } @@ -425,19 +491,29 @@ bool WiFiManager::startAP(){ if(_debugLevel >= DEBUG_DEV) debugSoftAPConfig(); + #ifdef WM_DEBUG_LEVEL if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] There was a problem starting the AP"); + #endif // @todo add softAP retry here delay(500); // slight delay to make sure we get an AP IP + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("AP IP address:"),WiFi.softAPIP()); + #endif // set ap hostname #ifdef ESP32 if(ret && (String)_hostname != ""){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"setting softAP Hostname:",_hostname); + #endif bool res = WiFi.softAPsetHostname(_hostname); + #ifdef WM_DEBUG_LEVEL if(!res)DEBUG_WM(DEBUG_ERROR,F("[ERROR] hostname: AP set failed!")); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("hostname: AP: "),WiFi.softAPgetHostname()); + #endif } #endif @@ -462,7 +538,9 @@ void WiFiManager::startWebPortal() { */ void WiFiManager::stopWebPortal() { if(!configPortalActive && !webPortalActive) return; + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Stopping Web Portal")); + #endif webPortalActive = false; shutdownConfigPortal(); } @@ -472,7 +550,9 @@ boolean WiFiManager::configPortalHasTimeout(){ if(_configPortalTimeout == 0 || (_apClientCheck && (WiFi_softap_num_stations() > 0))){ if(millis() - timer > 30000){ timer = millis(); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"NUM CLIENTS: " + (String)WiFi_softap_num_stations()); + #endif } _configPortalStart = millis(); // kludge, bump configportal start time to skew timeouts return false; @@ -481,7 +561,9 @@ boolean WiFiManager::configPortalHasTimeout(){ if(_webClientCheck && (_webPortalAccessed>_configPortalStart)>0) _configPortalStart = _webPortalAccessed; if(millis() > _configPortalStart + _configPortalTimeout){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("config portal has timed out")); + #endif return true; } else if(_debugLevel > 0) { // log timeout @@ -489,7 +571,9 @@ boolean WiFiManager::configPortalHasTimeout(){ uint16_t logintvl = 30000; // how often to emit timeing out counter logging if((millis() - timer) > logintvl){ timer = millis(); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Portal Timeout In"),(String)((_configPortalStart + _configPortalTimeout-millis())/1000) + (String)F(" seconds")); + #endif } } } @@ -499,20 +583,28 @@ boolean WiFiManager::configPortalHasTimeout(){ void WiFiManager::setupConfigPortal() { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("Starting Web Portal")); + #endif // setup dns and web servers dnsServer.reset(new DNSServer()); server.reset(new WM_WebServer(_httpPort)); if(_httpPort != 80) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"http server started with custom port: ",_httpPort); // @todo not showing ip + #endif } /* Setup the DNS server redirecting all the domains to the apIP */ dnsServer->setErrorReplyCode(DNSReplyCode::NoError); + #ifdef WM_DEBUG_LEVEL // DEBUG_WM("dns server started port: ",DNS_PORT); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,"dns server started with ip: ",WiFi.softAPIP()); // @todo not showing ip + #endif dnsServer->start(DNS_PORT, F("*"), WiFi.softAPIP()); // @todo new callback, webserver started, callback cannot override handlers, but can grab them first @@ -537,7 +629,9 @@ void WiFiManager::setupConfigPortal() { server->onNotFound (std::bind(&WiFiManager::handleNotFound, this)); server->begin(); // Web server start + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("HTTP server started")); + #endif if(_preloadwifiscan) WiFi_scanNetworks(true,true); // preload wifiscan , async } @@ -561,7 +655,9 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo _apName = apName; // @todo check valid apname ? _apPassword = apPassword; + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Starting Config Portal")); + #endif if(_apName == "") _apName = getDefaultAPName(); @@ -573,7 +669,9 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo // this fixes most ap problems, however, simply doing mode(WIFI_AP) does not work if sta connection is hanging, must `wifi_station_disconnect` WiFi_Disconnect(); WiFi_enableSTA(false); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Disabling STA")); + #endif } else { // @todo even if sta is connected, it is possible that softap connections will fail, IOS says "invalid password", windows says "cannot connect to this network" researching @@ -588,7 +686,9 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo _configPortalStart = millis(); // start access point + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Enabling AP")); + #endif startAP(); WiFiSetCountry(); @@ -598,21 +698,29 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo } // init configportal + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("setupConfigPortal")); + #endif setupConfigPortal(); if(!_configPortalIsBlocking){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Config Portal Running, non blocking/processing")); + #endif return result; } + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Config Portal Running, blocking, waiting for clients...")); + #endif // blocking loop waiting for config while(1){ // if timed out or abort, break if(configPortalHasTimeout() || abort){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("configportal abort")); + #endif shutdownConfigPortal(); result = abort ? portalAbortResult : portalTimeoutResult; // false, false break; @@ -629,7 +737,9 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo yield(); // watchdog } + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_NOTIFY,F("config portal exiting")); + #endif return result; } @@ -661,20 +771,30 @@ uint8_t WiFiManager::processConfigPortal(){ // Waiting for save... if(connect) { connect = false; + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("processing save")); + #endif if(_enableCaptivePortal) delay(_cpclosedelay); // keeps the captiveportal from closing to fast. // skip wifi if no ssid if(_ssid == ""){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("No ssid, skipping wifi save")); + #endif } else{ // attempt sta connection to submitted _ssid, _pass if (connectWifi(_ssid, _pass) == WL_CONNECTED) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("Connect to new AP [SUCCESS]")); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("Got IP Address:")); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(WiFi.localIP()); + #endif if ( _savewificallback != NULL) { _savewificallback(); @@ -682,7 +802,9 @@ uint8_t WiFiManager::processConfigPortal(){ shutdownConfigPortal(); return WL_CONNECTED; // CONNECT SUCCESS } + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_ERROR,F("[ERROR] Connect to new AP Failed")); + #endif } if (_shouldBreakAfterConfig) { @@ -691,7 +813,9 @@ uint8_t WiFiManager::processConfigPortal(){ // @todo this is more of an exiting callback than a save, clarify when this should actually occur // confirm or verify data was saved to make this more accurate callback if ( _savewificallback != NULL) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("WiFi/Param save callback")); + #endif _savewificallback(); } shutdownConfigPortal(); @@ -704,7 +828,9 @@ uint8_t WiFiManager::processConfigPortal(){ // if connect fails, turn sta off to stabilize AP WiFi_Disconnect(); WiFi_enableSTA(false); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Disabling STA")); + #endif } } @@ -739,19 +865,31 @@ bool WiFiManager::shutdownConfigPortal(){ // https://github.com/esp8266/Arduino/issues/3793 // [APdisconnect] set_config failed! *WM: disconnect configportal - softAPdisconnect failed // still no way to reproduce reliably + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("disconnect configportal")); + #endif bool ret = false; ret = WiFi.softAPdisconnect(false); + #ifdef WM_DEBUG_LEVEL if(!ret)DEBUG_WM(DEBUG_ERROR,F("[ERROR] disconnect configportal - softAPdisconnect FAILED")); + #endif delay(1000); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"restoring usermode",getModeString(_usermode)); + #endif WiFi_Mode(_usermode); // restore users wifi mode, BUG https://github.com/esp8266/Arduino/issues/4372 if(WiFi.status()==WL_IDLE_STATUS){ WiFi.reconnect(); // restart wifi since we disconnected it in startconfigportal + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"WiFi Reconnect, was idle"); + #endif } + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"wifi status:",getWLStatusString(WiFi.status())); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"wifi mode:",getModeString(WiFi.getMode())); + #endif configPortalActive = false; _end(); return ret; @@ -761,7 +899,9 @@ bool WiFiManager::shutdownConfigPortal(){ // one for connecting to flash , one for new client // clean up, flow is convoluted, and causes bugs uint8_t WiFiManager::connectWifi(String ssid, String pass) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Connecting as wifi client...")); + #endif uint8_t retry = 1; uint8_t connRes = (uint8_t)WL_NO_SSID_AVAIL; @@ -775,7 +915,9 @@ uint8_t WiFiManager::connectWifi(String ssid, String pass) { while(retry <= _connectRetries){ if(_connectRetries > 1){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("Connect Wifi, ATTEMPT #"),(String)retry+" of "+(String)_connectRetries); + #endif } // if ssid argument provided connect to that if (ssid != "") { @@ -794,11 +936,15 @@ uint8_t WiFiManager::connectWifi(String ssid, String pass) { connRes = waitForConnectResult(); } else { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("No wifi saved, skipping")); + #endif } } + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Connection result:"),getWLStatusString(connRes)); + #endif retry++; } @@ -829,14 +975,22 @@ uint8_t WiFiManager::connectWifi(String ssid, String pass) { */ bool WiFiManager::wifiConnectNew(String ssid, String pass){ bool ret = false; + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("CONNECTED:"),WiFi.status() == WL_CONNECTED); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("Connecting to NEW AP:"),ssid); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("Using Password:"),pass); + #endif WiFi_enableSTA(true,storeSTAmode); // storeSTAmode will also toggle STA on in default opmode (persistent) if true (default) WiFi.persistent(true); ret = WiFi.begin(ssid.c_str(), pass.c_str()); WiFi.persistent(false); + #ifdef WM_DEBUG_LEVEL if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] wifi begin failed"); + #endif return ret; } @@ -847,14 +1001,24 @@ bool WiFiManager::wifiConnectNew(String ssid, String pass){ */ bool WiFiManager::wifiConnectDefault(){ bool ret = false; + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("Connecting to SAVED AP:"),WiFi_SSID(true)); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("Using Password:"),WiFi_psk(true)); + #endif ret = WiFi_enableSTA(true,storeSTAmode); delay(500); // THIS DELAY ? + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,"Mode after delay: "+getModeString(WiFi.getMode())); + #endif + #ifdef WM_DEBUG_LEVEL if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] wifi enableSta failed"); + #endif ret = WiFi.begin(); + #ifdef WM_DEBUG_LEVEL if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] wifi begin failed"); + #endif return ret; } @@ -865,23 +1029,37 @@ bool WiFiManager::wifiConnectDefault(){ * @return bool success */ bool WiFiManager::setSTAConfig(){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("STA static IP:"),_sta_static_ip); + #endif bool ret = true; if (_sta_static_ip) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Custom static IP/GW/Subnet/DNS")); + #endif if(_sta_static_dns) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Custom static DNS")); + #endif ret = WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn, _sta_static_dns); } else { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Custom STA IP/GW/Subnet")); + #endif ret = WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn); } + #ifdef WM_DEBUG_LEVEL if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] wifi config failed"); + #endif + #ifdef WM_DEBUG_LEVEL else DEBUG_WM(F("STA IP set:"),WiFi.localIP()); + #endif } else { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("setSTAConfig static ip not set, skipping")); + #endif } return ret; } @@ -899,19 +1077,25 @@ void WiFiManager::updateConxResult(uint8_t status){ #elif defined(ESP32) // if(_lastconxresult == WL_CONNECT_FAILED){ if(_lastconxresult == WL_CONNECT_FAILED || _lastconxresult == WL_DISCONNECTED){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,"lastconxresulttmp:",getWLStatusString(_lastconxresulttmp)); + #endif if(_lastconxresulttmp != WL_IDLE_STATUS){ _lastconxresult = _lastconxresulttmp; // _lastconxresulttmp = WL_IDLE_STATUS; } } #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,"lastconxresult:",getWLStatusString(_lastconxresult)); + #endif } uint8_t WiFiManager::waitForConnectResult() { + #ifdef WM_DEBUG_LEVEL if(_connectTimeout > 0) DEBUG_WM(DEBUG_VERBOSE,_connectTimeout,F("ms connectTimeout set")); + #endif return waitForConnectResult(_connectTimeout); } @@ -922,12 +1106,16 @@ uint8_t WiFiManager::waitForConnectResult() { */ uint8_t WiFiManager::waitForConnectResult(uint16_t timeout) { if (timeout == 0){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("connectTimeout not set, ESP waitForConnectResult...")); + #endif return WiFi.waitForConnectResult(); } unsigned long timeoutmillis = millis() + timeout; + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,timeout,F("ms timeout, waiting for connect...")); + #endif uint8_t status = WiFi.status(); while(millis() < timeoutmillis) { @@ -936,7 +1124,9 @@ uint8_t WiFiManager::waitForConnectResult(uint16_t timeout) { if (status == WL_CONNECTED || status == WL_CONNECT_FAILED) { return status; } + #ifdef WM_DEBUG_LEVEL DEBUG_WM (DEBUG_VERBOSE,F(".")); + #endif delay(100); } return status; @@ -945,13 +1135,17 @@ uint8_t WiFiManager::waitForConnectResult(uint16_t timeout) { // WPS enabled? https://github.com/esp8266/Arduino/pull/4889 #ifdef NO_EXTRA_4K_HEAP void WiFiManager::startWPS() { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("START WPS")); + #endif #ifdef ESP8266 WiFi.beginWPSConfig(); #else // @todo #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("END WPS")); + #endif } #endif @@ -986,7 +1180,9 @@ void WiFiManager::handleRequest() { * HTTPD CALLBACK root or redirect to captive portal */ void WiFiManager::handleRoot() { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Root")); + #endif if (captivePortal()) return; // If captive portal redirect instead of displaying the page handleRequest(); String page = getHTTPHead(FPSTR(S_options)); // @token options @todo replace options with title @@ -1011,11 +1207,15 @@ void WiFiManager::handleRoot() { * HTTPD CALLBACK Wifi config page handler */ void WiFiManager::handleWifi(boolean scan) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Wifi")); + #endif handleRequest(); String page = getHTTPHead(FPSTR(S_titlewifi)); // @token titlewifi if (scan) { + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(DEBUG_DEV,"refresh flag:",server->hasArg(F("refresh"))); + #endif WiFi_scanNetworks(server->hasArg(F("refresh")),false); //wifiscan, force if arg refresh page += getScanItemOut(); } @@ -1052,14 +1252,18 @@ void WiFiManager::handleWifi(boolean scan) { server->send(200, FPSTR(HTTP_HEAD_CT), page); // server->close(); // testing reliability fix for content length mismatches during mutiple flood hits + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("Sent config page")); + #endif } /** * HTTPD CALLBACK Wifi param page handler */ void WiFiManager::handleParam(){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Param")); + #endif handleRequest(); String page = getHTTPHead(FPSTR(S_titleparam)); // @token titlewifi @@ -1078,7 +1282,9 @@ void WiFiManager::handleParam(){ server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length())); server->send(200, FPSTR(HTTP_HEAD_CT), page); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("Sent param page")); + #endif } @@ -1101,8 +1307,12 @@ String WiFiManager::getMenuOut(){ void WiFiManager::WiFi_scanComplete(int networksFound){ _lastscan = millis(); _numNetworks = networksFound; + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("WiFi Scan ASYNC completed"), "in "+(String)(_lastscan - _startscan)+" ms"); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("WiFi Scan ASYNC found:"),_numNetworks); + #endif } bool WiFiManager::WiFi_scanNetworks(){ @@ -1116,23 +1326,33 @@ bool WiFiManager::WiFi_scanNetworks(unsigned int cachetime){ return WiFi_scanNetworks(millis()-_lastscan > cachetime,false); } bool WiFiManager::WiFi_scanNetworks(bool force,bool async){ + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(DEBUG_DEV,"scanNetworks async:",async == true); + #endif + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(DEBUG_DEV,_numNetworks,(millis()-_lastscan )); + #endif + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(DEBUG_DEV,"scanNetworks force:",force == true); + #endif if(force || _numNetworks == 0 || (millis()-_lastscan > 60000)){ int8_t res; _startscan = millis(); if(async && _asyncScan){ #ifdef ESP8266 #ifndef WM_NOASYNC // no async available < 2.4.0 + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("WiFi Scan ASYNC started")); + #endif using namespace std::placeholders; // for `_1` WiFi.scanNetworksAsync(std::bind(&WiFiManager::WiFi_scanComplete,this,_1)); #else res = WiFi.scanNetworks(); #endif #else + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("WiFi Scan ASYNC started")); + #endif res = WiFi.scanNetworks(true); #endif return false; @@ -1141,23 +1361,33 @@ bool WiFiManager::WiFi_scanNetworks(bool force,bool async){ res = WiFi.scanNetworks(); } if(res == WIFI_SCAN_FAILED){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_ERROR,"[ERROR] scan failed"); + #endif } else if(res == WIFI_SCAN_RUNNING){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_ERROR,"[ERROR] scan waiting"); + #endif while(WiFi.scanComplete() == WIFI_SCAN_RUNNING){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_ERROR,"."); + #endif delay(100); } _numNetworks = WiFi.scanComplete(); } else if(res >=0 ) _numNetworks = res; _lastscan = millis(); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("WiFi Scan completed"), "in "+(String)(_lastscan - _startscan)+" ms"); + #endif return true; } else { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"Scan is cached",(String)(millis()-_lastscan )+" ms ago"); + #endif } return false; } @@ -1169,11 +1399,15 @@ String WiFiManager::WiFiManager::getScanItemOut(){ int n = _numNetworks; if (n == 0) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("No networks found")); + #endif page += FPSTR(S_nonetworks); // @token nonetworks } else { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(n,F("networks found")); + #endif //sort networks int indices[n]; for (int i = 0; i < n; i++) { @@ -1204,7 +1438,9 @@ String WiFiManager::WiFiManager::getScanItemOut(){ cssid = WiFi.SSID(indices[i]); for (int j = i + 1; j < n; j++) { if (cssid == WiFi.SSID(indices[j])) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("DUP AP:"),WiFi.SSID(indices[j])); + #endif indices[j] = -1; // set dup aps to index -1 } } @@ -1231,7 +1467,9 @@ String WiFiManager::WiFiManager::getScanItemOut(){ for (int i = 0; i < n; i++) { if (indices[i] == -1) continue; // skip dups + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("AP: "),(String)WiFi.RSSI(indices[i]) + " " + (String)WiFi.SSID(indices[i])); + #endif int rssiperc = getRSSIasQuality(WiFi.RSSI(indices[i])); uint8_t enc_type = WiFi.encryptionType(indices[i]); @@ -1250,11 +1488,15 @@ String WiFiManager::WiFiManager::getScanItemOut(){ item.replace(FPSTR(T_i), ""); } } + #ifdef WM_DEBUG_LEVEL //DEBUG_WM(item); + #endif page += item; delay(0); } else { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Skipping , does not meet _minimumQuality")); + #endif } } @@ -1281,7 +1523,9 @@ String WiFiManager::getIpForm(String id, String title, String value){ String WiFiManager::getStaticOut(){ String page; if ((_staShowStaticFields || _sta_static_ip) && _staShowStaticFields>=0) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,"_staShowStaticFields"); + #endif page += FPSTR(HTTP_FORM_STATIC_HEAD); // @todo how can we get these accurate settings from memory , wifi_get_ip_info does not seem to reveal if struct ip_info is static or not page += getIpForm(FPSTR(S_ip),FPSTR(S_staticip),(_sta_static_ip ? _sta_static_ip.toString() : "")); // @token staticip @@ -1321,7 +1565,9 @@ String WiFiManager::getParamOut(){ // add the extra parameters to the form for (int i = 0; i < _paramsCount; i++) { if (_params[i] == NULL || _params[i]->_length == 0) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_ERROR,"[ERROR] WiFiManagerParameter is out of scope"); + #endif break; } @@ -1367,7 +1613,9 @@ String WiFiManager::getParamOut(){ } void WiFiManager::handleWiFiStatus(){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP WiFi status ")); + #endif handleRequest(); String page; // String page = "{\"result\":true,\"count\":1}"; @@ -1382,8 +1630,12 @@ void WiFiManager::handleWiFiStatus(){ * HTTPD CALLBACK save form and redirect to WLAN config page again */ void WiFiManager::handleWifiSave() { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP WiFi save ")); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("Method:"),server->method() == HTTP_GET ? (String)FPSTR(S_GET) : (String)FPSTR(S_POST)); + #endif handleRequest(); // @todo use new callback for before paramsaves @@ -1401,22 +1653,30 @@ void WiFiManager::handleWifiSave() { //_sta_static_ip.fromString(server->arg(FPSTR(S_ip)); String ip = server->arg(FPSTR(S_ip)); optionalIPFromString(&_sta_static_ip, ip.c_str()); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("static ip:"),ip); + #endif } if (server->arg(FPSTR(S_gw)) != "") { String gw = server->arg(FPSTR(S_gw)); optionalIPFromString(&_sta_static_gw, gw.c_str()); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("static gateway:"),gw); + #endif } if (server->arg(FPSTR(S_sn)) != "") { String sn = server->arg(FPSTR(S_sn)); optionalIPFromString(&_sta_static_sn, sn.c_str()); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("static netmask:"),sn); + #endif } if (server->arg(FPSTR(S_dns)) != "") { String dns = server->arg(FPSTR(S_dns)); optionalIPFromString(&_sta_static_dns, dns.c_str()); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("static DNS:"),dns); + #endif } String page; @@ -1435,15 +1695,21 @@ void WiFiManager::handleWifiSave() { server->sendHeader(FPSTR(HTTP_HEAD_CORS), FPSTR(HTTP_HEAD_CORS_ALLOW_ALL)); server->send(200, FPSTR(HTTP_HEAD_CT), page); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("Sent wifi save page")); + #endif connect = true; //signal ready to connect/reset process in processConfigPortal } void WiFiManager::handleParamSave() { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP WiFi save ")); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("Method:"),server->method() == HTTP_GET ? (String)FPSTR(S_GET) : (String)FPSTR(S_POST)); + #endif handleRequest(); doParamSave(); @@ -1455,7 +1721,9 @@ void WiFiManager::handleParamSave() { server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length())); server->send(200, FPSTR(HTTP_HEAD_CT), page); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("Sent param save page")); + #endif } void WiFiManager::doParamSave(){ @@ -1466,12 +1734,18 @@ void WiFiManager::doParamSave(){ //parameters if(_paramsCount > 0){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Parameters")); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,FPSTR(D_HR)); + #endif for (int i = 0; i < _paramsCount; i++) { if (_params[i] == NULL || _params[i]->_length == 0) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_ERROR,"[ERROR] WiFiManagerParameter is out of scope"); + #endif break; // @todo might not be needed anymore } //read parameter from server @@ -1485,9 +1759,13 @@ void WiFiManager::doParamSave(){ //store it in params array value.toCharArray(_params[i]->_value, _params[i]->_length+1); // length+1 null terminated + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,(String)_params[i]->getID() + ":",value); + #endif } + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,FPSTR(D_HR)); + #endif } if ( _saveparamscallback != NULL) { @@ -1500,7 +1778,9 @@ void WiFiManager::doParamSave(){ * HTTPD CALLBACK info page */ void WiFiManager::handleInfo() { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Info")); + #endif handleRequest(); String page = getHTTPHead(FPSTR(S_titleinfo)); // @token titleinfo reportStatus(page); @@ -1582,7 +1862,9 @@ void WiFiManager::handleInfo() { server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length())); server->send(200, FPSTR(HTTP_HEAD_CT), page); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("Sent info page")); + #endif } String WiFiManager::getInfoData(String id){ @@ -1779,7 +2061,9 @@ String WiFiManager::getInfoData(String id){ * HTTPD CALLBACK root or redirect to captive portal */ void WiFiManager::handleExit() { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Exit")); + #endif handleRequest(); String page = getHTTPHead(FPSTR(S_titleexit)); // @token titleexit page += FPSTR(S_exiting); // @token exiting @@ -1792,7 +2076,9 @@ void WiFiManager::handleExit() { * HTTPD CALLBACK reset page */ void WiFiManager::handleReset() { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Reset")); + #endif handleRequest(); String page = getHTTPHead(FPSTR(S_titlereset)); //@token titlereset page += FPSTR(S_resetting); //@token resetting @@ -1801,7 +2087,9 @@ void WiFiManager::handleReset() { server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length())); server->send(200, FPSTR(HTTP_HEAD_CT), page); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("RESETTING ESP")); + #endif delay(1000); reboot(); } @@ -1814,7 +2102,9 @@ void WiFiManager::handleReset() { // handleErase(false); // } void WiFiManager::handleErase(boolean opt) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Erase")); + #endif handleRequest(); String page = getHTTPHead(FPSTR(S_titleerase)); // @token titleerase @@ -1823,7 +2113,9 @@ void WiFiManager::handleErase(boolean opt) { if(ret) page += FPSTR(S_resetting); // @token resetting else { page += FPSTR(S_error); // @token erroroccur + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_ERROR,F("[ERROR] WiFi EraseConfig failed")); + #endif } page += FPSTR(HTTP_END); @@ -1832,7 +2124,9 @@ void WiFiManager::handleErase(boolean opt) { if(ret){ delay(2000); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("RESETTING ESP")); + #endif reboot(); } } @@ -1868,7 +2162,9 @@ void WiFiManager::handleNotFound() { * Return true in that case so the page handler do not try to handle the request again. */ boolean WiFiManager::captivePortal() { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,"-> " + server->hostHeader()); + #endif if(!_enableCaptivePortal) return false; // skip redirections, @todo maybe allow redirection even when no cp ? might be useful @@ -1878,7 +2174,9 @@ boolean WiFiManager::captivePortal() { // doredirect = !isIp(server->hostHeader()) // old check if (doredirect) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- Request redirected to captive portal")); + #endif server->sendHeader(F("Location"), (String)F("http://") + serverLoc, true); server->send ( 302, FPSTR(HTTP_HEAD_CT2), ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. server->client().stop(); // Stop is needed because we sent no content length @@ -1894,7 +2192,9 @@ void WiFiManager::stopCaptivePortal(){ void WiFiManager::handleClose(){ stopCaptivePortal(); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP close")); + #endif handleRequest(); String page = getHTTPHead(FPSTR(S_titleclose)); // @token titleclose page += FPSTR(S_closing); // @token closing @@ -1969,10 +2269,14 @@ bool WiFiManager::stopConfigPortal(){ */ bool WiFiManager::disconnect(){ if(WiFi.status() != WL_CONNECTED){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"Disconnecting: Not connected"); + #endif return false; } + #ifdef WM_DEBUG_LEVEL DEBUG_WM("Disconnecting"); + #endif return WiFi_Disconnect(); } @@ -1981,7 +2285,9 @@ bool WiFiManager::disconnect(){ * @access public */ void WiFiManager::reboot(){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM("Restarting"); + #endif ESP.restart(); } @@ -1994,28 +2300,42 @@ bool WiFiManager::erase(){ } bool WiFiManager::erase(bool opt){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM("Erasing"); + #endif #if defined(ESP32) && ((defined(WM_ERASE_NVS) || defined(nvs_flash_h))) // if opt true, do nvs erase if(opt){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM("Erasing NVS"); + #endif esp_err_t err; err = nvs_flash_init(); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"nvs_flash_init: ",err!=ESP_OK ? (String)err : "Success"); + #endif err = nvs_flash_erase(); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"nvs_flash_erase: ", err!=ESP_OK ? (String)err : "Success"); + #endif return err == ESP_OK; } #elif defined(ESP8266) && defined(spiffs_api_h) if(opt){ bool ret = false; if(SPIFFS.begin()){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM("Erasing SPIFFS"); + #endif bool ret = SPIFFS.format(); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"spiffs erase: ",ret ? "Success" : "ERROR"); + #endif } else{ + #ifdef WM_DEBUG_LEVEL DEBUG_WM("[ERROR] Could not start SPIFFS"); + #endif } return ret; } @@ -2023,7 +2343,9 @@ bool WiFiManager::erase(bool opt){ (void)opt; #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM("Erasing WiFi Config"); + #endif return WiFi_eraseConfig(); } @@ -2033,7 +2355,9 @@ bool WiFiManager::erase(bool opt){ * @access public */ void WiFiManager::resetSettings() { +#ifdef WM_DEBUG_LEVEL DEBUG_WM(F("resetSettings")); + #endif WiFi_enableSTA(true,true); // must be sta to disconnect erase if (_resetcallback != NULL) @@ -2046,7 +2370,9 @@ void WiFiManager::resetSettings() { WiFi.disconnect(true); WiFi.persistent(false); #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("SETTINGS ERASED")); + #endif } // SETTERS @@ -2272,7 +2598,9 @@ void WiFiManager::setConfigPortalBlocking(boolean shoudlBlock) { void WiFiManager::setRestorePersistent(boolean persistent) { _userpersistent = persistent; if(!persistent){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("persistent is off")); + #endif } } @@ -2430,7 +2758,9 @@ void WiFiManager::setShowInfoErase(boolean enabled){ * @param uint8_t menu[] array of menu ids */ void WiFiManager::setMenu(const char * menu[], uint8_t size){ +#ifdef WM_DEBUG_LEVEL // DEBUG_WM(DEBUG_VERBOSE,"setmenu array"); + #endif _menuIds.clear(); for(size_t i = 0; i < size; i++){ for(size_t j = 0; j < _nummenutokens; j++){ @@ -2440,7 +2770,9 @@ void WiFiManager::setMenu(const char * menu[], uint8_t size){ } } } + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(getMenuOut()); + #endif } /** @@ -2453,7 +2785,9 @@ void WiFiManager::setMenu(const char * menu[], uint8_t size){ * @param {[type]} std::vector& menu [description] */ void WiFiManager::setMenu(std::vector& menu){ +#ifdef WM_DEBUG_LEVEL // DEBUG_WM(DEBUG_VERBOSE,"setmenu vector"); + #endif _menuIds.clear(); for(auto menuitem : menu ){ for(size_t j = 0; j < _nummenutokens; j++){ @@ -2463,7 +2797,9 @@ void WiFiManager::setMenu(std::vector& menu){ } } } + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(getMenuOut()); + #endif } @@ -2640,20 +2976,44 @@ void WiFiManager::debugSoftAPConfig(){ esp_wifi_get_country(&country); #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("SoftAP Configuration")); - DEBUG_WM(FPSTR(D_HR)); - DEBUG_WM(F("ssid: "),(char *) config.ssid); - DEBUG_WM(F("password: "),(char *) config.password); - DEBUG_WM(F("ssid_len: "),config.ssid_len); - DEBUG_WM(F("channel: "),config.channel); - DEBUG_WM(F("authmode: "),config.authmode); - DEBUG_WM(F("ssid_hidden: "),config.ssid_hidden); - DEBUG_WM(F("max_connection: "),config.max_connection); - #if !defined(WM_NOCOUNTRY) - DEBUG_WM(F("country: "),(String)country.cc); #endif - DEBUG_WM(F("beacon_interval: "),(String)config.beacon_interval + "(ms)"); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(FPSTR(D_HR)); + #endif + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("ssid: "),(char *) config.ssid); + #endif + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("password: "),(char *) config.password); + #endif + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("ssid_len: "),config.ssid_len); + #endif + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("channel: "),config.channel); + #endif + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("authmode: "),config.authmode); + #endif + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("ssid_hidden: "),config.ssid_hidden); + #endif + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("max_connection: "),config.max_connection); + #endif + #if !defined(WM_NOCOUNTRY) + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("country: "),(String)country.cc); + #endif + #endif + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("beacon_interval: "),(String)config.beacon_interval + "(ms)"); + #endif + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(FPSTR(D_HR)); + #endif } /** @@ -2664,20 +3024,42 @@ void WiFiManager::debugSoftAPConfig(){ void WiFiManager::debugPlatformInfo(){ #ifdef ESP8266 system_print_meminfo(); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("getCoreVersion(): "),ESP.getCoreVersion()); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("system_get_sdk_version(): "),system_get_sdk_version()); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("system_get_boot_version():"),system_get_boot_version()); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("getFreeHeap(): "),(String)ESP.getFreeHeap()); + #endif #elif defined(ESP32) + #ifdef WM_DEBUG_LEVEL DEBUG_WM("Free heap: ", ESP.getFreeHeap()); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM("ESP SDK version: ", ESP.getSdkVersion()); + #endif // esp_chip_info_t chipInfo; // esp_chip_info(&chipInfo); + #ifdef WM_DEBUG_LEVEL // DEBUG_WM("Chip Info: Model: ",chipInfo.model); + #endif + #ifdef WM_DEBUG_LEVEL // DEBUG_WM("Chip Info: Cores: ",chipInfo.cores); + #endif + #ifdef WM_DEBUG_LEVEL // DEBUG_WM("Chip Info: Rev: ",chipInfo.revision); + #endif + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(printf("Chip Info: Model: %d, cores: %d, revision: %d", chipInfo.model.c_str(), chipInfo.cores, chipInfo.revision)); + #endif + #ifdef WM_DEBUG_LEVEL // DEBUG_WM("Chip Rev: ",(String)ESP.getChipRevision()); + #endif // core version is not avail #endif } @@ -2721,12 +3103,18 @@ boolean WiFiManager::validApPassword(){ if (_apPassword == NULL) _apPassword = ""; if (_apPassword != "") { if (_apPassword.length() < 8 || _apPassword.length() > 63) { + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("AccessPoint set password is INVALID or <8 chars")); + #endif _apPassword = ""; return false; // @todo FATAL or fallback to empty ? } + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("AccessPoint set password is VALID")); + #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(_apPassword); + #endif } return true; } @@ -2761,7 +3149,9 @@ String WiFiManager::getWLStatusString(uint8_t status){ } String WiFiManager::encryptionTypeStr(uint8_t authmode) { +#ifdef WM_DEBUG_LEVEL // DEBUG_WM("enc_tye: ",authmode); + #endif return AUTH_MODE_NAMES[authmode]; } @@ -2779,18 +3169,26 @@ bool WiFiManager::WiFiSetCountry(){ else if(_wificountry == "US") ret = esp_wifi_set_country(&WM_COUNTRY_US) == ESP_OK; else if(_wificountry == "JP") ret = esp_wifi_set_country(&WM_COUNTRY_JP) == ESP_OK; else if(_wificountry == "CN") ret = esp_wifi_set_country(&WM_COUNTRY_CN) == ESP_OK; + #ifdef WM_DEBUG_LEVEL else DEBUG_WM(DEBUG_ERROR,"[ERROR] country code not found"); + #endif #elif defined(ESP8266) && !defined(WM_NOCOUNTRY) // if(WiFi.getMode() == WIFI_OFF); // exception if wifi not init! if(_wificountry == "US") ret = wifi_set_country((wifi_country_t*)&WM_COUNTRY_US); else if(_wificountry == "JP") ret = wifi_set_country((wifi_country_t*)&WM_COUNTRY_JP); else if(_wificountry == "CN") ret = wifi_set_country((wifi_country_t*)&WM_COUNTRY_CN); + #ifdef WM_DEBUG_LEVEL else DEBUG_WM(DEBUG_ERROR,"[ERROR] country code not found"); #endif + #endif + #ifdef WM_DEBUG_LEVEL if(ret) DEBUG_WM(DEBUG_VERBOSE,"esp_wifi_set_country: " + _wificountry); + #endif + #ifdef WM_DEBUG_LEVEL else DEBUG_WM(DEBUG_ERROR,"[ERROR] esp_wifi_set_country failed"); + #endif return ret; } @@ -2822,14 +3220,18 @@ bool WiFiManager::WiFi_Disconnect() { #ifdef ESP8266 if((WiFi.getMode() & WIFI_STA) != 0) { bool ret; + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("WiFi station disconnect")); + #endif ETS_UART_INTR_DISABLE(); // @todo probably not needed ret = wifi_station_disconnect(); ETS_UART_INTR_ENABLE(); return ret; } #elif defined(ESP32) + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("WiFi station disconnect")); + #endif return WiFi.disconnect(); // not persistent atm #endif return false; @@ -2837,7 +3239,9 @@ bool WiFiManager::WiFi_Disconnect() { // toggle STA without persistent bool WiFiManager::WiFi_enableSTA(bool enable,bool persistent) { +#ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_DEV,F("WiFi_enableSTA"),(String) enable? "enable" : "disable"); + #endif #ifdef ESP8266 WiFiMode_t newMode; WiFiMode_t currentMode = WiFi.getMode(); @@ -2847,7 +3251,9 @@ bool WiFiManager::WiFi_enableSTA(bool enable,bool persistent) { if((isEnabled != enable) || persistent) { if(enable) { + #ifdef WM_DEBUG_LEVEL if(persistent) DEBUG_WM(DEBUG_DEV,F("enableSTA PERSISTENT ON")); + #endif return WiFi_Mode(newMode,persistent); } else { @@ -2964,19 +3370,29 @@ String WiFiManager::WiFi_psk(bool persistent) const { #ifdef ESP32 void WiFiManager::WiFiEvent(WiFiEvent_t event,system_event_info_t info){ if(!_hasBegun){ + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(DEBUG_VERBOSE,"[ERROR] WiFiEvent, not ready"); + #endif Serial.println("[ERROR] wm not ready"); return; } + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(DEBUG_VERBOSE,"[EVENT]",event); + #endif if(event == SYSTEM_EVENT_STA_DISCONNECTED){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"[EVENT] WIFI_REASON:",info.disconnected.reason); + #endif if(info.disconnected.reason == WIFI_REASON_AUTH_EXPIRE || info.disconnected.reason == WIFI_REASON_AUTH_FAIL){ _lastconxresulttmp = 7; // hack in wrong password internally, sdk emit WIFI_REASON_AUTH_EXPIRE on some routers on auth_fail } else _lastconxresulttmp = WiFi.status(); + #ifdef WM_DEBUG_LEVEL if(info.disconnected.reason == WIFI_REASON_NO_AP_FOUND) DEBUG_WM(DEBUG_VERBOSE,"[EVENT] WIFI_REASON: NO_AP_FOUND"); + #endif #ifdef esp32autoreconnect + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"[Event] SYSTEM_EVENT_STA_DISCONNECTED, reconnecting"); + #endif WiFi.reconnect(); #endif } @@ -2993,7 +3409,9 @@ void WiFiManager::WiFi_autoReconnect(){ #elif defined(ESP32) // if(_wifiAutoReconnect){ // @todo move to seperate method, used for event listener now + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,"ESP32 event handler enabled"); + #endif using namespace std::placeholders; wm_event_id = WiFi.onEvent(std::bind(&WiFiManager::WiFiEvent,this,_1,_2)); // }