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

Here is my code, I allready try it, but i have something wrong. can you see it?

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "xxxx"; //insert here your token generated by Blynk
SimpleTimer mytimer;   // allocate a name (mytimer) to the timer

int relay1 = 12;
int relay2 = 14;
int button1 = 13;
int button2 = 5;
int relayVButton1 = 0;
int relayVButton2 = 0;

boolean relayState1 = 0;
boolean relayState2 = 0;
boolean buttonState1 = 0;
boolean buttonState2 = 0;
WidgetLED led1(11); //virtual led 
WidgetLED led2(10); //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
 mytimer.setInterval(2000L, somefunction);
 
while (Blynk.connect() == false) {
 buttonState1 = digitalRead (button1);
  if (buttonState1 > 0){
    relayState1 = !relayState1;
      } 
      digitalWrite(relay1, relayState1);
      delay(500);
       buttonState2 = digitalRead (button2);
  if (buttonState2 > 0){
    relayState2 = !relayState2;
      } 
      digitalWrite(relay2, relayState2);
      delay(500);
  }
  
pinMode(relay1, OUTPUT);
pinMode(button1,INPUT);
pinMode(relay2, OUTPUT);
pinMode(button2,INPUT);
}
void somefunction()
{

buttonState1 = digitalRead (button1);
  if (buttonState1 > 0 || relayVButton1 > 0){
    relayState1 = !relayState1;
      } 
      digitalWrite(relay1, relayState1);
    
      delay(500);
      
  buttonState2 = digitalRead (button2);
  if (buttonState2 > 0 || relayVButton2 > 0){
    relayState2 = !relayState2;
      } 
      digitalWrite(relay2, relayState2);
    
      delay(500);

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

   //----------------button virtual led2---------------
  byte inp2 = digitalRead(relay2);
   
  if (inp2 == HIGH)
  {
   led2.on();
  }
   else
   led2.off();
}  BLYNK_WRITE(V5)
{
  // Get the state of the VButton
  relayVButton1 = param.asInt();
  
 
}
BLYNK_WRITE(V4)
{
  // Get the state of the VButton
  relayVButton2 = param.asInt();
 
} 

 
void loop() {
    Blynk.run(); 
  mytimer.run();                 // call the simple timer routine

 
}

Please describe the problem and what is happening at your end in more detail. The code compiles ok here. You need to lose the delay(500)'s in the somefunction() as the 2 second timer you have should take care of this. Why do you have the relay state sandwiched between the Blynk.connect() while loop? Does it need to be there?

you have confused @mrebaj with me, @mpo881 . We both have the same color logo and initial. I have not posted any code related to this issue. Thank you though for your help.

@mpo881 but @mrebaj has posted code.

I understand that, your reply was to me and I just wanted to clarify so that the information got to the right person. Thanks for your help

Yes I see now, sorry.

no worries, thanks :grin:

I delete all delays and set timer to 500 and now it worked.

Thanks!

I do not know if it is the best, but it works:

//#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h> // here is the SimpleTimer library

char auth[] = "xxx"; //insert here your token generated by Blynk
SimpleTimer timer;   // allocate a name (timer) to the timer

int relay1 = 7;
int relay2 = 8;
int relay3 = 9;
int button1 = 2;
int button2 = 3;
int button3 = 5;
int relayVButton1 = 0;
int relayVButton2 = 0;
int relayVButton3 = 0;

boolean relayState1 = 1;
boolean relayState2 = 1;
boolean relayState3 = 1;
boolean buttonState1 = 1;
boolean buttonState2 = 1;
boolean buttonState3 = 1;

WidgetLED led1(10); //virtual led 
WidgetLED led2(11); //virtual led
WidgetLED led3(12); //virtual led

void setup() 
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth); //insert here your SSID and password
  timer.setInterval(600L, somefunction);
 
  pinMode(relay1, OUTPUT);
  pinMode(button1,INPUT_PULLUP);
  pinMode(relay2, OUTPUT);
  pinMode(button2,INPUT_PULLUP);
  pinMode(relay3, OUTPUT);
  pinMode(button3,INPUT_PULLUP);
  digitalWrite(relay1, 1);
  digitalWrite(relay2, 1);
  digitalWrite(relay3, 1);
} 

void somefunction()
{

  buttonState1 = digitalRead (button1);
    if (buttonState1 < 1 || relayVButton1 > 0)
    {
    relayState1 = !relayState1;
    } 
    digitalWrite(relay1, relayState1);
    
      
  buttonState2 = digitalRead (button2);
    if (buttonState2 < 1 || relayVButton2 > 0)
    {
    relayState2 = !relayState2;
    } 
    digitalWrite(relay2, relayState2);

  buttonState3 = digitalRead (button3);
    if (buttonState3 < 1 || relayVButton3 > 0)
    {
    relayState3 = !relayState3;
    } 
    digitalWrite(relay3, relayState3);
  
  if (digitalRead(relay1) == LOW) //virtual led1
  {
   led1.on();
  }
   else
   led1.off();
  
  if (digitalRead(relay2) == LOW) //virtual led2
  {
   led2.on();
  }
   else
   led2.off();

  if (digitalRead(relay3) == LOW) //virtual led3
  {
   led3.on();
  }
   else
   led3.off();

}  

BLYNK_WRITE(V4)
{
   relayVButton1 = param.asInt(); // Get the state of the VButton
}
BLYNK_WRITE(V5)
{
    relayVButton2 = param.asInt(); // Get the state of the VButton
} 
BLYNK_WRITE(V6)
{
    relayVButton3 = param.asInt(); // Get the state of the VButton
} 
 
void loop() 
{
  Blynk.run(); 
  timer.run();                 // call the simple timer routine
}
4 Likes

Hi,
I’ve been looking for while to find this code. Finally I could control my relays with physical buttons and virtual pins.
To take fully advantage of the code, is it possible to post a FRITZING sketch or any other sketch of this project.
This would really help me out to have a better control over the equipment of my home aquaponic system.

Kind regards,
max_delius

Dears @Costas @Lichtsignaal ,can we get the feedback of the output status on the same used “Button” (configured as switch) on mobile.

@mpo881 @zeeko thanks for help here

1 Like

@NickMurray @dutchman Thanks for your support

1 Like

can i use this code with esp8266 ? it have gpio0 and gpio2?

Hello Mrebaj, I am trying to use this code to connect my NodeMCU to control my light switch, i want to be able to control the light from the blynk app as well as from the physical button, right now i am only able to turn the button on from the blynk app but not from the physical button. do you have a diagram of your connections by any chance?

I want to make project with 4 channel with physical button my programiming knowlege 0 can u help me

This is not a forum where people do your work for you, this is the second thread I see you cluttering with this request.

No offense to you but no one here will do all work for you, If you want to work with mcus you at least need to start learning programming instead of just tossing out that you know 0 about it. It’s like trying trying to get a toddler to ride a bike if we were to try help you in this state.
Learn to:
Sit (arduino)
Crawl (basics of programming)
Walk (c++ for arduino)
Then people can help you ride your
4 channel bike.

5 Likes

very reasonable explanation @Fettkeewl :wink:

@Khatrichaitanya, forums are:

  • to answer technical, reasonable questions

  • or to share useful informations

  • what was your technical question?

  • what useful info did you share with others? (i think that “my programiming knowlege 0” is not useful nor anybody is interested in it)

@Dmitriy I think this is a good example of an older (and solved) thread that needs to be closed/locked or whatever… One user jumped in 7 months after last post, then followed by two more just days ago. All asking for handouts.

1 Like