I have this code running fine, button when i toggle relay using physical button it does not update the state in blynk app, i got it working before somehow but i don’t remember.
Any help will be appreciated.
Pull/Push works on D0 rather than V0 or V1.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Vito's Vifi";
char pass[] = "xxxxxxxxxxxxxx";
char server[] = "blynk-cloud.com"; // URL for Blynk Cloud Server
int port = 8080;
int DeviceLED = 2;
int ReCnctFlag; // Reconnection Flag
int ReCnctCount = 0; // Reconnection counter
// Set your LED and physical button pins here
const int ledPin = 0;
const int btnPin = 2;
BlynkTimer timer;
void checkPhysicalButton();
int ledState = LOW;
int btnState = HIGH;
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(btnPin, INPUT_PULLUP);
digitalWrite(ledPin, ledState);
WiFi.begin(ssid, pass); // Non-blocking if no WiFi available
Blynk.config(auth, server, port);
Blynk.connect();
timer.setInterval(1000L, UpTime);
// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButton);
}
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
Serial.println("Cconnected");
ReCnctCount = 0;
// Request the latest state from the server
Blynk.syncVirtual(V2);
// Alternatively, you could override server state using:
//Blynk.virtualWrite(V2, ledState);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V2) {
ledState = param.asInt();
digitalWrite(ledPin, ledState);
}
void checkPhysicalButton()
{
if (digitalRead(btnPin) == LOW) {
// btnState is used to avoid sequential toggles
if (btnState != LOW) {
// Toggle LED state
ledState = !ledState;
digitalWrite(ledPin, ledState);
// Update Button Widget
Blynk.virtualWrite(V2 , ledState);
}
btnState = LOW;
} else {
btnState = HIGH;
}
}
void UpTime() {
Blynk.virtualWrite(V2, millis() / 1000); // Send UpTime seconds to App
Serial.print("UpTime: ");
Serial.println(millis() / 1000); // Send UpTime seconds to Serial
digitalWrite(DeviceLED, !digitalRead(DeviceLED)); // Blink onboard LED
}
void loop()
{
timer.run();
if (Blynk.connected()) { // If connected run as normal
Blynk.run();
} else if (ReCnctFlag == 0) { // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
ReCnctFlag = 1; // Set reconnection Flag
Serial.println("Starting reconnection timer in 30 seconds...");
timer.setTimeout(30000L, []() { // Lambda Reconnection Timer Function
ReCnctFlag = 0; // Reset reconnection Flag
ReCnctCount++; // Increment reconnection Counter
Serial.print("Attempting reconnection #");
Serial.println(ReCnctCount);
Blynk.connect(); // Try to reconnect to the server
}); // END Timer Function
}
}