diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 6008fb4..5d11167 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]=='_')){ - DEBUG_WM(DEBUG_ERROR,"[ERROR] parameter IDs can only contain alpha numeric chars"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_ERROR,F("[ERROR] parameter IDs can only contain alpha numeric chars")); + #endif return false; } } @@ -136,23 +138,31 @@ bool WiFiManager::addParameter(WiFiManagerParameter *p) { // init params if never malloc if(_params == NULL){ - DEBUG_WM(DEBUG_DEV,"allocating params bytes:",_max_params * sizeof(WiFiManagerParameter*)); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_DEV,F("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); - DEBUG_WM(DEBUG_DEV,"re-allocating params bytes:",_max_params * sizeof(WiFiManagerParameter*)); + DEBUG_WM(DEBUG_DEV,F("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); // DEBUG_WM(_paramsCount); // DEBUG_WM(_max_params); + #endif if (new_params != NULL) { _params = new_params; } else { - DEBUG_WM(DEBUG_ERROR,"[ERROR] failed to realloc params, size not increased!"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_ERROR,F("[ERROR] failed to realloc params, size not increased!")); + #endif return false; } } @@ -160,7 +170,9 @@ bool WiFiManager::addParameter(WiFiManagerParameter *p) { _params[_paramsCount] = p; _paramsCount++; - DEBUG_WM(DEBUG_VERBOSE,"Added Parameter:",p->getID()); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("Added Parameter:"),p->getID()); + #endif return true; } @@ -207,7 +219,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 +232,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 +268,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 +278,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. - DEBUG_WM(DEBUG_ERROR,"[FATAL] Unable to enable wifi!"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_ERROR,F("[FATAL] Unable to enable wifi!")); + #endif return false; } @@ -288,7 +308,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 +318,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 - DEBUG_WM(DEBUG_DEV,"hostname: STA: ",WiFi.hostname()); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_DEV,F("hostname: STA: "),WiFi.hostname()); + #endif #elif defined(ESP32) - DEBUG_WM(DEBUG_DEV,"hostname: STA: ",WiFi.getHostname()); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_DEV,F("hostname: STA: "),WiFi.getHostname()); + #endif #endif } @@ -316,9 +346,15 @@ 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 DEBUG_WM(F("No Credentials are Saved, skipping connect")); + else { + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("No Credentials are Saved, skipping connect")); + #endif + } // not connected start configportal bool res = startConfigPortal(apName, apPassword); @@ -327,16 +363,27 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { bool WiFiManager::setupHostname(bool restart){ if((String)_hostname == "") { - DEBUG_WM(DEBUG_DEV,"No Hostname to set"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_DEV,F("No Hostname to set")); + #endif return false; - } else DEBUG_WM(DEBUG_DEV,"setupHostname: ",_hostname); + } + else { + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_DEV,F("setupHostname: "),_hostname); + #endif + } bool res = true; #ifdef ESP8266 - DEBUG_WM(DEBUG_VERBOSE,"Setting WiFi hostname"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("Setting WiFi hostname")); + #endif res = WiFi.hostname(_hostname); // #ifdef ESP8266MDNS_H #ifdef WM_MDNS - DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname, tcp 80"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("Setting MDNS hostname, tcp 80")); + #endif if(MDNS.begin(_hostname)){ MDNS.addService("http", "tcp", 80); } @@ -347,17 +394,23 @@ bool WiFiManager::setupHostname(bool restart){ res = WiFi.setHostname(_hostname); // #ifdef ESP32MDNS_H #ifdef WM_MDNS - DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname, tcp 80"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("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() @@ -369,12 +422,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)) { - DEBUG_WM(DEBUG_ERROR,"[ERROR] enableAP failed!"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_ERROR,F("[ERROR] enableAP failed!")); + #endif return false; } delay(500); // workaround delay @@ -382,9 +439,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)){ - DEBUG_WM(DEBUG_ERROR,"[ERROR] softAPConfig failed!"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_ERROR,F("[ERROR] softAPConfig failed!")); + #endif } } @@ -396,7 +457,9 @@ bool WiFiManager::startAP(){ else channel = _apChannel; if(channel>0){ - DEBUG_WM(DEBUG_VERBOSE,"Starting AP on channel:",channel); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("Starting AP on channel:"),channel); + #endif } // start soft AP with password or anonymous @@ -409,7 +472,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); } @@ -420,19 +485,29 @@ bool WiFiManager::startAP(){ if(_debugLevel >= DEBUG_DEV) debugSoftAPConfig(); - if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] There was a problem starting the AP"); + #ifdef WM_DEBUG_LEVEL + if(!ret) DEBUG_WM(DEBUG_ERROR,F("[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 != ""){ - DEBUG_WM(DEBUG_VERBOSE,"setting softAP Hostname:",_hostname); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("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 @@ -457,7 +532,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(); } @@ -467,7 +544,9 @@ boolean WiFiManager::configPortalHasTimeout(){ if(_configPortalTimeout == 0 || (_apClientCheck && (WiFi_softap_num_stations() > 0))){ if(millis() - timer > 30000){ timer = millis(); - DEBUG_WM(DEBUG_VERBOSE,"NUM CLIENTS: " + (String)WiFi_softap_num_stations()); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("NUM CLIENTS: "),(String)WiFi_softap_num_stations()); + #endif } _configPortalStart = millis(); // kludge, bump configportal start time to skew timeouts return false; @@ -476,7 +555,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 @@ -484,7 +565,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 } } } @@ -494,18 +577,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) DEBUG_WM(DEBUG_VERBOSE,"http server started with custom port: ",_httpPort); // @todo not showing ip + if(_httpPort != 80) { + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("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); - DEBUG_WM(DEBUG_DEV,"dns server started with ip: ",WiFi.softAPIP()); // @todo not showing ip + #endif + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_DEV,F("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 @@ -534,7 +627,9 @@ void WiFiManager::setupConfigPortal() { server->on((String)FPSTR(R_updatedone), HTTP_POST, std::bind(&WiFiManager::handleUpdateDone, this), std::bind(&WiFiManager::handleUpdating, 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 } @@ -558,7 +653,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(); @@ -570,7 +667,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 @@ -585,7 +684,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(); @@ -595,21 +696,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; @@ -626,7 +735,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; } @@ -658,20 +769,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(); @@ -679,7 +800,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) { @@ -688,7 +811,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(); @@ -701,7 +826,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 } } @@ -736,19 +863,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); - DEBUG_WM(DEBUG_VERBOSE,"restoring usermode",getModeString(_usermode)); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("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 - DEBUG_WM(DEBUG_VERBOSE,"WiFi Reconnect, was idle"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("WiFi Reconnect, was idle")); + #endif } - DEBUG_WM(DEBUG_VERBOSE,"wifi status:",getWLStatusString(WiFi.status())); - DEBUG_WM(DEBUG_VERBOSE,"wifi mode:",getModeString(WiFi.getMode())); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("wifi status:"),getWLStatusString(WiFi.status())); + #endif + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("wifi mode:"),getModeString(WiFi.getMode())); + #endif configPortalActive = false; _end(); return ret; @@ -758,7 +897,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; @@ -772,7 +913,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 != "") { @@ -791,11 +934,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++; } @@ -826,14 +973,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); - if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] wifi begin failed"); + #ifdef WM_DEBUG_LEVEL + if(!ret) DEBUG_WM(DEBUG_ERROR,F("[ERROR] wifi begin failed")); + #endif return ret; } @@ -844,14 +999,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 ? - DEBUG_WM(DEBUG_DEV,"Mode after delay: "+getModeString(WiFi.getMode())); - if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] wifi enableSta failed"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_DEV,F("Mode after delay: "),getModeString(WiFi.getMode())); + #endif + #ifdef WM_DEBUG_LEVEL + if(!ret) DEBUG_WM(DEBUG_ERROR,F("[ERROR] wifi enableSta failed")); + #endif ret = WiFi.begin(); - if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] wifi begin failed"); + #ifdef WM_DEBUG_LEVEL + if(!ret) DEBUG_WM(DEBUG_ERROR,F("[ERROR] wifi begin failed")); + #endif return ret; } @@ -862,23 +1027,35 @@ 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); } - if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] wifi config failed"); + #ifdef WM_DEBUG_LEVEL + if(!ret) DEBUG_WM(DEBUG_ERROR,F("[ERROR] wifi config failed")); 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; } @@ -896,19 +1073,23 @@ void WiFiManager::updateConxResult(uint8_t status){ #elif defined(ESP32) // if(_lastconxresult == WL_CONNECT_FAILED){ if(_lastconxresult == WL_CONNECT_FAILED || _lastconxresult == WL_DISCONNECTED){ - DEBUG_WM(DEBUG_DEV,"lastconxresulttmp:",getWLStatusString(_lastconxresulttmp)); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_DEV,F("lastconxresulttmp:"),getWLStatusString(_lastconxresulttmp)); + #endif if(_lastconxresulttmp != WL_IDLE_STATUS){ _lastconxresult = _lastconxresulttmp; // _lastconxresulttmp = WL_IDLE_STATUS; } } + DEBUG_WM(DEBUG_DEV,F("lastconxresult:"),getWLStatusString(_lastconxresult)); #endif - DEBUG_WM(DEBUG_DEV,"lastconxresult:",getWLStatusString(_lastconxresult)); } uint8_t WiFiManager::waitForConnectResult() { + #ifdef WM_DEBUG_LEVEL if(_connectTimeout > 0) DEBUG_WM(DEBUG_VERBOSE,_connectTimeout,F("ms connectTimeout set")); + #endif return waitForConnectResult(_connectTimeout); } @@ -919,12 +1100,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) { @@ -933,7 +1118,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; @@ -942,13 +1129,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 @@ -1000,7 +1191,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 @@ -1025,11 +1218,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(); } @@ -1066,14 +1263,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 @@ -1092,7 +1293,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 } @@ -1115,8 +1318,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(){ @@ -1130,6 +1337,7 @@ 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); // DEBUG_WM(DEBUG_DEV,_numNetworks,(millis()-_lastscan )); // DEBUG_WM(DEBUG_DEV,"scanNetworks force:",force == true); @@ -1143,7 +1351,9 @@ bool WiFiManager::WiFi_scanNetworks(bool force,bool async){ 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 @@ -1151,7 +1361,9 @@ bool WiFiManager::WiFi_scanNetworks(bool force,bool async){ 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; @@ -1160,20 +1372,35 @@ bool WiFiManager::WiFi_scanNetworks(bool force,bool async){ DEBUG_WM(DEBUG_VERBOSE,F("WiFi Scan SYNC started")); res = WiFi.scanNetworks(); } - if(res == WIFI_SCAN_FAILED) DEBUG_WM(DEBUG_ERROR,"[ERROR] scan failed"); + if(res == WIFI_SCAN_FAILED){ + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_ERROR,F("[ERROR] scan failed")); + #endif + } else if(res == WIFI_SCAN_RUNNING){ - DEBUG_WM(DEBUG_ERROR,"[ERROR] scan waiting"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_ERROR,F("[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 DEBUG_WM(DEBUG_VERBOSE,"Scan is cached",(String)(millis()-_lastscan )+" ms ago"); + } + else { + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("Scan is cached"),(String)(millis()-_lastscan )+" ms ago"); + #endif + } return false; } @@ -1184,11 +1411,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++) { @@ -1219,7 +1450,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 } } @@ -1246,7 +1479,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]); @@ -1265,11 +1500,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 } } @@ -1296,7 +1535,9 @@ String WiFiManager::getIpForm(String id, String title, String value){ String WiFiManager::getStaticOut(){ String page; if ((_staShowStaticFields || _sta_static_ip) && _staShowStaticFields>=0) { - DEBUG_WM(DEBUG_DEV,"_staShowStaticFields"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_DEV,F("_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 @@ -1336,7 +1577,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) { - DEBUG_WM(DEBUG_ERROR,"[ERROR] WiFiManagerParameter is out of scope"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_ERROR,F("[ERROR] WiFiManagerParameter is out of scope")); + #endif break; } @@ -1382,7 +1625,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}"; @@ -1397,8 +1642,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 @@ -1416,22 +1665,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; @@ -1450,15 +1707,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(); @@ -1470,7 +1733,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(){ @@ -1481,12 +1746,16 @@ void WiFiManager::doParamSave(){ //parameters if(_paramsCount > 0){ + #ifdef WM_DEBUG_LEVEL DEBUG_WM(DEBUG_VERBOSE,F("Parameters")); DEBUG_WM(DEBUG_VERBOSE,FPSTR(D_HR)); + #endif for (int i = 0; i < _paramsCount; i++) { if (_params[i] == NULL || _params[i]->_length == 0) { - DEBUG_WM(DEBUG_ERROR,"[ERROR] WiFiManagerParameter is out of scope"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_ERROR,F("[ERROR] WiFiManagerParameter is out of scope")); + #endif break; // @todo might not be needed anymore } //read parameter from server @@ -1500,9 +1769,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) { @@ -1515,7 +1788,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); @@ -1601,7 +1876,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){ @@ -1798,7 +2075,9 @@ String WiFiManager::getInfoData(String id){ * HTTPD CALLBACK exit, closes configportal if blocking, if non blocking undefined */ 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 @@ -1814,7 +2093,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 @@ -1823,7 +2104,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(); } @@ -1836,7 +2119,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 @@ -1845,7 +2130,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); @@ -1854,7 +2141,9 @@ void WiFiManager::handleErase(boolean opt) { if(ret){ delay(2000); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("RESETTING ESP")); + #endif reboot(); } } @@ -1890,7 +2179,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 @@ -1900,7 +2191,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 @@ -1918,7 +2211,9 @@ void WiFiManager::stopCaptivePortal(){ void WiFiManager::handleClose(){ DEBUG_WM(DEBUG_VERBOSE,F("Disabling Captive Portal")); 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 @@ -1993,10 +2288,14 @@ bool WiFiManager::stopConfigPortal(){ */ bool WiFiManager::disconnect(){ if(WiFi.status() != WL_CONNECTED){ - DEBUG_WM(DEBUG_VERBOSE,"Disconnecting: Not connected"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("Disconnecting: Not connected")); + #endif return false; } - DEBUG_WM("Disconnecting"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("Disconnecting")); + #endif return WiFi_Disconnect(); } @@ -2005,7 +2304,9 @@ bool WiFiManager::disconnect(){ * @access public */ void WiFiManager::reboot(){ - DEBUG_WM("Restarting"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("Restarting")); + #endif ESP.restart(); } @@ -2018,34 +2319,52 @@ 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){ - DEBUG_WM("Erasing NVS"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("Erasing NVS")); + #endif esp_err_t err; err = nvs_flash_init(); - DEBUG_WM(DEBUG_VERBOSE,"nvs_flash_init: ",err!=ESP_OK ? (String)err : "Success"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("nvs_flash_init: "),err!=ESP_OK ? (String)err : "Success"); + #endif err = nvs_flash_erase(); - DEBUG_WM(DEBUG_VERBOSE,"nvs_flash_erase: ", err!=ESP_OK ? (String)err : "Success"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("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()){ - DEBUG_WM("Erasing SPIFFS"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("Erasing SPIFFS")); + #endif bool ret = SPIFFS.format(); - DEBUG_WM(DEBUG_VERBOSE,"spiffs erase: ",ret ? "Success" : "ERROR"); - } else DEBUG_WM("[ERROR] Could not start SPIFFS"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("spiffs erase: "),ret ? "Success" : "ERROR"); + #endif + } else{ + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("[ERROR] Could not start SPIFFS")); + #endif + } return ret; } #else (void)opt; #endif - DEBUG_WM("Erasing WiFi Config"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("Erasing WiFi Config")); + #endif return WiFi_eraseConfig(); } @@ -2055,7 +2374,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) @@ -2068,7 +2389,9 @@ void WiFiManager::resetSettings() { WiFi.disconnect(true); WiFi.persistent(false); #endif + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("SETTINGS ERASED")); + #endif } // SETTERS @@ -2293,7 +2616,11 @@ void WiFiManager::setConfigPortalBlocking(boolean shoudlBlock) { */ void WiFiManager::setRestorePersistent(boolean persistent) { _userpersistent = persistent; - if(!persistent) DEBUG_WM(F("persistent is off")); + if(!persistent){ + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("persistent is off")); + #endif + } } /** @@ -2458,7 +2785,9 @@ void WiFiManager::setShowInfoUpdate(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++){ @@ -2468,7 +2797,9 @@ void WiFiManager::setMenu(const char * menu[], uint8_t size){ } } } + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(getMenuOut()); + #endif } /** @@ -2481,7 +2812,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++){ @@ -2491,7 +2824,9 @@ void WiFiManager::setMenu(std::vector& menu){ } } } + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(getMenuOut()); + #endif } @@ -2668,6 +3003,7 @@ 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); @@ -2677,11 +3013,14 @@ void WiFiManager::debugSoftAPConfig(){ 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 + #if !defined(WM_NOCOUNTRY) + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("country: "),(String)country.cc); + #endif DEBUG_WM(F("beacon_interval: "),(String)config.beacon_interval + "(ms)"); DEBUG_WM(FPSTR(D_HR)); + #endif } /** @@ -2692,20 +3031,26 @@ void WiFiManager::debugSoftAPConfig(){ void WiFiManager::debugPlatformInfo(){ #ifdef ESP8266 system_print_meminfo(); + #ifdef WM_DEBUG_LEVEL DEBUG_WM(F("getCoreVersion(): "),ESP.getCoreVersion()); DEBUG_WM(F("system_get_sdk_version(): "),system_get_sdk_version()); DEBUG_WM(F("system_get_boot_version():"),system_get_boot_version()); DEBUG_WM(F("getFreeHeap(): "),(String)ESP.getFreeHeap()); + #endif #elif defined(ESP32) - DEBUG_WM("Free heap: ", ESP.getFreeHeap()); - DEBUG_WM("ESP SDK version: ", ESP.getSdkVersion()); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(F("Free heap: "), ESP.getFreeHeap()); + DEBUG_WM(F("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); // DEBUG_WM("Chip Info: Cores: ",chipInfo.cores); // DEBUG_WM("Chip Info: Rev: ",chipInfo.revision); // DEBUG_WM(printf("Chip Info: Model: %d, cores: %d, revision: %d", chipInfo.model.c_str(), chipInfo.cores, chipInfo.revision)); // DEBUG_WM("Chip Rev: ",(String)ESP.getChipRevision()); + #endif // core version is not avail #endif } @@ -2749,12 +3094,16 @@ 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")); DEBUG_WM(_apPassword); + #endif } return true; } @@ -2789,7 +3138,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]; } @@ -2807,18 +3158,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); - else DEBUG_WM(DEBUG_ERROR,"[ERROR] country code not found"); + #ifdef WM_DEBUG_LEVEL + else DEBUG_WM(DEBUG_ERROR,F("[ERROR] country code not found")); + #endif #endif - if(ret) DEBUG_WM(DEBUG_VERBOSE,"esp_wifi_set_country: " + _wificountry); - else DEBUG_WM(DEBUG_ERROR,"[ERROR] esp_wifi_set_country failed"); + #ifdef WM_DEBUG_LEVEL + if(ret) DEBUG_WM(DEBUG_VERBOSE,F("esp_wifi_set_country: "),_wificountry); + #endif + #ifdef WM_DEBUG_LEVEL + else DEBUG_WM(DEBUG_ERROR,F("[ERROR] esp_wifi_set_country failed")); + #endif return ret; } @@ -2850,14 +3209,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; @@ -2865,7 +3228,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(); @@ -2875,7 +3240,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 { @@ -2992,19 +3359,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"); - Serial.println("[ERROR] wm not ready"); + #endif + Serial.println(F("[ERROR] wm not ready")); return; } + #ifdef WM_DEBUG_LEVEL // DEBUG_WM(DEBUG_VERBOSE,"[EVENT]",event); + #endif if(event == SYSTEM_EVENT_STA_DISCONNECTED){ - DEBUG_WM(DEBUG_VERBOSE,"[EVENT] WIFI_REASON:",info.disconnected.reason); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("[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(); - if(info.disconnected.reason == WIFI_REASON_NO_AP_FOUND) DEBUG_WM(DEBUG_VERBOSE,"[EVENT] WIFI_REASON: NO_AP_FOUND"); + #ifdef WM_DEBUG_LEVEL + if(info.disconnected.reason == WIFI_REASON_NO_AP_FOUND) DEBUG_WM(DEBUG_VERBOSE,F("[EVENT] WIFI_REASON: NO_AP_FOUND")); + #endif #ifdef esp32autoreconnect - DEBUG_WM(DEBUG_VERBOSE,"[Event] SYSTEM_EVENT_STA_DISCONNECTED, reconnecting"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("[Event] SYSTEM_EVENT_STA_DISCONNECTED, reconnecting")); + #endif WiFi.reconnect(); #endif } @@ -3021,7 +3398,9 @@ void WiFiManager::WiFi_autoReconnect(){ #elif defined(ESP32) // if(_wifiAutoReconnect){ // @todo move to seperate method, used for event listener now - DEBUG_WM(DEBUG_VERBOSE,"ESP32 event handler enabled"); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(DEBUG_VERBOSE,F("ESP32 event handler enabled")); + #endif using namespace std::placeholders; wm_event_id = WiFi.onEvent(std::bind(&WiFiManager::WiFiEvent,this,_1,_2)); // } diff --git a/WiFiManager.h b/WiFiManager.h index 20e68a9..9163730 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -586,6 +586,16 @@ class WiFiManager // build debuglevel support // @todo use DEBUG_ESP_x? + + // Testing debug level memory + #ifndef WM_DEBUG_LEVEL + #define WM_DEBUG_LEVEL DEBUG_DEV + #endif + + #ifdef WM_NODEBUG + #undef WM_DEBUG_LEVEL + #endif + #ifdef WM_DEBUG_LEVEL uint8_t _debugLevel = (uint8_t)WM_DEBUG_LEVEL; #else diff --git a/library.json b/library.json index 96c4164..b642a72 100644 --- a/library.json +++ b/library.json @@ -1,5 +1,5 @@ { - "name": "WifiManager", + "name": "WiFiManager", "keywords": "wifi,wi-fi,esp,esp8266,esp32,espressif8266,espressif32,nodemcu,wemos,arduino", "description": "WiFi Configuration manager with web configuration portal for ESP boards", "authors": @@ -25,5 +25,5 @@ "espressif8266", "espressif32" ], - "version": "2.0.3-alpha" + "version": "2.0.4-beta" } diff --git a/library.properties b/library.properties index 9ac084b..31bd6c7 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=WiFiManager -version=2.0.3-alpha +version=2.0.4-beta author=tzapu,tablatronix maintainer=tablatronix sentence=WiFi Configuration manager with web configuration portal for ESP boards diff --git a/strings_en.h b/strings_en.h index 83c665d..8fdd87d 100644 --- a/strings_en.h +++ b/strings_en.h @@ -101,6 +101,7 @@ const char HTTP_STYLE[] PROGMEM = ""; +#ifndef WM_NOHELP const char HTTP_HELP[] PROGMEM = "

Available Pages


" "" @@ -127,6 +128,9 @@ const char HTTP_HELP[] PROGMEM = "" "
Erase 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."; +#else +const char HTTP_HELP[] PROGMEM = ""; +#endif const char HTTP_UPDATE[] PROGMEM = "Upload New Firmware

* May not function inside captive portal, Open in browser http://192.168.4.1"; const char HTTP_UPDATE_FAIL[] PROGMEM = "
Update Failed!
Reboot device and try again
";