[SOLVED] Relay Control With Push Button and Button Widget

At this point i can not help you with your code as i have no time to look at it. But im sure there are better qualified people here who will be able to help. The only thing i can come up with atm is using a transistor in combination with your button. But thats a hardware solution and probably not what your looking for

Why reinvent the wheel? This question has been asked and answered so many times…

Pete.

2 Likes

Hi i specifically mentioned many times that i want to use the switch button widget not the push button widget!
I also mentioned that i know how to do it using those methods! :sweat_smile:

It makes no real difference if you’re using a Blynk app switch widget configured in Push or Switch mode, the outcome is the same. The real only coding difference is that with a switch widget in Push mode you may need to do some debounce coding on your Arduino.
In the code you posted you seem to be lacking any code that updates the app widget with your switch state, that may be changed using the physical button. If you look at the examples I posted you’ll see a variety of methods that others have used to achieve this.

Also, I think I’d be using virtual pins connected to the Blynk switch widgets and you appear to have yours configured to physical pins.

Pete.

The only reason i don’t want to use push mode is i don’t want to add another widget to know if my relay is turned on or off! I am using virtual pins for everything ! However i just can’t figure out the right code! If the code works then sometimes the physical button doesn’t work or the virtual button doesn’t ! So i just want some suggestion as to what changes i should make? None of the examples in the entire community worked for me!

I don’t know what more you are expecting here… copying others code is generally acceptable here, but don’t pass it off as “yours”… [SOLVED] Turn on and off relay with blynk button and/or physical button - #36 by dutchman

Many people purposely leave code for others to use, but you still need need to learn how to understand it in order to modify it and eventually write your own code.

We can make suggestions, as has already been done in this topic, but ultimately if you don’t know how to try it yourself, then we are not going to be much help.

sorry if i mentioned its my code , but its just a slightly modified code from the blynk community! As for suggestions , i barely got any, all i got was just some links that uses push button widget ! No where has anyone tried to make it work with switch button widget so i wanted to for i didn’t like having another led widget to display the status ! However any suggestion on what changes to do or anything would be really appreciated !

@Nilava_Chowdhury, I am a little confused as to what you are asking for. You want the widget to be a switch, or do you want a physical switch (like a wall switch) as opposed to a physical button (like a doorbell)?

1 Like

He wants to turn his Blynk button from PUSH to SWITCH.

1 Like

i want both! I want a physical switch (like a wall switch) and a widget in blynk to turn my relay on or off!
Many have already done such thing but in the blynk app they have the widget configured as push and so they have another led widget configured to see the status of the relay, however i don’t want to use a led widget unnecessarily instead use the blynk button widget in switch mode so that the button itself is an indicator if the relay is on or not! i hope i could explain it properly.

Have you tried the Sketch Builder example, “Sync Physical Button”?

https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=More%2FSync%2FSyncPhysicalButton

This uses the Button Widget on the BLYNK App (set as a switch), to control a digital pin (could be a relay, LED, etc.). Although, the physical hardware is a momentary button.

I am using it to control a relay, and the Switch on the app will change if I push the button on the wall.

1 Like

yep , it lead to unlimited sequential toggles!

Okay so i know you want to solve this software wise, but i’m more of a hardware guy so to give you some kind of solution i would suggest using a Transistor in parallel to the physical switch. Like this:

I am assuming this is when using a physical switch instead of a physical button? Because, it works just fine in my application.

Try swapping the button widget so that it sends a 1 when off and a 0 when on.

Pete.

2 Likes

Oh yheah those arduino relays are weird sometimes. I have some that turn on with a 1 and others that turn on with a zero.

1 Like

Damn it man! It was my timer!!! I had set it to 100 ms and that lead to all the problems!!! Thanks to everyone for helping out!
Btw any suggestions for improving the code? or any changes suggestion?
P.S. There’s a slight lag now with button press.

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX
#include <SPI.h>
#include <BlynkSimpleStream.h>
#include <SimpleTimer.h> // here is the SimpleTimer library

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

int relay1 = 3;
int relay2 = 4;
int relay3 = 5;
int relay4 = 6;

int button1 = 12;
int button2 = 11;
int button3 = 10;
int button4 = 9;

int relayVButton1 = 0;
int relayVButton2 = 0;
int relayVButton3 = 0;
int relayVButton4 = 0;

int buttonState1 = HIGH;
int buttonState2 = HIGH;
int buttonState3 = HIGH;
int buttonState4 = HIGH;

boolean relayState1 = 1;
boolean relayState2 = 1;
boolean relayState3 = 1;
boolean relayState4 = 1;

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

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}
WidgetLED led1(11); //virtual led 
WidgetLED led2(12); //virtual led
WidgetLED led3(13); //virtual led
WidgetLED led4(14); //virtual led
WidgetLCD lcd(V31);

void setup() 
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(Serial, auth); 
  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);
  pinMode(relay4, OUTPUT);
  pinMode(button4,INPUT_PULLUP);
  digitalWrite(relay1, 1);
  digitalWrite(relay2, 1);
  digitalWrite(relay3, 1);
  digitalWrite(relay4, 1);
} 

void somefunction()
{
//Relay1
  if (digitalRead(button1) == LOW)
    {
      if (buttonState1 !=LOW)
      {
       relayState1= !relayState1;
      Blynk.virtualWrite(V1, relayState1);
      digitalWrite(relay1, relayState1);
      }
      buttonState1 = LOW;
    }else{
      buttonState1 = HIGH;
    }
//Relay 2
     if (digitalRead(button2) == LOW)
    {
      if (buttonState2 !=LOW)
      {
       relayState2= !relayState2;
      Blynk.virtualWrite(V2, relayState2);
      digitalWrite(relay1, relayState2);
            }
      buttonState1 = LOW;
    }else{
      buttonState1 = HIGH;
    }
//Relay3
   if (digitalRead(button3) == LOW)
    {
      if (buttonState3 !=LOW)
      {
       relayState3= !relayState3;
      Blynk.virtualWrite(V3, relayState3);
      digitalWrite(relay1, relayState3);
      
      }
      buttonState1 = LOW;
    }else{
      buttonState1 = HIGH;
    }
//Relay4
      if (digitalRead(button4) == LOW)
    {
      if (buttonState4 !=LOW)
      {
       relayState4= !relayState4;
      Blynk.virtualWrite(V4, relayState4);
      digitalWrite(relay1, relayState4);
      
      }
      buttonState1 = LOW;
    }else{
      buttonState1 = HIGH;
    }
  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();
     
   if (digitalRead(relay4) == LOW) //virtual led3
  {
   led4.on();
   
  }
   else
   led4.off();
  }

  


BLYNK_WRITE(V1)
{
   relayVButton1 = param.asInt(); // Get the state of the VButton
   digitalWrite(relay1, relayVButton1);
}
BLYNK_WRITE(V2)
{
    relayVButton2 = param.asInt(); // Get the state of the VButton
    digitalWrite(relay2, relayVButton2);
} 
BLYNK_WRITE(V3)
{
    relayVButton3 = param.asInt(); // Get the state of the VButton
    digitalWrite(relay3, relayVButton3);
} 
BLYNK_WRITE(V4)
{
    relayVButton4 = param.asInt(); // Get the state of the VButton
    digitalWrite(relay4, relayVButton4);
} 
 
void loop() 
{
  Blynk.run(); 
  timer.run();                 // call the simple timer routine
 
}

@Dema323 , yah they are really weird! Mine turns on with a 0 !

1 Like

So did you end up using a Physical button or Physical switch?

Also, I though the idea was to not use the led widget, but i see you are monitoring the relay pin and turning the virtual led on/off. If you aren’t using them on the App, I would remove the code from your program.

How long is a “slight lag”?