Random Device Offline (Clean Loop)

I suspect that the issue lies downstream from the router, probably with your ISP.

Pete.

Itā€™s still running fine without any disconnections, so Iā€™m going to take the Wemos offline because I think weā€™ve proven the point.
Iā€™ll flash the Wemos with a blank sketch so that it doesnā€™t connect next time I plug it in, to avoid any confusion at your end.

Pete.

1 Like

Thank you Pete
I will try a couple of things on my router and if everything fails, Iā€™ll speak to my ISP.

1 Like

A little bit of positive news,
My other D1 mini which was running my main sketch was online for the longest it has been for awhile!
Almost an entire day without a single disconnect. It unfortunately went offline 1 and a half hours ago.

I hope to be free this weekend to have a look at my router settingsā€¦

Edit: The Blynk protocol is non-edgent, port 80 sketchā€¦

Update:
Still disconnecting from Blynk servers and SM output is continous ā€œConnect to blynk.cloud:80ā€

My last idea is to continously check if the board is connected to blynk servers and if it is not, reboot the board.

if(!Blynk.connected())
{
    ESP.restart();
}

This code doesnā€™t seem to work as the board does automatically reboot when the ā€œ(!Blynk.connected()ā€ is metā€¦

What are options :confused:

I donā€™t understand this statement.

I think the bottom line is to establish what the primary cause of the issue is - and I suspect itā€™s your ISP - and fix that problem.

Pete.

Apologies for the typo.

I meant the board does not reboot when the ā€œ(!Blynk.connected())ā€ condition is met. It appears to return ā€œtrueā€ even though itā€™s attempting to reconnect to the server. I read another discussion of the same issue.

I spoke to my ISP, unsurprisingly the technician didnā€™t have a clue what Blynk is and I had to briefly explain it. After a good 10mins of on and off hold, they said there are no restrictions in place for Blynk and any other IoT platforms.
They then suggested the same old restart the router and try againā€¦.

I agree with you Pete about finding the root cause instead of taking a short cut. I am ashamed to come to a point where I still havenā€™t solved this and my last option being to add a line of code to patch the bugā€¦.

Iā€™ve even thought of adding an arduino with a relay to reboot the wemos every ā€œXā€ hours by interrupting the powerā€¦
Iā€™m desperate to keep the device running and online continuously without me having to reset it.

This week I will try connecting the wemos to its own router (only device connected). Hoping for positive resultsā€¦.

You should probably post the code you are using.

It might be worth reading this, then having another chat with the technicianā€¦

Pete.

1 Like

Pete I made an interesting discovery.

I disconnected the fiber cable from the router to simulate a internet blackout and the wemos attempts to connect to blynk.cloud (as expected). However, when I reconnect the fiber cable back to the router and confirm on my phone that the internet connection as been restored, the wemos however still attemps to connect to blynk.cloud.

So what I believe is happening is that theres brief internet blackout, and the wemos gets stuck on a loop trying to establish a connection with the sever, even after the internet has been restored.
So this comfirms that it has nothing to do with my ISP blocking Blynk servers. The wemos is simply unable to reconnect to blynk server on its own after an internet blackout.

Any suggestion on how I can fix this?

Please see attached SM output and code below

At timestamp 17:52:47.361, the board has succefully connected to the server.
At timestamp 18:05:51.544, I unplugged the fiber line to simulate a internet blackout
At timestamp 18:07:03.074, I reconnected the fiber line and comfirmed internet is restored

17:52:47.327 -> 
17:52:47.327 -> [8533] Connecting to blynk.cloud:80
17:52:47.361 -> [8688] Ready (ping: 56ms).
18:05:51.544 -> [792882] Connecting to blynk.cloud:80
18:05:57.562 -> [798884] Connecting to blynk.cloud:8080
18:06:04.660 -> [805988] Connecting to blynk.cloud:80
18:06:10.659 -> [811989] Connecting to blynk.cloud:8080
18:06:17.775 -> [819093] Connecting to blynk.cloud:80
18:06:23.751 -> [825094] Connecting to blynk.cloud:8080
18:06:30.856 -> [832197] Connecting to blynk.cloud:80
18:06:36.875 -> [838199] Connecting to blynk.cloud:8080
18:06:43.977 -> [845302] Connecting to blynk.cloud:80
18:06:49.967 -> [851303] Connecting to blynk.cloud:8080
18:06:57.081 -> [858407] Connecting to blynk.cloud:80
18:07:03.074 -> [864408] Connecting to blynk.cloud:8080
18:07:10.175 -> [871512] Connecting to blynk.cloud:80
18:07:16.188 -> [877513] Connecting to blynk.cloud:8080
18:07:23.293 -> [884617] Connecting to blynk.cloud:80
18:07:29.305 -> [890618] Connecting to blynk.cloud:8080
18:07:36.404 -> [897721] Connecting to blynk.cloud:80
18:07:42.380 -> [903723] Connecting to blynk.cloud:8080
18:07:49.508 -> [910826] Connecting to blynk.cloud:80
18:07:55.506 -> [916828] Connecting to blynk.cloud:8080
18:08:02.606 -> [923931] Connecting to blynk.cloud:80
18:08:08.594 -> [929932] Connecting to blynk.cloud:8080
18:08:15.693 -> [937036] Connecting to blynk.cloud:80
18:08:21.711 -> [943037] Connecting to blynk.cloud:8080
18:08:28.807 -> [950141] Connecting to blynk.cloud:80
18:08:34.818 -> [956142] Connecting to blynk.cloud:8080
18:08:41.904 -> [963246] Connecting to blynk.cloud:80
18:08:47.928 -> [969247] Connecting to blynk.cloud:8080
18:08:55.015 -> [976351] Connecting to blynk.cloud:80
18:09:01.017 -> [982352] Connecting to blynk.cloud:8080

Code

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME "Yamaha EF5500EFW"
#define BLYNK_AUTH_TOKEN ""
#define BLYNK_FIRMWARE_VERSION        "5.5"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define BLYNK_GREEN     "#23C48E"
#define BLYNK_RED       "#D3435C"
#define BLYNK_YELLOW    "#ED9D00"

int CEBsensor = 0;
int CEBstate = 0;
int Timerstate = 0;
int InvTimerstate = 0;
int inverterstate = 0;

//Voltage Sensor
//int Vsensor = A0;
float correctionfactor = 7.40;
float vout = 0.0;
float vin = 0.0;
float fuelcon = 0.0;

//float R1 = 30000;
//float R2 = 7500;
//int value = 0;


//Declare millis for timers
unsigned long msec = 0;
unsigned long msec2 = 0;
float times = 0.0;
float hourmeter = 0.0;

BlynkTimer timer;

//Blynk LED Widgets
WidgetLED led1(V3); //COIL LED
WidgetLED led2(V7); //FUEL LED
WidgetLED led3(V4); //STARTER LED
WidgetLED led13(V13); //GENERATOR SHUTOFF LED
WidgetLED led14(V14); //CEB STATUS LED
WidgetLED led15(V18); //INVERTER STATUS LED
WidgetLED led16(V20); //INVERTER SHUTOFF LED
WidgetLED led17(V24); //CHANGEOVER RELEASE INDICATOR

int newTimer = 1;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

String error;

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  pinMode(D0, OUTPUT);  //STARTER Relay D0
  pinMode(D5, OUTPUT);  //FUEL Relay D5
  pinMode(D6, OUTPUT);  //COIL Relay D6
  pinMode(D7, OUTPUT);  //INVERTER Relay D7
  pinMode(D1, INPUT);   //CEB LDR Sensor D1
  //pinMode(Vsensor, INPUT); //Voltage Sensor A0
  {
    //(To prevent self-trigger during initial boot)
    digitalWrite(D0, HIGH); //D0
    digitalWrite(D5, HIGH); //D5
    digitalWrite(D6, HIGH); //D6
  }

  timer.setInterval(10000L, CEBsensor1);
  timer.setInterval(1000L, runtime);
  timer.setInterval(1000L, invertertime);
  //timer.setInterval(10000L,Voltagesensor1);
  //timer.setInterval(1000L, stats);
}

void loop()
{
  timer.run();

  if (WiFi.status() != WL_CONNECTED && !Blynk.connected())
  {
    ESP.restart();
  } else {
    Blynk.run();
  }
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V10, V18); //Works, Doesn't get triggered when not on
}

/*
  void stats () {
  ESP.wdtFeed();

  if (WiFi.status() != WL_CONNECTED)
  {
    ESP.restart();
  }

  if (!Blynk.connected())
  {
    ESP.restart();
  }

  }
*/

void invertertime()
{
  if (CEBstate == LOW) {
    if (inverterstate == HIGH) {
      led15.on();
      Blynk.setProperty(V10, "color", "#23C48E");
      Blynk.setProperty(V18, "color", "#23C48E"); //Inverter Status (ON) GREEN
      digitalWrite(D7, HIGH); //INV RELAY ON D7
      delay(100);
      Blynk.setProperty(V2, "offLabel", "OFF Inverter Before Starting");
      Blynk.setProperty(V2, "onLabel", "OFF Inverter!");
    } else {
      Blynk.setProperty(V18, "color", "#ED9D00"); //Inverter Status (OFF) Yellow
      Blynk.setProperty(V10, "color", "#ED9D00");
      digitalWrite(D7, LOW); //INV RELAY OFF D7
      delay(100);
      Blynk.setProperty(V2, "offLabel", "Hold to Start");
      Blynk.setProperty(V2, "onLabel", "Release");
    }
  } else {
  }

  if (inverterstate == HIGH && CEBsensor == LOW) {
    led16.on(); //Inverter Shutoff Indicator ON
    yield();
  } else {
    led16.off(); //Inverter Shutoff Indicator OFF
    yield();
  }
}

void runtime()
{
  if (CEBstate == HIGH && Timerstate == HIGH) {
    times = ((millis() - msec));
    Blynk.setProperty(V16, "label", "Current Generator Run Time");
    Blynk.setProperty(V17, "label", "Current Fuel Consumption");
    {
      String readableTime;
      getReadableTime(readableTime);
      Blynk.virtualWrite(V16, readableTime);
      Blynk.virtualWrite(V17, 1.7 * ((millis() - msec) / 1000) / 3600);
      delay(500);
    }
  } else {
  }

  if (InvTimerstate == HIGH && CEBstate == LOW && Timerstate == LOW) {
    times = ((millis() - msec2));
    Blynk.setProperty(V16, "label", "Inverter Run Time");
    {
      String readableTime;
      getReadableTime(readableTime);
      Blynk.virtualWrite(V16, readableTime);
    }
    delay(500);
  } else {
  }
}

void getReadableTime(String & readableTime) {
  unsigned long currentMillis;
  unsigned long seconds;
  unsigned long minutes;
  unsigned long hours;
  unsigned long days;

  currentMillis = times;
  seconds = currentMillis / 1000;
  minutes = seconds / 60;
  hours = minutes / 60;
  days = hours / 24;
  currentMillis %= 1000;
  seconds %= 60;
  minutes %= 60;
  hours %= 24;

  if (days > 0) {
    readableTime = String(days) + " ";
  }

  if (hours > 0) {
    readableTime += String(hours) + "H:";
  }

  if (minutes < 10) {
    readableTime += "0";
  }
  readableTime += String(minutes) + "m:";

  if (seconds < 10) {
    readableTime += "0";
  }
  readableTime += String(seconds) + "s";
}

BLYNK_WRITE(V1)
//attach Button on virtual V1,
//it will control the Coil
{
  if (param.asInt() == 1) {
    delay(100);
    digitalWrite(D6, LOW); //COIL RELAY ON D6
    delay(100);
    led1.on();
    msec = millis();
    CEBstate = 1;
  }
  if (param.asInt() == 0) {
    delay(100);
    digitalWrite(D6, HIGH); //COIL RELAY OFF D6
    delay(100);
    led1.off();
    CEBstate = 0;
    Blynk.setProperty(V16, "label", "Previous Generator Run Time");
    Blynk.setProperty(V17, "label", "Previous Fuel Consumption");
    //Blynk.setProperty(V23, "isDisabled", true);
    delay(100);
    Blynk.logEvent("generator_off");
  }
}

BLYNK_WRITE(V2)
//attach Button on virtual V2,
//it will control the Starter
{
  if (param.asInt()) {
    newTimer = timer.setTimeout(2000, Starter); // button pressed for >2Sec
  }
  else {
    timer.disable(newTimer);
  }
}

void Starter()
{

  if (inverterstate == LOW) {
    led3.on();
    digitalWrite(D0, LOW); //STARTER CRANK ON D0
    delay(1200); //CRANK DURATION
    digitalWrite(D0, HIGH); //STARTER CRANK OFF D0
    led3.off();
    Blynk.logEvent("generator_on");
    //Blynk.setProperty(V23, "isDisabled", true);
  } else {
  }
}

BLYNK_WRITE(V9)
//attach Button on virtual V9,
//This will control the Fuel
{
  if (param.asInt() == 1) {
    delay(100);
    digitalWrite(D5, LOW); //FUEL RELAY ON D5
    delay(100);
    led2.on();
    Timerstate = 1;
  }
  if (param.asInt() == 0) {
    delay(100);
    digitalWrite(D5, HIGH); //FUEL RELAY OFF D5
    delay(100);
    led2.off();
    Timerstate = 0;
  }
}

BLYNK_WRITE(V10)
//attach Button on virtual V10,
//it will control the Inverter Relay
{
  if (param.asInt() == 1) {
    inverterstate = 1;
    msec2 = millis();
    InvTimerstate = 1;
    Blynk.logEvent("inverter_on");
    Blynk.setProperty(V22, "isDisabled", true);
    Blynk.setProperty(V23, "isDisabled", false);
  }
  if (param.asInt() == 0) {
    inverterstate = 0;
    InvTimerstate = 0;
    Blynk.logEvent("inverter_off");
    Blynk.setProperty(V22, "isDisabled", false);
  }
}


BLYNK_WRITE(V15)
//attach Button on virtual V15,
//This will update System Status
{
  if (param.asInt() == 1) {
    Blynk.virtualWrite(V8, map(WiFi.RSSI(), -110, -30, 30, 100));
  }
  if (param.asInt() == 0) {
  }
}

BLYNK_WRITE(V19)
//attach Button on virtual V19,
//This will reset Coil/Fuel Status
{
  if (param.asInt() == 1) {
    Blynk.virtualWrite(V1, 0);
    led1.off();
    Blynk.virtualWrite(V9, 0);
    led2.off();
    delay(100);
    Blynk.setProperty(V16, "label", "Previous Run Time");
    Blynk.setProperty(V17, "label", "Previous Fuel Consumption");
    delay(100);
    Blynk.logEvent("generator_off");
    Timerstate = 0;
    CEBstate = 0;
  }
  if (param.asInt() == 0) {
  }
}

void CEBsensor1()
//Run every second
{
  CEBsensor = digitalRead(D1); //LDR Sensor
  Blynk.virtualWrite(V12, CEBsensor);

  //If COIL ON and CEBsensor LOW, Trigger "Grid Restored" Blynk Automation
  if (CEBsensor == LOW && CEBstate == HIGH) {
    led13.on(); //Shutoff Indicator ON
    yield();
  } else {
    led13.off(); //Shutoff Indicator OFF
    yield();
  }

  //CEB STATUS LED
  if (CEBsensor == HIGH) {
    led14.on();
    Blynk.setProperty(V14, "color", "#D3435C"); //CEB Status (ON) RED
    Blynk.logEvent("grid_failure");
    Blynk.setProperty(V23, "isDisabled", false);
    Blynk.setProperty(V22, "isDisabled", false);
    yield();
  } else {
    Blynk.setProperty(V14, "color", "#23C48E"); //CEB Status (ON) GREEN
    Blynk.setProperty(V23, "isDisabled", true);
    Blynk.setProperty(V22, "isDisabled", true);
    yield();
  }

}
/*
  void Voltagesensor1()
  //Run every 10 sec
  {
  int sdata = 0;

  value = analogRead(Vsensor);
  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2 / (R1 + R2));

  vin = vin - correctionfactor;

  Blynk.virtualWrite(V5, vin);
  yield();

  if (vin < 12.0) {
    Blynk.setProperty(V5, "color", "#D3435C"); //If UPS Voltage < 12.5v, Gauge RED
  } else {
    Blynk.setProperty(V5, "color", "#F7CE46"); //If UPS Voltage > 12.5v, Gauge ORANGE
  }
  }
*/

//BETA
BLYNK_WRITE(V22)
//attach Button on virtual V22,
//it will control the GENtoINV
{
  if (param.asInt()) {
    newTimer = timer.setTimeout(2000, GENtoINV); // button pressed for >2Sec
  }
  else {
    timer.disable(newTimer);
  }
}

void GENtoINV()
{

  if (inverterstate == LOW) {
    {
      led17.on();
      //FUEL
      Blynk.virtualWrite(V1, 0);
      led1.off();
      digitalWrite(D6, HIGH); //COIL RELAY OFF D6
      delay(100);
      CEBstate = 0;
      Blynk.setProperty(V16, "label", "Previous Generator Run Time");
      Blynk.setProperty(V17, "label", "Previous Fuel Consumption");
      delay(100);
      Blynk.logEvent("generator_off");
    }
    delay(1500);
    {
      //COIL
      Blynk.virtualWrite(V9, 0);
      led2.off();
      digitalWrite(D5, HIGH); //FUEL RELAY OFF D5
      delay(100);
      Timerstate = 0;
    }

    delay(2000);
    {
      //INV Relay
      Blynk.virtualWrite(V10, HIGH);
      inverterstate = 1;
      msec2 = millis();
      InvTimerstate = 1;
      Blynk.logEvent("inverter_on");
      led17.off();
    }

  } else {
  }
}

BLYNK_WRITE(V23)
//attach Button on virtual V23,
//it will control the INVtoGEN
{
  if (param.asInt()) {
    newTimer = timer.setTimeout(2000, INVtoGEN); // button pressed for >2Sec
  }
  else {
    timer.disable(newTimer);
  }
}

void INVtoGEN()
{

  {
    //INV RELAY OFF
    led17.on();
    inverterstate = 0;
    InvTimerstate = 0;
    Blynk.logEvent("inverter_off");
    {
      Blynk.virtualWrite(V10, 0);
      Blynk.setProperty(V10, "color", "#ED9D00");
      Blynk.setProperty(V18, "color", "#ED9D00"); //Inverter Status (OFF) Yellow
      digitalWrite(D7, LOW); //INV RELAY OFF D7
      delay(100);
      Blynk.setProperty(V2, "offLabel", "Hold to Start");
      Blynk.setProperty(V2, "onLabel", "Release");
    }
    Blynk.setProperty(V22, "isDisabled", false);
  }
  delay(3000);
  {
    //FUEL
    Blynk.virtualWrite(V9, 1);
    digitalWrite(D5, LOW); //FUEL RELAY ON D5
    led2.on();
    Timerstate = 1;
  }
  delay(1500);
  {
    //COIL
    Blynk.virtualWrite(V1, 1);
    digitalWrite(D6, LOW); //COIL RELAY ON D6
    led1.on();
    msec = millis();
    Blynk.setProperty(V23, "isDisabled", true);
    CEBstate = 1;
  }
  delay(2000);
  {
    //STARTER
    if (inverterstate == LOW) {
      led3.on();
      digitalWrite(D0, LOW); //STARTER CRANK ON D0
      delay(1200); //CRANK DURATION
      digitalWrite(D0, HIGH); //STARTER CRANK OFF D0
      led3.off();
      Blynk.logEvent("generator_on");
      led17.off();
    }
  }

}

You need to stop using Blynk.begin, and switch to managing your own WiFi connection and using Blynk.config and Blynk.connect.

You then need to have timed functions that check if youā€™re still connected to WiFi and to Blynk, and that re-connect where necessary.

Pete.

1 Like

Noted. I refered to this discussion and code and implemented certain parts for testing. So far so good, the board reboots when it detects a loss of connection to the servers. I will continue testing and hopefully the issue has been solved.

Good news!
The issue appears to be solved and the board has stayed online for 2 days. Within the 2 days it has lost connection to the server, but after implementing parts from the above attached discussion, the board automatically reboots and restores connection to the server, thus fixing the issue.

I am really happy with this result. My project is now online and everything is working as expected.
Its been a month of diagnosing this simple matter but I really appriciate your help Pete. Thank you.

Heres a summary for anyone new of the issue I faced.
My D1 mini would go offline multiple times a day and would not reconnect to the Blynk server automatically. After concluding that it wasnā€™t my code nor my ISP blocking Blynk, I discovered that my internet connection would have multiple black outs per day. Therefore when a blackout occurs, the board looses connection to the server (expected), however when the internet connection is restored, the board continues to ā€œConnecting to Blynk.cloud:80ā€ till I manually reboot it.

You do t need to reboot the server, just re-establish the Blynk and/or WiFi connection.

Pete.