BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

How to make the relay close for 15 minutes every 2 hours?

Hi everyone! I use NodeMCU v3 with esp8266.I have a goal to turn on the button in the Blynk application, the relay will turn on for 15 minutes every 2 hours. Tell me how to do this please:)

Hi, you need to use timers to check the duration, this is the sort of logic you need to include in you sketch:

If buttonWidget is pressed;
Start 2Hour timer;
Start 15Minute timer;
Turn on relay;
15Minute timer ends;
Turn off relay;
2Hour timer ends;
Is buttonWidget still pressed?;
Yes?
Start 15Minute timer;
Turn on relay;
15Minute timer ends;
Turn off relay;
No?
Stop;

Start with the basics, learn how to use the individual components, button widget, timers (hint: millis() ); etc Once you know how to do these things then gradually add them together.

cul
billd

Hi, thanks a lot for the advice. But I’m new to programming, can you please show by example how this will look like code)

This part

Take a look at this code.

This code did not help me, thank you. Here is a fragment of my code and
in this function, RELE1 and RELE2 must be closed every 2 hours for 15 minutes. When the value of the LOW relay is closed, when the HIGH relay is not closed. HELP ME PLEASE


BLYNK_WRITE(V5)
  {
    rele_cycle = param.asInst();
    if (rele_cycle ==1)
      {
        digitalWrite(RELE1,LOW);
        digitalWrite(RELE2,LOW);
        digitalWrite(RELE3,HIGH);
        digitalWrite(RELE4,HIGH);
        digitalWrite(RELE5,HIGH);
        digitalWrite(RELE6,HIGH);
        digitalWrite(RELE7,HIGH);
        digitalWrite(RELE8,HIGH);
        Blynk.virtualWrite(V0,0);
        Blynk.virtualWrite(V1,0);
        Blynk.virtualWrite(V2,0);
        Blynk.virtualWrite(V3,0);
        Blynk.virtualWrite(V4,0);
      }
    
  }

I’d suggest that you search the forum for Timeout Timers. You will then understand what @Bill_Donnelly said.
Nobody is going to write your code for you, so it’s up to you to have a go and ask for help with specific issues that you encounter along the way.

Pete.

1 Like

So give this a try.

BlynkTimer timer;  //If you aren't yet using an instance of BlynkTimer that is called timer, in the top of your code with the global declarations put a line like this it creates a timer instance.

//in the void setup() add a line like this

timer.setInterval(7200000L, trigger_Relay);  // runs this function every 2 hrs based on milliseconds

// create a new function in your code
void trigger_Relay()
{
digitalWrite(relay_1, HIGH); //or low depending on if your relay is active high or low.
//then for my favorite gadget known as Lambda timer
timer.setTimeout(900000L,[]()  //this will activate the following code after 15 min or 900000 milliseconds
   {   
      digitalWrite(relay_1,LOW); // or opposite of above                                
    });
}

Now if those times would ever change I would set up two sliders that input a number to a global variable, then instead of

timer.setInterval(7200000L, trigger_Relay);
//use
timer.setInterval(relay_interval, trigger_Relay);

timer.setTimeout(900000L, ......
//and
timer.setTimeout(run_time,......

Now does it make sence?

3 Likes

You probably didn’t understand me, I have a button on the virtual pin V5, I need that when this button is turned on in BLYNK, the relay closes every 2 hours for 15 minutes.

#define RELE1 D1
#define RELE2 D2
#define RELE3 D3
#define RELE4 D4
#define RELE5 D5
#define RELE6 D6
#define RELE7 D7
#define RELE8 D8
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
bool para_rele1;
bool para_rele2;
bool para_rele3;
bool para_rele4;
bool rele_off;
bool rele_cycle;

char auth[] = "****";
char ssid[] = "*****";
char pass[] = "*****";

void setup()
timer.setInterval(7200000L, trigger_Relay);
{
  Serial.begin(115200);
  Blynk.begin(auth,ssid,pass);
  pinMode(RELE1,OUTPUT);
  pinMode(RELE2,OUTPUT);
  pinMode(RELE3,OUTPUT);
  pinMode(RELE4,OUTPUT);
  pinMode(RELE5,OUTPUT);
  pinMode(RELE6,OUTPUT);
  pinMode(RELE7,OUTPUT);
  pinMode(RELE8,OUTPUT);
  
  digitalWrite(RELE1,HIGH);
  digitalWrite(RELE2,HIGH);
  digitalWrite(RELE3,HIGH);
  digitalWrite(RELE4,HIGH);
  digitalWrite(RELE5,HIGH);
  digitalWrite(RELE6,HIGH);
  digitalWrite(RELE7,HIGH);
  digitalWrite(RELE8,HIGH);

  Blynk.virtualWrite(1,HIGH);
  Blynk.virtualWrite(2,HIGH);
  Blynk.virtualWrite(3,HIGH);
  Blynk.virtualWrite(4,HIGH);
  Blynk.virtualWrite(5,HIGH);
  Blynk.virtualWrite(6,HIGH);
  Blynk.virtualWrite(7,HIGH);
  Blynk.virtualWrite(8,HIGH);
  }

  BLYNK_WRITE(V0)
  {
  rele_off = param.asInt();
  if (rele_off ==1)
    {
     digitalWrite(RELE1,HIGH);
     digitalWrite(RELE2,HIGH);
     digitalWrite(RELE3,HIGH); 
     digitalWrite(RELE4,HIGH);
     digitalWrite(RELE5,HIGH);
     digitalWrite(RELE6,HIGH);
     digitalWrite(RELE7,HIGH);
     digitalWrite(RELE8,HIGH);
     Blynk.virtualWrite(V1,0);
     Blynk.virtualWrite(V2,0);
     Blynk.virtualWrite(V3,0);
     Blynk.virtualWrite(V4,0);
     Blynk.virtualWrite(V5,0);
    }
  }
  
  BLYNK_WRITE(V1)
  {
  para_rele1 = param.asInt();
  if (para_rele1 ==1)
    {
     digitalWrite(RELE1,LOW);
     digitalWrite(RELE2,LOW);
     digitalWrite(RELE3,HIGH); 
     digitalWrite(RELE4,HIGH);
     digitalWrite(RELE5,HIGH);
     digitalWrite(RELE6,HIGH);
     digitalWrite(RELE7,HIGH);
     digitalWrite(RELE8,HIGH);
     Blynk.virtualWrite(V0,0);
     Blynk.virtualWrite(V2,0);
     Blynk.virtualWrite(V3,0);
     Blynk.virtualWrite(V4,0);
     Blynk.virtualWrite(V5,0);
    }
  }
  BLYNK_WRITE(V2)
  {
  para_rele2 = param.asInt();
  if (para_rele2 ==1)
    {
     digitalWrite(RELE3,LOW);
     digitalWrite(RELE4,LOW);
     digitalWrite(RELE1,HIGH);
     digitalWrite(RELE2,HIGH);
     digitalWrite(RELE5,HIGH);
     digitalWrite(RELE6,HIGH);
     digitalWrite(RELE7,HIGH);
     digitalWrite(RELE8,HIGH);
     Blynk.virtualWrite(V0,0);
     Blynk.virtualWrite(V1,0);
     Blynk.virtualWrite(V3,0);
     Blynk.virtualWrite(V4,0);
     Blynk.virtualWrite(V5,0);
     }
  }
  BLYNK_WRITE(V3)
  {
  para_rele3 = param.asInt();
  if (para_rele3 ==1)
    {
     digitalWrite(RELE5,LOW);
     digitalWrite(RELE6,LOW);
     digitalWrite(RELE2,HIGH);
     digitalWrite(RELE4,HIGH);
     digitalWrite(RELE1,HIGH);
     digitalWrite(RELE3,HIGH);
     digitalWrite(RELE7,HIGH);
     digitalWrite(RELE8,HIGH);
     Blynk.virtualWrite(V0,0);
     Blynk.virtualWrite(V1,0);
     Blynk.virtualWrite(V2,0);
     Blynk.virtualWrite(V4,0);
     Blynk.virtualWrite(V5,0);
    }
  }
  BLYNK_WRITE(V4)
  {
  para_rele4 = param.asInt();
  if (para_rele4 ==1)
    {
     digitalWrite(RELE7,LOW);
     digitalWrite(RELE8,LOW);
     digitalWrite(RELE1,HIGH);
     digitalWrite(RELE2,HIGH);
     digitalWrite(RELE3,HIGH);
     digitalWrite(RELE4,HIGH);
     digitalWrite(RELE5,HIGH);
     digitalWrite(RELE6,HIGH);
     Blynk.virtualWrite(V0,0);
     Blynk.virtualWrite(V1,0);
     Blynk.virtualWrite(V2,0);
     Blynk.virtualWrite(V3,0);
     Blynk.virtualWrite(V5,0);
    }
  }
  BLYNK_WRITE(V5)
  {
    rele_cycle = param.asInst();
    if (rele_cycle ==1)
      {
        digitalWrite(RELE1,LOW);
        digitalWrite(RELE2,LOW);
        digitalWrite(RELE3,HIGH);
        digitalWrite(RELE4,HIGH);
        digitalWrite(RELE5,HIGH);
        digitalWrite(RELE6,HIGH);
        digitalWrite(RELE7,HIGH);
        digitalWrite(RELE8,HIGH);
        Blynk.virtualWrite(V0,0);
        Blynk.virtualWrite(V1,0);
        Blynk.virtualWrite(V2,0);
        Blynk.virtualWrite(V3,0);
        Blynk.virtualWrite(V4,0);
      }
    
  }
void trigger_Relay()
{
  BLYNK_WRITE(V5)
  {
  para_rele1 = param.asInt();
  if (para_rele1 ==1)
    {
     digitalWrite(RELE1,LOW);
     digitalWrite(RELE2,LOW);
     digitalWrite(RELE3,HIGH); 
     digitalWrite(RELE4,HIGH);
     digitalWrite(RELE5,HIGH);
     digitalWrite(RELE6,HIGH);
     digitalWrite(RELE7,HIGH);
     digitalWrite(RELE8,HIGH);
     Blynk.virtualWrite(V0,0);
     Blynk.virtualWrite(V2,0);
     Blynk.virtualWrite(V3,0);
     Blynk.virtualWrite(V4,0);
    }
 timer.setTimeout(900000L,[]()
   {
     digitalWrite(RELE1,HIGH);
     digitalWrite(RELE2,HIGH);
     digitalWrite(RELE3,HIGH); 
     digitalWrite(RELE4,HIGH);
     digitalWrite(RELE5,HIGH);
     digitalWrite(RELE6,HIGH);
     digitalWrite(RELE7,HIGH);
     digitalWrite(RELE8,HIGH);
     Blynk.virtualWrite(V0,0);
     Blynk.virtualWrite(V2,0);
     Blynk.virtualWrite(V3,0);
     Blynk.virtualWrite(V4,0);
   }
    
}

void loop()
{
  Blynk.run();
}

Maybe like this?

Gives an error message:expected initializer before ‘timer’

void setup()
timer.setInterval(7200000L, trigger_Relay);
{
  Serial.begin(115200);
  Blynk.begin(auth,ssid,pass);
  pinMode(RELE1,OUTPUT);
  pinMode(RELE2,OUTPUT);

This

timer.setInterval(7200000L, trigger_Relay);

Needs to be inside of the setup(). Like so…

void setup()
{
Serial.begin(115200);
  Blynk.begin(auth,ssid,pass);
  pinMode(RELE1,OUTPUT);
  pinMode(RELE2,OUTPUT);
  pinMode(RELE3,OUTPUT);
  pinMode(RELE4,OUTPUT);
  pinMode(RELE5,OUTPUT);
  pinMode(RELE6,OUTPUT);
  pinMode(RELE7,OUTPUT);
  pinMode(RELE8,OUTPUT);
  
  digitalWrite(RELE1,HIGH);
  digitalWrite(RELE2,HIGH);
  digitalWrite(RELE3,HIGH);
  digitalWrite(RELE4,HIGH);
  digitalWrite(RELE5,HIGH);
  digitalWrite(RELE6,HIGH);
  digitalWrite(RELE7,HIGH);
  digitalWrite(RELE8,HIGH);

  Blynk.virtualWrite(1,HIGH);
  Blynk.virtualWrite(2,HIGH);
  Blynk.virtualWrite(3,HIGH);
  Blynk.virtualWrite(4,HIGH);
  Blynk.virtualWrite(5,HIGH);
  Blynk.virtualWrite(6,HIGH);
  Blynk.virtualWrite(7,HIGH);
  Blynk.virtualWrite(8,HIGH);
  
   timer.setInterval(7200000L, trigger_Relay);
  }


:frowning:

For this you will need to give the timers a name, and enable/disable them when the virtual button in pressed. For Example

int timer1;
int timer2;

BLYNK_WRITE(V5)
  {
    rele_cycle = param.asInst();
    if (rele_cycle ==1)
      {
       digitalWrite(relay_1, HIGH); //or low depending on if your relay is active high or low.
      timer1= timer.setTimeout(900000L, turnOff);
       timer2 = timer.setInterval(7200000L, trigger_Relay);  // runs this function every 2 hrs based on milliseconds
      }
else
{
  deleteTimer(timer1);
  deleteTimer(timer2);
  digitalWrite(relay_1, LOW); //or low depending on if your relay is active high or low.
}
    
  }

void trigger_Relay()
{
   digitalWrite(relay_1, HIGH); //or low depending on if your relay is active high or low.
   timer1= timer.setTimeout(900000L, turnOff);
}

void turnOff() {
     digitalWrite(relay_1, LOW); //or low depending on if your relay is active high or low.
}

The code structure you are showing in the screenshot is wrong. You should really take the time to learn some of the fundamentals of programming with Arduino. Without some basic knowledge and understanding you will just be guessing at what to do, and not understanding why things don’t work and what the errors are.

I am a big fan of cut-and-paste programming, but even then you still need to know what you are cutting and pasting, where to put it, and how to adapt it to your own needs.

Can I suggest a couple things and we maybe work through this a bit.
1- Those banks of relay writes possibly they have a name you could give that function say all high and two low opens only the dining room blinds I would write a function like this.

void close_dining_shades()
{
     digitalWrite(RELE1,HIGH);
     digitalWrite(RELE2,HIGH);
     digitalWrite(RELE3,HIGH);
     digitalWrite(RELE4,HIGH);
     digitalWrite(RELE5,HIGH);
     digitalWrite(RELE6,HIGH);
     digitalWrite(RELE7,LOW);
     digitalWrite(RELE8,LOW);

     Blynk.virtualWrite(V0,0);
     Blynk.virtualWrite(V1,0);
     Blynk.virtualWrite(V2,0);
     Blynk.virtualWrite(V3,0);
     Blynk.virtualWrite(V4,0);
}

Then in the BLYNK_WRITE function. You could go something like this.

 BLYNK_WRITE(V1)
  {
  para_rele1 = param.asInt();
  if (para_rele1 ==1)
    {
         close_dining_shades();
    }
   }

Then once that is cleaned up maybe we can step through the timer issue.

Should I do this with every BLYNK_WRITE, or just where I want to set the timer?

This is just to clean up the code so you don’t have to type that out every time you want to do a certain thing.

Sorry @Toro_Blanco 's code wasn’t showing up for me… his is a good route. Just remove the

digitalWrite(relay_1, HIGH);

because that will be done in the functions that he is calling just lower “timer1” and “timer2”.

But as he also mentioned you do need to familiarize yourself with Arduino coding more. One way to do this is shorten your intervals to a couple seconds and instead of relay just use the virtual LED’s on the app and you can play around and see whats happening.

This DigitalWrite i need to remove?

Or this?

Please only use three back ticks and put them on a seperate line before and after the code so it is readable…

Think… if the digitalWrite is now inside the function that you just wrote you don’t need it anymore.

Hi i think i don’t need this digitalWrite, If I’m wrong, please show which one is not needed.