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?
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.
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.