[SOLVED] Turn on and off relay with blynk button and/or physical button

this is the working code:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxx"; //insert here your token generated by Blynk


int relay1 = 12;
int button1 = 13;
int relayVButton = 0;

boolean relayState = 1;
boolean buttonState = 0;
WidgetLED led1(11); //virtual led 

void setup() {
     Serial.begin(115200); // See the connection status in Serial Monitor
 Blynk.begin(auth, "ssid", "password"); //insert here your SSID and password
while (Blynk.connect() == false) {
 buttonState = digitalRead (button1);
  if (buttonState > 0){
    relayState = !relayState;
      } 
      digitalWrite(relay1, relayState);
    
      delay(500);
  }
  
pinMode(relay1, OUTPUT);
pinMode(button1,INPUT);

}

BLYNK_WRITE(V5)
{
  // Get the state of the VButton
  relayVButton = param.asInt();
  
}


void loop() {
    Blynk.run();
  // put your main code here, to run repeatedly:
  buttonState = digitalRead (button1);
  if (buttonState > 0 || relayVButton > 0){
    relayState = !relayState;
      } 
      digitalWrite(relay1, relayState);
    
      delay(500);


//----------------button virtual led---------------
  byte inp = digitalRead(relay1);
   
  if (inp == HIGH)
  {
   led1.on();
  }
   else
   led1.off();
 }
1 Like