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();
}