Nodemcu Relay control help using Blynk Server

Hello Sir,
I need help regarding my beginner project.
I am able to control a relay using nodemcu and blynk but my question is if my nodemcu goes offline can we change state of graphical button in blynk ? Also if i turn on that relay by using physical button on nodemcu then it is possible to change graphical button in Blynk app ?

Please help me, this is my first post in this community.

You can still change the value of a button widget in the app. It obviously won’t have any effect on your relay until your NodeMCU comes back online and you synchronise the state of the the pins with the app.

Yes, take a look at the ‘synchronise physical button’ example in Sketch Builder.

Pete.

Thanks Sir Very Much for your time I will Check and reply you.
Thanks again for your time.

Hello Sir,
First it not worked but when i changed button widget pin to V2 it worked, Great Thanks for helping me.
can you please look into my whole code ?
I am sharing it here.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "8USD52ctA2f2wrk4K_hMI45juD";
char ssid[] = "Ai66";
char pass[] = "ins1234";
WidgetLED led1(V8);
BlynkTimer timer;
WidgetLCD lcd1(V0);
const int R1  = 12;
const int btnPin = 13;
void checkPhysicalButton();
int ledState = LOW;
int btnState = HIGH;
BLYNK_CONNECTED() {
  
  Blynk.syncVirtual(V2);
}

BLYNK_WRITE(V2) {
  ledState = param.asInt();
  digitalWrite(R1, ledState);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
   
    if (btnState != LOW) {

      // Toggle LED state
      ledState = !ledState;
      digitalWrite(R1, ledState);

      // Update Button Widget
      Blynk.virtualWrite(V2, ledState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  timer.setInterval(1000L, blinkLedWidget);
  pinMode(btnPin, INPUT_PULLUP);
  pinMode(R1,OUTPUT);
  digitalWrite(R1, ledState);
  timer.setInterval(100L, checkPhysicalButton);
  Blynk.begin(auth, ssid, pass); 
}

void blinkLedWidget()
{
  if (led1.getValue()) {
    led1.off();
    Serial.println("LED on V8: off");
  } else {
    led1.on();
    Serial.println("LED on V8: on");
    }
 }

void loop()

{  Blynk.run();
  timer.run();
 }

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

Pete.

Thanks for Update I am sorry.
Also please tell me How to keep my relay off by default when Nodemcu powers on.
by using this current code if i left button on at Blynk app and power cycle nodemcu then relay get ON autocratically.

When first powered on , the Nodemcu will run the setup() function first. As you can see, in your setup() function you have digitalWrite(R1, ledState); . So you just need to make sure that your ledState is defined correctly so that the relay starts OFF. That would be done with int ledState = LOW; definition near the top of your code. If your relay is active low, then you would want to change it to int ledState = HIGH; for it to start in the OFF state upon powering up.

Also, once BLYNK is connected to the server the BLYNK_CONNECTED function will run. In your case you have the command Blynk.syncVirtual(V2); which will sync your relay to the virtual button state. That is if the virtual button is ON, then it will turn your relay ON. If you would prefer to force the virtual button to sync with the relay state then you would want to change it to Blynk.virtualWrite(ledState); (as per the comments in the example you were pointed to).

BLYNK_CONNECTED() {
  // Request the latest state from the server
  //Blynk.syncVirtual(V2);

  // Alternatively, you could override server state using:
  Blynk.virtualWrite(V2, ledState);
}

Remove this line to disable the state sync after power cycle.

Blynk.syncVirtual(V2)