Help me with not updating button state

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
  }
}

Is it a push button or a switch ?

I have connected the physical push button to GPIO 1 so when i push, it turns on relay and when i push it again it turn off relay. it works through blynk app but when i push physical button relay works but blynk app on off button does not update either on or off.

Usually , you need to use interrupt and debounce code instead of reading digital pin with a push button.
Maybe you could test led state instead of button

can you help me with that or just is there any way to update button widget?
I got this working by adding just a line, i lost that file. I wrote it again and now i dont remember how i got it working before.

You can test if led pin is high or low and trig virtual led

val = digitalRead(inPin);

try

WidgetLED led1(V2); // before setup

and

led1.off(); and led1.on();

instead of

Blynk.virtualWrite(V2 , ledState);

i tried this but no luck yet.

this my old video of my former sketch. play it from 4:00 and you will see me pushing physical button and updating state in app. i want to do that.

1 Like

ok im at something now, i changed
from this
const int ledPin = 0;
const int btnPin = 2;
to this
const int ledPin = 1;
const int btnPin = 2;

now physical button blinks the blue light on esp when i push not the relay but if i update pin status via the url below, it shows in app just the way i want…widget button changes it state.

http://188.166.206.43/xxxtoken/update/D0?value=1

im confused :frowning:

But the problem is the ledWidget, Try to put a value instead a ledstate.
225 and 0
high and low
but don’t use ledsate value

ok now, physical button is working, relay is working but physical button not updates in blynk app but if change its state using url from browser then it works.

just physical button not updating the widget…whats next ?

Did you change Blynk.virtualWrite(V2 , ledState);?

i tried changing it to
Blynk.virtualWrite(V2 , 0);
and also
Blynk.virtualWrite(ledPin , ledState);

No Luck

Try this

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 , 0);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
 // Update Button Widget
      Blynk.virtualWrite(V2 , 255);
  }
}

Nope, still same.

V2 isn’t a led ? it’s a button ?

I guess so, it controls the Relay.

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 , LOW);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
 // Update Button Widget
      Blynk.virtualWrite(V2 , HIGH);
  }
}

Still not working, its same. Updates button state via url but not with physical button.
Man i will buy you a cup of coffee if you help me solve this.
This url turns on relay and changes button state in blynk app
and if i write 0 at the end it turns of as desired.
http://188.166.206.43/xxxxxxxxxxxxxx/update/D0?value=1