How to avoid changing relay status when lost WiFi connection?

Hello,
I have done a simple application, using a ESP8266 module controlling a relay. I add a button Widget to turn on/off the relay.
It works fine, until the WiFi is lost, then the relay also turned off. When WiFi is back, relay turns on as before WiFi was lost.

My question is, how to avoid the relay to change the status when WiFi is lost?

Probably many different ways… but most all will require extra programming on the device side and use of Blynk.syncVirtual(vPin)

Is the problem on Blynk side ex. use of (blynk.run) or ESP problem?
I mean, by using a virtual pin, could cause pin to be out of “sync,”, when communication to Blynk server is lost.
BTW, I use Blynk.syncVirtual in code.

What code? Just guessing if we can’t see what you are doing :wink:

Please remember to format it properly for forum veiwing

Blynk - FTFC

Here is my code. As you see I use a Watchdog.
Problem is, when Watchdog is activated (reach value 5), the ESP tries to restart, but never startup, ESP hang.
If not using the Watchdog, Blynk keep settings and GPIO’s after WiFi dissapear.

Below my original code

#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <WidgetRTC.h>
#include <Ticker.h>

#define LED 2                               //Bulletin LED

volatile int wdCount = 0;
int ledStatus;
SimpleTimer timer;
WidgetRTC rtc;
Ticker secondTick;

char auth[] = "1234567890";                 // Room 2
char ssid[] = "xx";                         // Demo        
char pass[] = "zz";

//-------------------------------------------------------------------------------
void setup() 
{
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  pinMode(D0, OUTPUT);
  digitalWrite(LED, HIGH);                          //bulletin LED need LOW for ON and HIGH for OFF
  rtc.begin();
  Blynk.begin(auth, ssid, pass); 
  secondTick.attach(1,ISRwatchDog);                 // WatchDog check every 1 sec.
  ESP.wdtEnable(1000);
}

// ********************************************************************************
BLYNK_CONNECTED()                                   // runs every time Blynk connection is established
{
   Blynk.syncAll();                                 // Request server to re-send latest values for all pins
   Serial.println(" Connected.....");
}

// ********************************************************************************
BLYNK_WRITE(V0)                                    //Button Widget is writing to pin V0
{
 ledStatus = param.asInt(); 

   switch (ledStatus)
   {
    case 1:
    digitalWrite(LED, LOW);
    ledStatus = 1;
    delay(500);
    break;
    
    case 0:
    digitalWrite(LED, HIGH);
    ledStatus = 0;
    delay(500);
    break;
   }
}

//--------------------------------------------------------------------------------
void loop() 
{
  wdCount = 0;
  Blynk.run();
  timer.run();
}

// ********************************************************************************
void ISRwatchDog()
{
  wdCount++;
  if(wdCount == 5)
  {
    wdt_reset();
    ESP.restart();
  } 
}

After read on some forums, I realize other have same problem. I found a solution which I like to share.

At the Setup, add following lines:

 ESP.wdtDisable();
 ESP.wdtEnable(WDTO_8S);

In loop add line:

 ESP.wdtFeed();

Then remove all other lines related to WatchDog.

Ticker secondTick; 
secondTick.attach(1,ISRwatchDog);                 // WatchDog check every 1 sec._
wdCount = 0;_
void ISRwatchDog()_
{
}

It works for me and got a stable code.