[SOLVED] Relay Control With Push Button and Button Widget

Hi! I want to control relay using a push button and the button widget in switch mode ! I know how to do it in push mode and its easy but i want to use it in switch mode only! Here’s a code that i wrote , but it just goes in a loop and the relay turns on and off! Please HELP!

#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 = 9;
int button1 = 12;
int button2 = 11;
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;
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncAll();

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}
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(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);
  digitalWrite(relay1, 1);
  digitalWrite(relay2, 1);
  digitalWrite(relay3, 1);
} 

void somefunction()
{

  buttonState1 = digitalRead (button1);
    if (buttonState1 < 1 || relayVButton1 < 1)
    {
    relayState1 = !relayState1;
    digitalWrite(relay1, relayState1);
    }
    else if (buttonState1 < 1 && relayVButton1 < 1)
    {
    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();
   Blynk.virtualWrite(V4, LOW);
  }
   else{
   led1.off();
   Blynk.virtualWrite(V4, HIGH);
   }
  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
 
}
1 Like

What hardware are you using ?

Arduino UNO! I am trying to specifically use the relay 1 ! ignore the codes for relay 2 and 3 please! :stuck_out_tongue:

You should be able to do this without any code at all except for the Blynk part code. But i assume you want to us the WigetLeds ?

Nope! I don’t need to use widget leds! I just want a physical button and a button widget to control a relay!
The button widget being in switch mode and the physical button being a push button!

1 Like

Then you don’t need any additional code except for the Blynk part

umm… what … How do i link the physical button then to my relay? O.o

Ah, i did not realise you were talking about a physical button

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!