The blynk app button and physical pushbutton is not syncing

i am working on a project so i am trying to make a relay work with blynk app switch as well as with physical push button the physical push button and the blynk app button is working perfectly but the physical push buttons status is not syncing with the button on the blynk app i have tried everything here is the code


//Replace with your own blynk credentials
#define BLYNK_TEMPLATE_ID "________________"
#define BLYNK_TEMPLATE_NAME "TEST IOT"
#define BLYNK_AUTH_TOKEN "...........................................................-"
//

#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

//server status led
#define BlynkLED 5     // D5


//buttons and led pins
#define btn1     12    // D12

#define led1     13    // D13

//led states
bool led1State = false;

//server state | flag for online/Offline Mode
bool onlineMode = false;

//Wifi config
char ssid[] = "moto g34 5G_4565";
char pass[] = "12345678";
BlynkTimer timer;

//
void isServerConnected(){
  bool isConnected = Blynk.connected();
  if(isConnected == true){
    onlineMode = true;
    digitalWrite(BlynkLED, HIGH);
    Serial.println("Connected");
  }
  if(isConnected == false){
    if(onlineMode == true){
     onlineMode = false;
    } 
    Serial.println("Not Connected");
    digitalWrite(BlynkLED, LOW);
  }
}

void setup(){
  Serial.begin(115200);

  timer.setInterval(2000L, isServerConnected);

  pinMode(BlynkLED, OUTPUT);

  pinMode(led1, OUTPUT);
  
  pinMode(btn1, INPUT_PULLUP);
  
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}

void loop()
{
  //Blynk.run();
  timer.run();
  
  if(onlineMode){
    Blynk.run();
    whenOnline(); //handles online functionalities
  }else{
    whenOffline(); //handles offline functionalities
  }
}

void whenOnline(){
 if (digitalRead(btn1) == LOW) {
    led1State = !led1State;
    updateLEDs();
    delay(50);
    while (digitalRead(btn1) == LOW);

   updateBlynkServer();
  }

    
 }

void whenOffline(){
if (digitalRead(btn1) == LOW) {
    led1State = !led1State;
    updateLEDs();
    delay(50);
    while (digitalRead(btn1) == LOW);
  }

   
}

void updateLEDs(){
  digitalWrite(led1, led1State);
  
}

void updateBlynkServer(){
   Blynk.virtualWrite(V0, led1State); 
   
}

  BLYNK_WRITE(V0){
    led1State = param.asInt();
    Serial.println(led1State);
    updateLEDs();
  }
here i am using a led instead of a relay to test the circuit 

and i am using a esp32 dev module i am new to esp32 i am using esp32 for the first time
my observations

  1. at first time when i turn on the light with the blynk app button the light turns on or off
  2. when turn on or off the light with the physical push button it also works
  3. both the buttons are working fine individually but when i turn on or off the light with the physical push button the state of the blynk app push button does not changes

@pradyut Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code and serial output so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

1 Like

okay i did that

Your sketch is a mess, and contains lots of unnecessary code.

All of the code that checks if the server is connected is a waste of time. You are using Blynk.begin() which is a blocking function, so all code execution will stop if your device isn’t connected.

Also, you should be using either an interrupt on your physical switch, or a BlynkTimer to poll the state of the physical switch. You probably need a denounce routine in there too.
You should not be polling the physical switch in your void loop.

You also need to use BLYNK_CONNECTED() to do a BLYNK.syncVirtual(V0)

You haven’t provided info about how your physical switch and physical LED are wired, or how your datastreams and widgets are configured.

Pete.

1 Like

as i told you i am just a beginner
so can you provide with a fix code ?
or tell what should i do
led is connected with D13 in series with a 220 ohm resistor
push button connection
one leg >-------D12
other leg>-----GND
the blynk button is in switch mode connected with data stream “integer VO[V0]
integer,0/255,id=2”
also one thing i noticed at first time when i turn on the light with blynk button then i turn off the light with the physical push button the the light turns off and also the blynk button state changes to off , after than the blynk app button does not respond to the state change of the physical button mean the synchronisation fails .i am also adding the circuit photo take a look

First of all, I’m not going to write your code for you.

I’m happy to point you in the right direction, and I’ve already done that by pointing-out the issues with your code structure.

I’ve asked you questions about your physical LED and physical switch.
You’ve answered the LED question, but not the switch question.

Until you change your code as I suggested and provide feedback on the behaviour at that stage, and provide information about how you’ve wired your physical switch I can’t really help much further.

I’m not 100% sure what you’re saying here, but one interpretation of the data you’ve provided is that virtual datastream V0 has a minimum value of 0 and a maximum value of 255.
If this is the case then this could be the reason why you are experiencing the issues you’ve described, because your datastream should have a minimum of 0 and a maximum of 1.

Your digitalRead() command on your physical button will return LOW (0) or HIGH (1).
Writing 1 to a datastream with a range of 0-255 will not result in the widget attached to that switch changing from off to on.

Pete.

1 Like

thank you sir i think my problem is solved now
my data stream range was causing the problem i have changed the data stream range from 0-255 to 0-1 and now it is working perfectly fine and to remove the unnecessary part from the code i am using this code i found online and its working really fine