BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Blynk app not recover from power failure

#1

i have a NodeMcu ESP8266 running great… except there is this one moment in time I think it failed to recover… a short power failure in the building…

when the power comes back on, the WIFI network is not yet back up when the Arduino wifi api wants to connect… I ‘thought’ I had fixed that and let it fall thru config (1st time with no wifi defined) and use the saved ssid/and password…

but I have lost contact (and won’t see it til tomorrow) when I could update the code to handle this better

any advice welcomed… here is my setup

void setup() {
  ArduinoOTA.onError([](ota_error_t error) {
    ESP.restart();
  });       //added for OTA
  ArduinoOTA.begin();

  //**    can be commented out, test only
  Serial.begin(115200);                                               // Open serial console.
  //*******************************
  pulseCount        = 0;
  flowRate          = 0.0;
  lastTime           = 0;

// if an SSID was previously stored from autoconnect config
 if(WiFi.SSID())
 {
    while(1){
      printmsg("attempting to connect to saved wifi");
      if(WiFi.begin()==WL_CONNECTED){
        printmsg("connected to save wifi ok");
        break;
      }
      printmsg("connect to saved wifi sleeping");  
      delay(1*60*1000);
    }
    printmsg("connect to saved wifi completed");
 }
 else{
   
        WiFiManager *wifi = new WiFiManager();
        if( wifi->autoConnect(Name)){
           // if we connected ok,
           printmsg("autoconnect ok");
        }
  }
  Blynk.config(auth);     // should use stored info
  Blynk.connect();   // and start up

  // connected, start timers now
  timer.setInterval(Period, sendSensorReadings);      // Setup a function to be called every 'n' seconds
  timer.setInterval(watchFlow, flowTimer);

  delay(10);
  // The Hall-effect sensors are connected 
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  attachInterrupt(flowInterrupt, flowCounter, FALLING);

  // this allows setup() to continue/exit and the main loop() to begin without blocking
  timer.setTimeout(2000L, checkBlynkConnection); // in  2 seconds, do initial connection check

  sonar[0] = NewPing(PingPin1, EchoPin1, MAX_DISTANCE);

  delay(20);
  OurIP=(String)WiFi.localIP().toString();
  printmsg(OurIP);                    //this is local IP for this board
  printmsg("WiFi connected");

}
void checkBlynkConnection()
{
  //printmsg("\n WiFi status="+WiFi.status());
  if (WiFi.status() == 3) {
    if (!Blynk.connected()) {
      printmsg("\nWiFi OK, trying to connect to the server...");
      Blynk.connect();
      if (!Blynk.connected()) {
        printmsg("\nreconnect failed");
      }
    }
  }
  if (WiFi.status() == 1) {
    printmsg("No WiFi connection, going offline.");
  }
}
void loop() {

  // only attempt Blynk-related functions when connected to Blynk
  if (!Blynk.connected())
  {
    Blynk.connect();
  }
  Blynk.run();
  timer.run();
  ArduinoOTA.handle();
}
0 Likes

#2

perhaps the 120s timeout is not long enough…
or dont let it timeout at all, have it keep trying until it gets online

0 Likes

#3

sadly if it decides to go to config mode (have user connect to its the special wifi access point to get wifi and wifi credentials ), it will never exit that mode on its own… so the timeout…

i can’t tell if its stuck there yet…

1 Like

#4

ah yes, right. So it seems longer timeout is needed then.

This reminds me that I have not yet tested power outage and router stack for all my devices…

0 Likes

#5

I am thinking I need a retry loop… just don’t understand how to get out of it…
(or a UPS so it doesn’t power off, but the network will still go away for a while)

1 Like

#6

i found the WifiManger samples on github, and it has an AutoConnect timeout example

0 Likes

#7

well, i tried out the autoconnect() timeout loop, and it fails… on the second iteration it doesn’t stop for the auto config response…

once in Access Point mode, the only way out is either to do the config, OR reboot…
reboot didn’t work for me…

so, I changed it to check if some config was previously completed (ssid stored) and it so, then loop trying to WiFi.begin() as client mode… this worked… waited an hour, then brought network back up and it connected nearly immediately (1 minute waits between attempts)…

I changed the code above to show that solution

0 Likes