tzapu / WiFiManager

ESP8266 WiFi Connection manager with web captive portal
http://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/
MIT License
6.6k stars 1.98k forks source link

Autoconect non blocking problem #1305

Open sebosfato opened 3 years ago

sebosfato commented 3 years ago

i identified a little issue with the autoconect using along with nonblocking config portal...

it work pretty well if the internet source (modem router.. ) is on before the esp try to connect...

it even will never lost connection even if the modem restart (because if it first connected well it will try to reconnect forever (i think)
however if it takes too long for the modem to restart after a power failure the lib is doing the following

First it attempt to connect and it fails

after setconnect timeout it enter into config portal

after config portal set time out it simply dont try to reconnect again...

it may be simple to solve, maybe calling autoconect again if certain condition is reported by the wifimanager?

im going to put the relevant code im using here

im not sure how to check if the autoconnect is connected =( so i could try to add a way to handle this...

Did you already had this problem? is there any simple solution?

i tried to add wm.process() to the loop too no work

[

if (wm.getWiFiIsSaved() == 1) { Serial.println("I have a saved network"); Serial.println("set time out"); wm.setConfigPortalBlocking(false); wm.setConfigPortalTimeout(10); wm.setConnectTimeout(10); wm.setAPStaticIPConfig(apIP, apIP, netMsk); wm.autoConnect("Portal"); }

WiFi.softAPConfig(apIP, apIP, netMsk); delay(500); WiFi.softAP(ssid,password,6,hiden);

](url)

sebosfato commented 3 years ago

Im still trying here...

i came up with the following for now but i would have two questions..

First is how to stop autoconect if it fail so i can start an access point instead...

I ask this because it get stuck in trying to connect and i cannot get to my webserver

if im using the wm.setConfigPortalBlocking(false);

i tried to use wm.stopWebPortal() wm.disconnect();

and many different configurations...

one success is to run the config portal first time if nowifiissaved this makes it user friendly so the first uses until save wifi and something in memory it will run the captive config portal once before start...

like this: [ if (wm.getWiFiIsSaved()==0 && memoria==false) {

 wm.setTimeout(300);//5min

// //Serial.println("Button Pressed, Starting Config Portal"); // wm.setConfigPortalBlocking(true); wm.setBreakAfterConfig(true); wm.setWiFiAPChannel(7); wm.startConfigPortal("Portal"); wm.stopConfigPortal();

ESP.restart();

}

]

sebosfato commented 3 years ago

But if wifi is saved and it get no connection for example my goal would best be to provide the AP so i can access the server and after some minutes try autoconect() again...

im not very experienced so i hope you may have a hint how can i do it?

im trying this if (WiFi.status() != WL_CONNECTED){ wm.disconnect();

//WiFi.disconnect(true); //wm.startWebPortal(); //WiFi.mode(WIFI_STA); //WiFi.softAP(ssid,password,6,hiden); //WiFi.softAPConfig(apIP, apIP, netMsk); // delay(500);

//IPAddress myIP = WiFi.softAPIP(); // Serial.print("AP IP address: "); // Serial.println(myIP);

wm.stopWebPortal(); //wm.disconnect(); //WiFi.disconnect(true); delay(500); WiFi.mode(WIFI_AP_STA); WiFi.softAP(ssid,password,6,hiden); WiFi.softAPConfig(apIP, apIP, netMsk); delay(500); WiFi.begin(); // Serial.print("wm.getConfigPortalActive()"); //Serial.println(wm.getConfigPortalActive()); //Serial.print("wm.getWebPortalActive()") ; //Serial.println(wm.getWebPortalActive()) ; }

sebosfato commented 3 years ago

I guess i solved by calling the webportal on demand by setting a timer for if its not connected (if local ip is == IPUnset) than it send a variable to execute like if were pressing the button

it supose to wake the config portal but is doing what i need.

i post the code later

sebosfato commented 3 years ago

the only problem now is that the SSID of the start web portal seems limited to the ESP74847 like instead of custom name the startwebportal dont seem to allow seting a name for the ap

tablatronix commented 3 years ago

Esp8266 or esp32?

sebosfato commented 3 years ago

Hi tablatronix its ESP8266

I managed to improove the usability but i still missing how to set the country to BR it only allow me to set it to US JP orCN i even tried to substitute on wifimanager.cpp the JP for BR to see if it worked hhaaha not

Setting to BR sometimes apear as X2 as CN or BR on my mac wifi info

Can you help me how to set the BR?

alrerady tried // wifi.setcountry({country="BR", start_ch=1, end_ch=11, policy=wifi.COUNTRY_AUTO}); no wifi declared or something...

here is what im doing

setup 👍 wm.setCountry("BR");

if (wm.getWiFiIsSaved()==1) {

Serial.println("I have a saved network");
wm.setConnectTimeout(30);
wm.setConfigPortalTimeout(60);  ///se entrou no portal é pq nao esta conseguindo conectar
Serial.println("set time out");

Serial.println("block config portal false"); wm.setWiFiAPChannel(7); wm.autoConnect("Portal"); }

if ((wm.getWiFiIsSaved()==0) && (memoria == false)) {

 wm.setConfigPortalTimeout(180);
wm.setConfigPortalBlocking(true);

wm.setWiFiAPChannel(7); wm.startConfigPortal("Portal"); wm.stopWebPortal(); wm.stopConfigPortal(); notconect=true;

if (wm.getWiFiIsSaved()==0) wm.disconnect(); delay(500);

}

WiFi.softAPConfig(apIP, apIP, netMsk); WiFi.softAP(ssid, password, 7, hiden);

////here i can call the config portal from the AP from a link server.on("/WIFICONFIG", HTTP_GET, []() { server.send(205);//, "text/plain", ""+versao); //205 serve para nao redirecionar WiFi.mode(WIFI_STA); dnsServer.stop(); server.stop(); if (startAP) { wm.setTimeout(180); //Serial.println("Button Pressed, Starting Config Portal"); wm.setConfigPortalBlocking(true); wm.setBreakAfterConfig(true); wm.setWiFiAPChannel(7); wm.startConfigPortal("Portal"); ESP.restart(); } } );

void doWiFiManager() {

// is auto timeout portal running if (portalRunning) { wm.process(); if ((millis() - startTime) > (timeout * 1000)) { Serial.println("portaltimeout"); portalRunning = false; if (startAP) { wm.stopConfigPortal(); } else { // wm.stopWebPortal(); ////// disabled the stop webportal if disable this timeout it gives a ERROR 28 } } }

// is configuration portal requested?

if(notconect == true && (!portalRunning)) { notconect == false; if(startAP){ WiFi.softAP(ssid, password, 6, hiden); Serial.println("Starting Config Portal"); wm.setConfigPortalBlocking(false); wm.startConfigPortal("AP"); }
else{ WiFi.softAP(ssid, password, 6, hiden); Serial.println("Starting Web Portal"); wm.startWebPortal(); }
portalRunning = true; startTime = millis(); }

}

//here is a scheduler every 65 seconds repeat //this is what saved the day as it kind of detect the wifi is not connected and try if anything blocked void t1Callback() { // programa Task t1(65000, TASK_FOREVER, &t1Callback);

wifiatempt++; // if((iplocal.toString()=="(IP unset)") &&(wm.getWiFiIsSaved() == 1) && wifiatempt>1){ if (wm.getWiFiIsSaved() == 0) {

}else{ if(((iplocal.toString()=="(IP unset)") || !wm.autoConnect()) && wifiatempt>1){ Serial.println("IP unset ou !autoconect tendo wifi salvo e passado 130segundos "); Serial.print("autoconectagain"); //Serial.println(wm.autoConnect());

notconect=true; wifiatempt=0; //reconecttt();

}else{ notconect=false; }} // //Serial.println(timeClient.getSeconds()); }

tablatronix commented 3 years ago

Esp8266 is supposed to autoconnect so this is odd are you setting wifi mode sta?

sebosfato commented 3 years ago

i set it sta in the setup

it connects automatically but if the modem takes too long to setup the wifi connection i was getting it to hang on the portal non blocking... so i changed to blocking and so if didnt connected and have wifi saved it will keep trying every two minutes few times.....

i did all this to be able to let the ap available if it didnt connected, but still keep trying.. it seems to be working perfectly this way... not sure behind the scene what is happening however...

sebosfato commented 3 years ago

HEy i came up with another question, have you ever managed to come up with a solution for opening a link direct from the captive portal to a google chrome or defaut browser?

Captive portal seems to not handle javascript very well for some reason... so i would like to redirect where it should work...

window prompts dont work on captive and at the mac is little worst..

tablatronix commented 3 years ago

Yeah someone said there was a bug in non blocking that timeout doesn't work.. there is an open issue I think

19eighties commented 2 years ago

Is this issue still open? (non-blocking mode) If I shut off the wifi radio on my router and turn it back on, the ESP8266 won't reconnect. Also, if the wifi radio is off before and after the initial setup and then I turn the radio on, the ESP8266 doesn't connect while in the main loop. Basically simulating power loss to router and the delay it takes to get situated or if the router's radio temporarily goes down for whatever reason.

WMTaylor2Degrees commented 3 days ago

Sorry to pull this from the depths of time. I'm seeing the same issue with an ESP32 while using the non-blocking code. If WiFi AP turns off and then on again, the ESP32 won't attempt to reconnect to the WiFi once it's in portal mode.

@tablatronix (love your work here btw) you mentioned ESP8266 is supposed to auto reconnect, is there something extra that needs doing for ESP32? I've attempted to manually call WiFi.reconnect() every 10 seconds while in the loop, but it doesn't appear to have helped.

I've attached a sample of code below. Anyones thoughts would be appreciated:

/*
* Initializes the WiFi on the ESP. Attempts to connect using saved WiFi name and password.
* If connection fails, a fallback hotspot is spun up, including a configuration web portal.
* This method can also be used to attempt WiFi reconnections.
*/
void InitializeWiFi() {
  DEBUG_SERIAL.println("Initializing WiFi");
  WriteToLCD("WiFi connecting");
  WiFi.mode(WIFI_STA);

  // Attempt connection using stored WiFi configuration.
  // This allows us to resolve connection drops without invoking WiFiManager.
  WiFi.reconnect();
  elapsedMillis autoConnectMillis = 0;
  while (autoConnectMillis < WIFI_RECONN_TIMEOUT * 1000) {
    if (IsWiFiConnected()) {
      DEBUG_SERIAL.println("WiFi connected!");
      WriteToLCD("WiFi connected!");
      return;
    }
    delay(100);
  }

  // WiFi auto-connection wasn't successful. Spin up portal for config.
  DEBUG_SERIAL.println("WiFi connection failed");
  WriteToLCD("Automatic WiFi", "reconnect failed");
  delay(3000);

  DEBUG_SERIAL.println("Invoking WiFi configuration portal");
  WriteToLCD("Generating WiFi", "config portal");
  delay(3000);

  // Generate an instance of WiFi Manager to build the portal and handle reconnect.
  WiFiManager wifiManager;
  wifiManager.setConfigPortalBlocking(false);
  wifiManager.autoConnect("ESP-CX-CTR", SECRET_WIFI_PASSWORD); // This will actually attempt reconnection one more time. It's possible to use .startConfigPortal() but the non-blocking code is a lot more complex.

  char passwordText[LCD_COLUMNS + 1];                          // Create a char[] to build password text. C does not support string concatenation for string literals directly.
  sprintf(passwordText, "Pass: %s", SECRET_WIFI_PASSWORD);     // Build the text to be displayed on the LCD and store in the char[].

  // Non-blocking WiFi Manager instance to allow us to refresh the display while the portal is active.
  autoConnectMillis = 0;
  while (WiFi.status() != WL_CONNECTED) {
    wifiManager.process();

    WriteToLCD("Connect to the", "following WiFi:");
    delay(2000);
    WriteToLCD("Name: ESP-CX-CTR", passwordText);
    delay(5000);

    WriteToLCD("Then visit the", "following site:");
    delay(2000);
    WriteToLCD("URL:", "192.168.4.1");
    delay(4000);

    // Periodically re-attempt connection to the saved WiFi... Just in case.
    if (autoConnectMillis > WIFI_RECONN_TIMEOUT * 1000) {
      WiFi.reconnect();
      autoConnectMillis = 0;
    }
  }

  DEBUG_SERIAL.println("WiFi connected!");
  WriteToLCD("WiFi connected!");
  DEBUG_SERIAL.println("WiFi initialized");
}