[GH-ISSUE #840] What if cannot connect to router? #705

Open
opened 2026-02-28 01:26:41 +03:00 by kerem · 9 comments
Owner

Originally created by @panosss on GitHub (Mar 4, 2019).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/840

Hi everybody.
I was studying the code in the AutoConnectWithTimeOut example.

#include           //https://github.com/esp8266/Arduino

//needed for library
#include 
#include 
#include           //https://github.com/tzapu/WiFiManager



void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;
  //reset settings - for testing
  //wifiManager.resetSettings();

  //sets timeout until configuration portal gets turned off
  //useful to make it all retry or go to sleep
  //in seconds
  wifiManager.setTimeout(180);
  
  //fetches ssid and pass and tries to connect
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP"
  //and goes into a blocking loop awaiting configuration
  if(!wifiManager.autoConnect("AutoConnectAP")) {
    Serial.println("failed to connect and hit timeout");
    delay(3000);
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(5000);
  } 

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");
 
}

void loop() {
  // put your main code here, to run repeatedly:

}

Let's say that my router has no power.
So my ESP82266 cannot connect to internet and the code gets stuck forever at:

 if(!wifiManager.autoConnect

Right?
So how can I make it go to the code in the main loop in this case?

What I want is this:
-at setup, try to connect to wifi
-if succeeds ok no problem If fails, again no problem, BUT don't get stucked here, continue to main loop and.
-while at main loop, try at sometime to reconnect to wifi

Originally created by @panosss on GitHub (Mar 4, 2019). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/840 Hi everybody. I was studying the code in the AutoConnectWithTimeOut example. <pre>#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino //needed for library #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager void setup() { // put your setup code here, to run once: Serial.begin(115200); //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; //reset settings - for testing //wifiManager.resetSettings(); //sets timeout until configuration portal gets turned off //useful to make it all retry or go to sleep //in seconds wifiManager.setTimeout(180); //fetches ssid and pass and tries to connect //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" //and goes into a blocking loop awaiting configuration if(!wifiManager.autoConnect("AutoConnectAP")) { Serial.println("failed to connect and hit timeout"); delay(3000); //reset and try again, or maybe put it to deep sleep ESP.reset(); delay(5000); } //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); } void loop() { // put your main code here, to run repeatedly: }</pre> Let's say that my router has no power. So my ESP82266 cannot connect to internet and the code gets stuck forever at: <pre> if(!wifiManager.autoConnect</pre> Right? So how can I make it go to the code in the main loop in this case? What I want is this: -at setup, try to connect to wifi -if succeeds ok no problem If fails, again no problem, BUT don't get stucked here, continue to main loop and. -while at main loop, try at sometime to reconnect to wifi
Author
Owner

@tablatronix commented on GitHub (Mar 4, 2019):

setConfigPortalTimeout

<!-- gh-comment-id:469453836 --> @tablatronix commented on GitHub (Mar 4, 2019): setConfigPortalTimeout
Author
Owner

@panosss commented on GitHub (Mar 5, 2019):

I suppose you mean this?

From what I see, the user has to press a button for the ESP to try to connect to the WiFi, right?
This means that it cannot be done automatically but only by the intervention of the user?

<!-- gh-comment-id:469580543 --> @panosss commented on GitHub (Mar 5, 2019): I suppose you mean [this](https://github.com/tzapu/WiFiManager/blob/master/examples/OnDemandConfigPortal/OnDemandConfigPortal.ino)? From what I see, the user has to press a button for the ESP to try to connect to the WiFi, right? This means that it cannot be done automatically but only by the intervention of the user?
Author
Owner

@tablatronix commented on GitHub (Mar 5, 2019):

No , its in the docs.
https://github.com/tzapu/WiFiManager#configuration-portal-timeout

You do not have to deal with reconnect, esp does it automatically, just check for connected now and then , or startConfigPortal on demand

<!-- gh-comment-id:469700171 --> @tablatronix commented on GitHub (Mar 5, 2019): No , its in the docs. https://github.com/tzapu/WiFiManager#configuration-portal-timeout You do not have to deal with reconnect, esp does it automatically, just check for connected now and then , or startConfigPortal on demand
Author
Owner

@panosss commented on GitHub (Mar 5, 2019):

How about this code? It tries to connect for 3 minutes, and if doesn't succeed just proceeds to main loop????

#include           //https://github.com/esp8266/Arduino

//needed for library
#include 
#include 
#include           //https://github.com/tzapu/WiFiManager



void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  WiFiManager wifiManager;   
   
  wifiManager.setConfigPortalTimeout(180);
 
  if(!wifiManager.autoConnect("AutoConnectAP")) {
    Serial.println("failed to connect and hit timeout");
    delay(3000);
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(5000);
  } 

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");
}

void loop() {
  // put your main code here, to run repeatedly:

}
<!-- gh-comment-id:469721226 --> @panosss commented on GitHub (Mar 5, 2019): How about this code? It tries to connect for 3 minutes, and if doesn't succeed just proceeds to main loop???? <pre> #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino //needed for library #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager void setup() { // put your setup code here, to run once: Serial.begin(115200); WiFiManager wifiManager; wifiManager.setConfigPortalTimeout(180); if(!wifiManager.autoConnect("AutoConnectAP")) { Serial.println("failed to connect and hit timeout"); delay(3000); //reset and try again, or maybe put it to deep sleep ESP.reset(); delay(5000); } //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); } void loop() { // put your main code here, to run repeatedly: } </pre>
Author
Owner

@tablatronix commented on GitHub (Mar 5, 2019):

That will start the configportal and timeout after 180 seconds
if you want to try to connect for 3 minutes use setConnectTimeout

   //sets timeout before AP,webserver loop ends and exits even if there has been no setup.
    //useful for devices that failed to connect at some point and got stuck in a webserver loop
    //in seconds setConfigPortalTimeout is a new name for setTimeout, ! not used if setConfigPortalBlocking
    void          setConfigPortalTimeout(unsigned long seconds);

    //sets timeout for which to attempt connecting, useful if you get a lot of failed connects
    void          setConnectTimeout(unsigned long seconds);

autoconnect->connecttwifitimer->configportalloop( no reconnection occurs here, atm )

<!-- gh-comment-id:469797218 --> @tablatronix commented on GitHub (Mar 5, 2019): That will start the configportal and timeout after 180 seconds if you want to try to connect for 3 minutes use setConnectTimeout ```C++ //sets timeout before AP,webserver loop ends and exits even if there has been no setup. //useful for devices that failed to connect at some point and got stuck in a webserver loop //in seconds setConfigPortalTimeout is a new name for setTimeout, ! not used if setConfigPortalBlocking void setConfigPortalTimeout(unsigned long seconds); //sets timeout for which to attempt connecting, useful if you get a lot of failed connects void setConnectTimeout(unsigned long seconds); ``` autoconnect->connecttwifitimer->configportalloop( no reconnection occurs here, atm )
Author
Owner

@panosss commented on GitHub (Mar 5, 2019):

No, I don't want to try to connect for 3 minutes.
I just descried what I thought it does. I was wrong.

So, my program now is completely ok?
If it does not connect at setup, it will, automatically, try to connect sometime at the main loop?

<!-- gh-comment-id:469857107 --> @panosss commented on GitHub (Mar 5, 2019): No, I don't want to try to connect for 3 minutes. I just descried what I thought it does. I was wrong. So, my program now is completely ok? If it does not connect at setup, it will, automatically, try to connect sometime at the main loop?
Author
Owner

@tablatronix commented on GitHub (Mar 5, 2019):

well no because you have
ESP.reset();

<!-- gh-comment-id:469882784 --> @tablatronix commented on GitHub (Mar 5, 2019): well no because you have ESP.reset();
Author
Owner

@dontsovcmc commented on GitHub (Mar 27, 2019):

@tablatronix my way is right or I can use setConnectTimeout?

#define ESP_CONNECT_TIMEOUT 5000  //5 sec
WiFi.mode(WIFI_STA);
WiFi.begin(); 

uint32_t start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < ESP_CONNECT_TIMEOUT) {
    delay(200);
}

if (WiFi.status() == WL_CONNECTED) {
	//do smth
}

<!-- gh-comment-id:476930149 --> @dontsovcmc commented on GitHub (Mar 27, 2019): @tablatronix my way is right or I can use setConnectTimeout? ``` #define ESP_CONNECT_TIMEOUT 5000 //5 sec WiFi.mode(WIFI_STA); WiFi.begin(); uint32_t start = millis(); while (WiFi.status() != WL_CONNECTED && millis() - start < ESP_CONNECT_TIMEOUT) { delay(200); } if (WiFi.status() == WL_CONNECTED) { //do smth } ```
Author
Owner

@tablatronix commented on GitHub (Mar 27, 2019):

Both should work

<!-- gh-comment-id:477378519 --> @tablatronix commented on GitHub (Mar 27, 2019): Both should work
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#705
No description provided.