Trigger relay specific times of day

Wemos has millis() function that counts milliseconds.
something like:

when relay is on;
Timetocount = millis();
.
.
.
If (millis()-timetocount>5000) do something

Not sure what you mean by this. I use the timer widget to trigger relays with no problem.

I’m using the standalone example. On the App I’m using Timer from an specific time to 5 secs later. It only turns the port high and doesn’t change back to low.

I would use virtual pins, and instead of tying to manipulate the relay directly I would use code.

For example:

BLYNK_WRITE(V5)
{   
int value = param.asInt(); // Get value as integer

if (value == 1)
  {
   digitalWrite(pin, LOW);
   timer.setTimeout(5000L, []() {  // Run every 5 second
   digitalWrite(pin, HIGH);
   Blynk.virtualWrite(V5, LOW);
     });  // END Timer Function
  }
}

Hey, that looks promising. I’m unfamiliar with virtual pins, so if I turn V5 on with a Timer it will shut off after 5000?.. Interesting, will try that.

You will have to add some more stuff to your code beside what I posted. I would take a look at the DOCS and familiarize yourself with timers and such.

Hi @Toro_Blanco ,
What does this line do when V5 is associated to a push button to turn on/off a relay? Is it there to satisfy the Lambda expression? I noticed that the code works without it. Thx

In this particular case where it is being triggered by the timer it may not be absolutley necessary, same for a button widget that has its mode set as push.

I put it in there to make V5 return to the Off/LOW state. If the button widget is set as a switch, it will return the button to the Off/LOW state.

For example, say you had a button widget linked to V5, mode set to switch. When you pressed the button it would go On/HIGH and stay that way for 5 seconds, indicating that whatever task it is doing is on. It will then return to its Off/LOW state after the 5 seconds have passed indicating that the process is over.

It is probably more for the visual aspect than anything, but I use it none the less.

Although, I can also picture a case where if using this line of code below (which I do often use) without the Blynk.virtualWrite(V5, LOW);, you may get a false trigger on startup as V5 will always be On/HIGH.

BLYNK_CONNECTED() {
Blynk.syncVirtual(V5);
}

For some reason I’m getting the error “a function-definition is not allowed here before ‘{’ token”…

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “*”;

// Your WiFi credentials.
// Set password to “” for open networks.
char ssid[] = “";
char pass[] = "
”;

void setup()
{
// Debug console
Serial.begin(9600);

Blynk.begin(auth, ssid, pass);

BLYNK_WRITE(V5)

{int pinValue= param.asInt();
if (pinValue == 1)
{
digitalWrite(D4, LOW);
timer.setTimeout(5000L, {
digitalWrite(D4, HIGH);
Blynk.virtualWrite(V5, LOW);
}); // END Timer Function
}
}
}

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

I’m looking at this on my iPad, so it’s difficult to follow the code, but I think you need to sort out your curly brackets.
Your void setup needs a closing curly bracket before the BLYNK_WRITE(V5) function begins.
And this looks all wrong…

Pete.

Ok, so I rewrote it like this, but now I get “‘timer’ was not declared in this scope”

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
}
int pinValue;
BLYNK_WRITE(V5)
  {pinValue = param.asInt();
   if (pinValue == 1){
   digitalWrite(D4, LOW);
   timer.setTimeout(5000L, []() { 
   digitalWrite(D4, HIGH);
   Blynk.virtualWrite(V5, LOW);
     }); 
  } } 
void loop()
{
   Blynk.run();
 

}

I don’t understand your question.

Pete.

Sorry, I was asking how to post my code so it would appear formatted instead of regular text.

Blynk - FTFC

This is a standalone lambda function… you have it included inside another Blynk function… and it assumes you have properly declared BlynkTimer

Ok, I did forget to run BkynkTimer… Now the code loads ok, but the timer doesn’t work. V5 is stuck on high.

Did you also include timer.run() in the void loop() … and I am still unsure of the Lambda inside another function. But it might work?

Oops… Lol!

Well, it’s working now, but only when the ESP is connected to the wifi if the internet is down it doesn’t trigger the relay. Is that supposed to happen?

As the trigger is being sent by BLYNK, yes you will need to have the Wemos connected to the internet for it to receive the signal from the timer.

Unless you go for a local server of course.

Pete.