Virtual button to control relay on GPIO 16

I’m trying to build a project to turn on a relay for 10 seconds when a virtual (push type) button on v0 is pressed
hardware - ESP8266
relay is connected to GPIO16(D0)
im using the blynk Edgent_ESP8266 code

You should read this…

And this…

and pay particular attention to the bit about timeout timers.

Pete.

1 Like

thanks for the help.
I read the articles, and this was what i came up with

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_FIRMWARE_VERSION        "1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG
int wifists=2;
#include "BlynkEdgent.h"
#define USE_NODE_MCU_BOARD

BLYNK_WRITE(V0)
{
  int pin = param.asInt();
  if (param.asInt()){
  digitalWrite(16, LOW);
  timer.setTimeout(10000L, [] () {digitalWrite(16, HIGH);} );
  } else {
  digitalWrite(16, HIGH);  
  }
}

void setup() {
  Serial.begin(115200);
  delay(100);
  pinMode(16, OUTPUT);
  pinMode(wifists, OUTPUT);
  digitalWrite(16,HIGH);
  BlynkEdgent.begin();
}
BLYNK_CONNECTED()
{
 
}
void loop() {
  BlynkEdgent.run();
  timer.run();
  
  if (Blynk.connected()) {
    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet
  } else {
    digitalWrite(wifists, LOW);
  }

}

the code works fine now, just that the button stays on even after 10 secs;though the relay turns off at 10 secs
i need the relay to stay on for 10 secs no matter how long i press the button for
TIA

You should update the button status using the Blynk.virtualWrite function.

You can also try automation

1 Like

Thanks, works now :). Is there any way to adjust the time interval of the delay of the timer using any widgets on the dashboard?
TIA

Yes, you can use a parameter as the timeout period, maybe set from a slider or numeric input widget.

Pete.

1 Like

any example code for this?

There’s a snippet of code in the BltnkTimer link I provided earlier.

Pete.

1 Like
BLYNK_WRITE(V0)
{
  
  int pin = param.asInt();
  if (param.asInt()){
  digitalWrite(16, LOW);
  timer.setTimeout(10000L, [] () {digitalWrite(16, HIGH);Blynk.virtualWrite(V0,LOW);} ); //<--- What is the purpose of the "L" after timer.setTimeout(10000 
  } else {
  digitalWrite(16, HIGH);  
  }

}

What is the purpose of the “L” after timer.setTimeout(10000

It defines it as a long Integer.

Pete.

i had tried it without the L and it worked the same way ; does it make any difference

#define BLYNK_DEVICE_NAME ""
#define BLYNK_FIRMWARE_VERSION        "1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG
int wifists=2;
#include "BlynkEdgent.h"
#define USE_NODE_MCU_BOARD

BLYNK_WRITE(V7)
{
 int slider = param.asInt();
}
  

BLYNK_WRITE(V0)
{
  
  int pin = param.asInt();
  if (param.asInt()){
  digitalWrite(16, LOW);
  timer.setTimeout(slider, [] () {digitalWrite(16, HIGH);Blynk.virtualWrite(V0,LOW);} );
  } else {
  digitalWrite(16, HIGH);  
  }

}


void setup() {
  Serial.begin(115200);
  delay(100);
  pinMode(16, OUTPUT);
  pinMode(wifists, OUTPUT);
  digitalWrite(16,HIGH);
  BlynkEdgent.begin();
}
BLYNK_CONNECTED()
{
 
}
void loop() {
  BlynkEdgent.run();
  timer.run();
  
  if (Blynk.connected()) {
    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet
  } else {
    digitalWrite(wifists, LOW);
  }

}```
This is my updated code.... it doesnt compile and gives the error " 'slider' was not declared in this scope"

int typically stores integers in the range of -2147483648 to 2147483647 and requires 4 bytes (32 bits) of memory.
long int can store integers in the range of -9223372036854775808 to 9223372036854775807 and requires 8 bytes (64 bits) of memory.

1 Like

The slider is a local variable. A local variable is a variable that is declared inside a function or a block of code, and it can only be accessed within that function or block. You should use global variable instead.

soo… where should i include the slider variable

Outside the function.

can you please explain further

sorry im new to these stuff

Declaring this as an integer within a function means that its value will always be zero outside of that function, because it’s a local variable.

What is this doing in your void loop?

Pete.

Int slider;

BLYNK_WRITE(V7)
{
 slider = param.asInt();
}

Thats to turn on a led if the esp is connected to the server