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

Be careful sending too many requests to the Blynk server, use simpletimer library to help with this. In my code I only check the state of my outputs every 0.2sec

Also avoid using ‘delay’ in your code.

Thanks for the tip. I will add the simpletimer libary. I am not using any delays.

@mpo881 you say you are not using any delays, which is the recommended use, but I see two delay(500)'s in the main loop.

where can i find a tutorial about simpletimer?

I’m sure there is plenty of stuff on the internet but it is very ‘simple’.

Include the library, name the timer, set the interval, name the function, create the function, call the timer.

//
#include <SimpleTimer.h>
SimpleTimer mytimer;   // allocate a name (mytimer) to the timer

void setup()
{

mytimer.setInterval(1000L, somefunction);   // set the interval 1000L is 1s / 1000 ms
                                       // and name your function

mytimer.setInterval(5000L, someotherfunction);  // do something every 5 seconds
}

void somefunction()
{
    // code for whatever you want to do at the chosen intervals (e.g. every 1 second)
}

void someotherfunction()
{
    // code for whatever you want to do at the chosen intervals (e.g. every 5 seconds)
}

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

thanks costas, ill try it.

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?