Virtual button to control relay on GPIO 16

If this was in response to my question about your void loop then you need to re-think your code structure.

Pete.

Yes it was.
I dont get what you mean by re thiink though

You should read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

If you’d read the first part of my Timers tutorial you’d understand.

Pete.

I need to keep this part of code in the loop as the led wouldnt go off when disconnected from server if placed in the setup;as it only runs as the esp is powered

    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet
  } else {
    digitalWrite(wifists, LOW);
  }

@PeteKnight
@John93
This is my code as of now

#define BLYNK_DEVICE_NAME ""
#define BLYNK_FIRMWARE_VERSION        "1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

#include "BlynkEdgent.h"
#define USE_NODE_MCU_BOARD

const int wifists=2;
int slider;

BLYNK_WRITE(V7)
{
 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()
{
 if (Blynk.connected()) {
    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet
  } else {
    digitalWrite(wifists, LOW);
  }
}
void loop() {
  BlynkEdgent.run();
  timer.run();
}

The problem i face now is, when i press the button on the app, the esp goes offline.
What can I do to solve this issue. the code runs perfectly fine if i use a fixed value for the timeout value instead of the slider on the app
Like this

int slider;

BLYNK_WRITE(V7)

{

 slider = param.asInt();

}

 

BLYNK_WRITE(V0)

{

  int pin = param.asInt();

  if (param.asInt()){

  digitalWrite(16, LOW);

  timer.setTimeout(10000, [] () {digitalWrite(16, HIGH);Blynk.virtualWrite(V0,LOW);} );

  } else {

  digitalWrite(16, HIGH);  

  }

}

TIA

No you don’t. You can use a timer to call that code.

Your BLYNK_CONNECTED() function makes no sense at all.
You should remove your code from that function and add a Blynk.syncVirtual(V7) command to get the latest value from your slider widget.

I suspect that the sketch is crashing because you’re using a 0 value for your slider integer and BlynkTimer doesn’t like that.

Pete.

#define BLYNK_TEMPLATE_ID ""

#define BLYNK_DEVICE_NAME ""

#define BLYNK_FIRMWARE_VERSION        "1.0"

#define BLYNK_PRINT Serial

//#define BLYNK_DEBUG

#define APP_DEBUG

const int wifists=2;

int slider;

#include "BlynkEdgent.h"

#define USE_NODE_MCU_BOARD

BLYNK_WRITE(V7)

{

 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()

{

 if (Blynk.connected()) {

    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet

  } else {

    digitalWrite(wifists, LOW);

  }

  Blynk.syncVirtual(V7);

}

void loop() {

  BlynkEdgent.run();

  timer.run();

}

I added a Blynk.syncVirtual(V7); and the code seems to run fine; how do i use a timer to call this code

{
 if (Blynk.connected()) {
    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet
  } else {
    digitalWrite(wifists, LOW);
  }
  Blynk.syncVirtual(V7);
}

hope that the above code makes sense.do you mind checking it please :slight_smile:
Thanks for the help

No, not really, as I said before…

Only the last line of code should be in this BLYNK_CONNECTED function.

I’ve already linked you to a tutorial on timers in post #2 and @John93 has already linked you to the ‘keep your void loop clean’ tutorial in post #23
If you’d read either of these you wouldn’t be asking this question.

Pete.

@PeteKnight

timer.run();

so this line should be included in the BLYNK_CONNECTED function
like this?

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_FIRMWARE_VERSION        "1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG

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

BLYNK_WRITE(V7)
{
 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);  
  }
  {
 if (Blynk.connected()) {
    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet
  } else {
    digitalWrite(wifists, LOW);
  }
  Blynk.syncVirtual(V7);
}
}
void setup() {
  Serial.begin(115200);
  delay(100);
  pinMode(16, OUTPUT);
  pinMode(wifists, OUTPUT);
  digitalWrite(16,HIGH);
  BlynkEdgent.begin();
  
}
BLYNK_CONNECTED()
{
  timer.run();
}
void loop() {
  BlynkEdgent.run();
  
}

Timer.run() must be in the void loop
Blynk.syncVirtual(V7) must be in BLYNK_CONNECTED()

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_FIRMWARE_VERSION        "1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG

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

BLYNK_WRITE(V7)
{
 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);  
  }
  {
 if (Blynk.connected()) {
    digitalWrite(wifists, HIGH);//turn on wifi status led if device isconnected to internet
  } else {
    digitalWrite(wifists, LOW);
  }
  
}
}
void setup() {
  Serial.begin(115200);
  delay(100);
  pinMode(16, OUTPUT);
  pinMode(wifists, OUTPUT);
  digitalWrite(16,HIGH);
  BlynkEdgent.begin();
  
}
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V7);
}
void loop() {
  BlynkEdgent.run();
  timer.run();
}

This is my code now… but the wifi status led doesnt turn on (even though the blynk.console website says the device is online) until i turn on and off a button on the app

The way that you’re hacking this code around is taking it to a point where none of it will work.

You would be far better taking time to read the links that are provided for you, and to read the comments that are posted - in detail.

I have absolutely no idea how you msanaged to interpret this…

As meaning that you should have timer.run in the BLYNK_CONNECTED function, or that you should take the existing code from BLYNK_CONNECTED and dump it into BLYNK_WRITE(V7)

Take time to read and understand!

Pete.

@PeteKnight what about the code i sent at last (post #32)
ive put Timer.run() in the void loop and
Blynk.syncVirtual(V7) in BLYNK_CONNECTED(), just as @Blynk_Coeur said

What is the purpose of this code in BLYNK_WRITE(V0) ?

When do you think BLYNK_WRITE(V0) will trigger, and do you think it’s possible for it to trigger is the device isn’t connected to the Blynk server?

Instead of just dumping more code into the discussion, how about answering these questions?

Pete.

Pete.

Sorry im new to the platform…
BLYNK_WRITE(V0) will trigger when the button is pressed?
… so the

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

should go into the BLYNK_CONNECTED()?

I’m aware of that, and that’s why I gave you links to tutorials in post #2 but you seem unwilling to read them.

When do you think BLYNK_CONNECTED is triggered?

Pete.

When the esp connects to the server

Correct. It runs ONCE when the device connects or re-connects to the server.

Armed with that knowledge, why would you want to put that code in BLYNK_CONNECTED ?

Pete.

…so, what goes into BLYNK_CONNECTED