BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Relay and virtual pin

If V1 is 1, turn on relay D3 and after delay (500) set pin V1 to 0 and turn off relay D3 after delay (500).
Something’s not working for me.

void przekaznik()
{
if ( V1 == 1 )
{
digitalWrite(D3,HIGH);
delay(500);
Blynk.virtualWrite(V1,0);
}
else {
delay(500);
digitalWrite(D3,LOW);
}
}

This means nothing to Blynk… you must use Blynk functions like BLYNK_WRITE(V1) in order to respond to vPin state changes.

And it is bad form to use delay() with Blynk… use timers instead.

A Lambda shortcut version of a non-blocking timeout timer command/function works nicely.

// Timed Lambda Function - Timeout countdown
  timer.setTimeout(500L, []() {  // Run following command(s) after 500ms
    Blynk.virtualWrite(V1,0);
  });  // END Timer Function
1 Like

I have something wrong; /

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


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

    SimpleTimer timer;
    int Value;
    int Relay = D3; 

    BLYNK_WRITE(V2) 
    {
      Value = param.asInt();

    if ( Value == 1 ) 
      {
        timer.setTimeout(500L, RelayON);
        timer.setTimeout(600L, RelayOFF);
        timer.setTimeout(700L, RelayON);
        timer.setTimeout(800L, RelayOFF);
        timer.setTimeout(900L, RelayON);
        Blynk.virtualWrite(Value,0);
        Blynk.syncVirtual(V2);
          } 
     else {
      timer.setTimeout(1200L, RelayOFF);
      }
      
    } 
    BLYNK_CONNECTED () {
    Blynk.syncAll ();
    }


    void setup()
    {
    Serial.begin(9600);
    pinMode(Relay,OUTPUT);
    digitalWrite(Relay,LOW);
    Blynk.begin(auth, ssid, pass);
    }

    void RelayON() {
      digitalWrite(Relay, HIGH);
    }  
    void RelayOFF() {
      digitalWrite(Relay, LOW);
    }  

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

What is it EXACTLY that you are now trying to achieve, and what results are you actually seeing?
Why do yo now have multiple timeout timers when you’ve said that your aim is to…

What is the purpose of this line:

Pete.

It works for me but I would like to change “delay” on “timer”

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


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

SimpleTimer timer;
int Value;
int Relay = D3; 

BLYNK_WRITE(V2) 
{
  Value = param.asInt();
} 

BLYNK_CONNECTED () {
Blynk.syncAll ();
}


void setup()
{
Serial.begin(9600);
pinMode(Relay,OUTPUT);
digitalWrite(Relay,LOW);

Blynk.begin(auth, ssid, pass);
timer.setInterval(500L, Relay_1); 
}
void loop()
{
timer.run();
Blynk.run();
} 

void Relay_1()
{
if ( Value == 1 ) 
  {
  digitalWrite(Relay,HIGH);
  delay(500); 
  digitalWrite(Relay,LOW);
  delay(500); 
  digitalWrite(Relay,HIGH);
  delay(500); 
  digitalWrite(Relay,LOW);
  delay(500); 
  digitalWrite(Relay,HIGH);
  delay(500); 
  digitalWrite(Relay,LOW);
  Blynk.virtualWrite(V2,0);
  Blynk.syncVirtual(V2); /// Virtual pin update
   delay(500); 
  } 
 else {
  digitalWrite(Relay,LOW);
  }
}

Now it’s 100% clear (not)!

Pete.

I do not understand

Neither do I.
I’d asked you to explain…

and your reply was…

I have no idea what this means, so I can’t help until you explain in MUCH more detail.

Pete.

I want the relay to work intermittently ON-OFF-ON-OFF-ON-OFF. I don’t know how to replace “delay (500)” with “SimpleTimer”

Go back to your previous code.
Your problem there was that you had multiple timeout timers that I think l you were expecting would run one after another.
Because they are non blocking the timers run at the same time, so your LED would probably have turned on for 500ms, off for 100ms, on for 100ms etc.

It’s better to trigger the next timeout timer at the end of the function that is called when the first timer ends.

Pete.

Are you able to write me an example how to do it?

I’m not going to write your code for you, but other forum members may wish to do that.

If you have a go at writing it yourself, and can articulate what it is that you are trying to achieve and in which ways the code is failing to do this, then I’d be happy to critique your code and point you in the right direction.

Pete.

1 Like

Now it works for me, but it does not turn off the relay and does not change the state of V2 = 0

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

char auth[] = "";
char ssid[] = "";
char pass[] = "";
int SWITCH;
#define Relay D3 

BlynkTimer timer;
BLYNK_CONNECTED () {
Blynk.syncAll ();
}


void setup() {
  pinMode(Relay, OUTPUT);
Blynk.begin(auth, ssid, pass);
}


BLYNK_WRITE(V2)
{
  SWITCH = param.asInt();
  if (SWITCH == 1 ) {
    digitalWrite(Relay, HIGH); 
    timer.setTimer(1000L, RelayON, 2); 
    Blynk.virtualWrite(SWITCH,0);
    Blynk.syncVirtual(SWITCH);
  }  
} 


void RelayON() {
  digitalWrite(Relay, LOW);
  timer.setTimeout(500L, RelayOFF);
} 
void RelayOFF() {
  digitalWrite(Relay, HIGH);
} 


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

I’ve asked you before what the purpose of this line is, and you haven’t answered that question.

Pete.

To update the status

What it actually does is to trigger BLYNK_WRITE(V1) which doesn’t exist in your code.

Also, this…

Will set the widget attached to V1 (if there is one) to 0 or off. I doubt if that is what you want.

Pete.

i want to set pin v2 to 0

So it has to be ?

BLYNK_WRITE(V2)
{
  SWITCH = param.asInt();
  if (SWITCH == 1 ) {
    digitalWrite(Relay, HIGH); 
    timer.setTimer(1000L, RelayON, 2); 
    Blynk.virtualWrite(V2,0);
    Blynk.syncVirtual(V2);
  }  
}

Why do you want to force the BLYNK_WRITE(V2) callback to trigger again, passing a param.asInt value of 0 to the function? It makes no sense and serves no productive purpose.

Pete.

So this is how it should be?
But how to turn on and off the relays ON (1sec) OFF (1sec) ON (1sec) OFF?

BLYNK_WRITE(V2)
{
  SWITCH = param.asInt();
  if (SWITCH == 1 ) {
    digitalWrite(Relay, HIGH); 
    timer.setTimer(1000L, RelayON, 2); 
    Blynk.virtualWrite(V2,0);
       }  
}

What is the relay attached to D3 doing at the moment?

Pete.