Okay, so here is my issue using nodemcu esp8266 with blynk.
i have installed a 4ch relay and 4 physical buttons to my nodemcu running blynk.
the problem i am facing is, lets say everything is working fine controlling my relays using physical buttons and blynk app and all 4 relays are turned on. But suddenly loose connection to the internet and nodemcu is still connected to the wifi router but not to the internet hence not connected to the blynk server.
i can still use the system with the physical buttons without any problem.
the problem comes when my internet connection is restored and nodemcu connects to the internet, rather than the relays staying in the state it should be when the internet was down, all 4 relays turn on (previous state in blynk app) as soon as blynk server connection is restored.
what i want is the relays to stay as it is rather than going back to the previous state.
( blynk app should update the buttons status from nodemcu rather than it updating the nodemcu button status when connection is restored )
here is my code.
//#define BLYNK_DEBUG
#define BLYNK_TIMEOUT_MS 750 // must be BEFORE BlynkSimpleEsp8266.h doesn't work !!!
#define BLYNK_HEARTBEAT 17 // must be BEFORE BlynkSimpleEsp8266.h works OK as 17s
#define BLYNK_PRINT Serial
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxx";
char ssid[] = "xxxxxx"; // username or ssid of your WI-FI
char pass[] = "xxxxxx"; // password of your Wi-Fi
char server[] = "blynk-cloud.com";
unsigned int port = 8442;
// Your WiFi credentials.
// Set password to "" for open networks.
// Set your LED and physical button pins here
const int ledPin1 = 0;
const int ledPin2 = 13;
const int ledPin3 = 1;
const int ledPin4 = 2;
const int btnPin1 = 5;
const int btnPin2 = 4;
const int btnPin3 = 12;
const int btnPin4 = 14;
void checkPhysicalButton();
int led1State = LOW;
int btn1State = HIGH;
int led2State = LOW;
int btn2State = HIGH;
int led3State = LOW;
int btn3State = HIGH;
int led4State = LOW;
int btn4State = HIGH;
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(V12);
Blynk.syncVirtual(V13);
Blynk.syncVirtual(V14);
Blynk.syncVirtual(V15);
// Alternatively, you could override server state using:
//Blynk.virtualWrite(V12, led1State);
//Blynk.virtualWrite(V13, led2State);
//Blynk.virtualWrite(V14, led3State);
//Blynk.virtualWrite(V15, led4State);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V12) {
led1State = param.asInt();
digitalWrite(ledPin1, led1State);
}
BLYNK_WRITE(V13) {
led2State = param.asInt();
digitalWrite(ledPin2, led2State);
}
BLYNK_WRITE(V14) {
led3State = param.asInt();
digitalWrite(ledPin3, led3State);
}
BLYNK_WRITE(V15) {
led4State = param.asInt();
digitalWrite(ledPin4, led4State);
}
void checkPhysicalButton()
{
if (digitalRead(btnPin1) == LOW) {
// btn1State is used to avoid sequential toggles
if (btn1State != LOW) {
// Toggle LED state
led1State = !led1State;
digitalWrite(ledPin1, led1State);
// Update Button Widget
Blynk.virtualWrite(V12, led1State);
}
btn1State = LOW;
} else {
btn1State = HIGH;
}
if (digitalRead(btnPin2) == LOW) {
// btnState is used to avoid sequential toggles
if (btn2State != LOW) {
// Toggle LED state
led2State = !led2State;
digitalWrite(ledPin2, led2State);
// Update Button Widget
Blynk.virtualWrite(V13, led2State);
}
btn2State = LOW;
} else {
btn2State = HIGH;
}
if (digitalRead(btnPin3) == LOW) {
// btnState is used to avoid sequential toggles
if (btn3State != LOW) {
// Toggle LED state
led3State = !led3State;
digitalWrite(ledPin3, led3State);
// Update Button Widget
Blynk.virtualWrite(V14, led3State);
}
btn3State = LOW;
} else {
btn3State = HIGH;
}
if (digitalRead(btnPin4) == LOW) {
// btnState is used to avoid sequential toggles
if (btn4State != LOW) {
// Toggle LED state
led4State = !led4State;
digitalWrite(ledPin4, led4State);
// Update Button Widget
Blynk.virtualWrite(V15, led4State);
}
btn4State = LOW;
} else {
btn4State = HIGH;
}
}
void setup()
{
// Debug console
Serial.begin(115200);
Serial.println();
WiFiManager wifiManager;
wifiManager.autoConnect("MCU1", "1397");
// line below is blocking code, consider regular WiFi connection with timeout if you have router problems
Blynk.connectWiFi(ssid, pass); // used with Blynk.connect() in place of Blynk.begin(auth, ssid, pass, server, port);
// line below needs to be BEFORE Blynk.connect()
timer.setInterval(11000L, CheckConnection); // check if still connected every 11s
Blynk.config(auth, server, port);
Blynk.connect();
pinMode(ledPin1, OUTPUT);
pinMode(btnPin1, INPUT_PULLUP);
digitalWrite(ledPin1, led1State);
pinMode(ledPin2, OUTPUT);
pinMode(btnPin2, INPUT_PULLUP);
digitalWrite(ledPin2, led2State);
pinMode(ledPin3, OUTPUT);
pinMode(btnPin3, INPUT_PULLUP);
digitalWrite(ledPin3, led3State);
pinMode(ledPin4, OUTPUT);
pinMode(btnPin4, INPUT_PULLUP);
digitalWrite(ledPin4, led4State);
// Setup a function to be called every 100 ms
timer.setInterval(500L, checkPhysicalButton);
}
void CheckConnection(){ // check every 11s if connected to Blynk server
if(!Blynk.connected()){
Serial.println("Not connected to Blynk server");
Blynk.connect(); // try to connect to server with default timeout
}
else{
Serial.println("Connected to Blynk server");
}
}
void loop()
{
Blynk.run();
timer.run();
}
Any Help Would be Appreciated…!