1

I am using an ESP 12e on a MQTT system. When it first starts up I have wifiManager open an access point so the user can put in the wifi, password, MQTT name and Key. This all works fine.
However every once in a while something happens (like an electrical interrupt) and the ESP 12e starts up and looks for the wifi to connect to but because of the power outage the wifi isn't up and running yet so it starts the access point. If this occurs while I am away the ESP 12e just sits there in access mode waiting for someone to enter the data until I return home and restart the ESP 12e manually while the wifi is broadcasting. I am trying to program the ESP 12e to restart after a certain amount of time (6 minutes) when the wifi has had time to start broadcasting again.

I am using Arduino IDE.

  void setup() {
    Serial.begin(57600);
    
    //NEW AUTOCONNECT
      WiFiManager wifiManager;
        // I added three extra parameters.
    WiFiManagerParameter customAPIKey("authorizationKey", "Authorization Code",authorizationKey, 32);
    WiFiManagerParameter customAPIKey2("apiKey", "Time Zone #", apiKey, 32);
    WiFiManagerParameter customAPIKey3("userNameKey", "User Name",userNameKey, 32);
    
    wifiManager.addParameter(&customAPIKey);
        wifiManager.addParameter(&customAPIKey2);
         wifiManager.addParameter(&customAPIKey3);
        
    //this is where I put your code
    wifiManager.setTimeout(120); // 2 minutes
      do {
        WiFi.begin();
        wifiManager.autoConnect("LEVEL1");
      } while (WiFi.status() != WL_CONNECTED);
        
     //I get the data captured by wifiManager AP

    strcpy(authorizationKey, customAPIKey.getValue());
      strcpy(apiKey, customAPIKey2.getValue());
      strcpy(userNameKey, customAPIKey3.getValue());
    
    
       EEPROM.begin(512);  //Initialize EEPROM
       String rrr = apiKey;
//If there is no data input I wait for data to be input to AP
     if (rrr.length() == 0){
        Serial.print ("empty");
           //Store data to EEPROM
      }
//If there is input data I write it to EEPROM
      else {
        Serial.print ("Not Empty");
        
      String uuu = authorizationKey;
      String www = apiKey;
      String yyy = userNameKey;
      String fff = String(www)+String(",");
      String vvv = String(yyy)+String(",");
      for(int i=0;i<uuu.length();i++) //loop upto string lenght www.length() returns length of string
      {
        EEPROM.write(0x06+i,uuu[i]); //Write one by one with starting address of 0x0F
      }
      for(int i=0;i<fff.length();i++) //loop upto string lenght www.length() returns length of string
      {
        EEPROM.write(0x50+i,fff[i]); //Write one by one with starting address of 0x0F
      }
      for(int i=0;i<vvv.length();i++) //loop upto string lenght www.length() returns length of string
      {
        EEPROM.write(0x90+i,vvv[i]); //Write one by one with starting address of 0x0F
      }
      
        EEPROM.commit(); 
      }
    
      delay (2000);
     
  
   //If the ESP 12e connects using the wifiManager stored name and password I retrieve the data from EEPROM and used it to connect to Adafruit.   
     if (WiFi.status() == WL_CONNECTED) {  Serial.println("Connected1");
    delay(1000);

I start up the ESP 12e and the AP pops up. I fill in the wifi.name and password and the other added parameters and save them. It then connects to the wifi and adafruit.

If I unplug the ESP and the router and then launch the ESP 12e it tries to use the last saved values but since there is no wifi it starts the AP. If I wait two minutes the serial monitors shows that the ESP 12e tries to reconnect with the saved values but can't. Then I restart the router and the wifi come back. The next time the ESP 12e times out it tries to reconnect using saved values but it doesn't and it starts the AP again. Over and over again.

Somewhere I have done something wrong. Could you suggest something.

2 Answers 2

0

In my project I solved it this way:

  wm.setTimeout(120); // 2 minutes
  do {
    WiFi.begin();
    wm.autoConnect(PROJECT_NAME);
  } while (WiFi.status() != WL_CONNECTED);

I set timeout for WiFiManager, then I directly invoke WiFi.begin, to repeatedly try to join the last AP. If the WiFiManager times out, and WiFi is not connected I repeat it. This way once the AP booted the sketch connects to it.

This uses that WiFiManager stores the WiFi credentials using a way supported by the SDK so WiFi.begin() without parameters will try to join the last stored AP set by WiFiManager.

Sign up to request clarification or add additional context in comments.

9 Comments

I tried putting your code instead of mine and ran it. However there is no way to tell if it is recycling. I put Serial.print("restart"); but the serial monitor doesn't show that it is relaunching the access point. How can I know for sure it is restarting?
isn't it after 2 minutes able to connect to WiFi? WiFiManager will not start the AP if WiFi is connected
I added your code and started the ESP 12e up. The access point showed up and I input the wifi name and password and it connected and the access code went away. Then I turned off the ESP 12e and my router. I restarted the ESP 12e and it tried to connect but couldn't so it launched the access point. I then waited a couple minutes and stated my router back up. The wifi came back but the ESP 12e stayed in access point mode and never reconnected to the wifi using the stored name and password. I will add more code to my original post so you can see what I am doing.
@user1114881 the AP mode should end after 2 minutes with wm.setTimeout(120);
As I said I start the ESP and AP launches. I enter name, password and it is saved. I turn off ESP and Router. Relaunch ESP and since no wifi starts AP. After two minutes AP ends and ESP tries to connect using saved name, password. Wifi not available so AP mode. Turn on router and wifi comes back. After 2 minutes AP ends and ESP tries to connect using saved name, password. Since wifi is available it should connect but doesn't. So it goes into AP mode again. Continues this never connecting even though wifi is available and name, password are saved. Don't understand why not.
|
0

This took a bit of work but I got it working.

First I found out it I had stored some data in EEPROM

EEPROM.begin(512);
//I search a section
  String uuuu;
  for(int i=0;i<32;i++)
  {uuuu =  uuuu + char(EEPROM.read(0x50+i));
  } 
//and see if it has a coma, if it does I have saved to EEPROM
  int firstCommaIndex = uuuu.indexOf(',');
   
if (firstCommaIndexa >0) {
    Serial.print ("comma present");
    delay (30000);//I wait to see if it can connect using stored data

//if it doesn't connect there is no wifi modem and router is rebooting

 if((WiFi.status()!= WL_CONNECTED) &(firstCommaIndexa >0)){
      Serial.println("IP address: "); Serial.println(WiFi.localIP());

delay(1000);

Serial.printf("SSID: %s\n", WiFi.SSID().c_str());
Serial.printf("SSID: %s\n", WiFi.psk().c_str());
ssidString = WiFi.SSID().c_str();
pskString = WiFi.psk().c_str();
WiFi.begin(ssidString, pskString);
      delay (30000);
//I repeatedly do this for 3 minutes (long enough for modem and router //reboot and connect

//At the end of the 3 minutes I figure something else is the problem like maybe the wifi name and/or password has changed so I give the client a chance to resign in and save the new information to EEPROM.

WiFiManager wifiManager;
    
WiFiManagerParameter customAPIKey("authorizationKey", "Authorization Code",authorizationKey, 32);
WiFiManagerParameter customAPIKey2("apiKey", "Time Zone #", apiKey, 32);
WiFiManagerParameter customAPIKey3("userNameKey", "User Name",userNameKey, 32);

wifiManager.addParameter(&customAPIKey);
    wifiManager.addParameter(&customAPIKey2);
     wifiManager.addParameter(&customAPIKey3);
   
wifiManager.autoConnect("LEVEL1");
   Serial.println("Connected");

//of course if the is no saved data in EEPROM I immediately open the access point to enter wifi name, password and other info and save to EEPROM

else {
    Serial.print ("no comma present");
  
   //Serial.print(userr);
//NEW AUTOCONNECT
  WiFiManager wifiManager;
    // put your setup code here, to run once:
WiFiManagerParameter customAPIKey("authorizationKey", "Authorization Code",authorizationKey, 32);
WiFiManagerParameter customAPIKey2("apiKey", "Time Zone #", apiKey, 32);
WiFiManagerParameter customAPIKey3("userNameKey", "User Name",userNameKey, 32);

wifiManager.addParameter(&customAPIKey);
    wifiManager.addParameter(&customAPIKey2);
     wifiManager.addParameter(&customAPIKey3);
    

  wifiManager.autoConnect("LEVEL1");
   Serial.println("Connected");

strcpy(authorizationKey, customAPIKey.getValue());
  strcpy(apiKey, customAPIKey2.getValue());
  strcpy(userNameKey, customAPIKey3.getValue());


   EEPROM.begin(512);  //Initialize EEPROM
   String rrr = apiKey;
 if (rrr.length() == 0){
    Serial.print ("empty");
       //Store data to EEPROM
  }
  else {
    Serial.print ("Not Empty");
    
  String uuu = authorizationKey;
 Serial.print("uuu");
  Serial.print(uuu);
  String www = apiKey;
  
  Serial.print("www");
  Serial.print(www);
  String yyy = userNameKey;
  String fff = String(www)+String(",");
  String vvv = String(yyy)+String(",");
  Serial.print("vvv");
  Serial.print(vvv);
  for(int i=0;i<uuu.length();i++) //loop upto string lenght www.length() returns length of string
  {
    EEPROM.write(0x06+i,uuu[i]); //Write one by one with starting address of 0x0F
  }
  for(int i=0;i<fff.length();i++) //loop upto string lenght www.length() returns length of string
  {
    EEPROM.write(0x50+i,fff[i]); //Write one by one with starting address of 0x0F
  }
  for(int i=0;i<vvv.length();i++) //loop upto string lenght www.length() returns length of string
  {
    EEPROM.write(0x90+i,vvv[i]); //Write one by one with starting address of 0x0F
  }
  
    EEPROM.commit(); 
  }

  delay (2000);
  
}

Anyway this is how I solved my problem. I hope it helps someone else with a similar dilemma.

Thanks to everyone who responded and helped me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.