BLYNK
BLYNK.IO       📲 GETTING STARTED       đź“— DOCS       âť“HELP CENTER       👉 SKETCH BUILDER

Reading data from blynk to be used in timer.setTimeout()

Hello There
I am working on a project where I need to control the intensity of LED1 whenever I want, while turn on the LED2 only after a set time interval (Input from Slider).
For this I am using WeMos D1 R1 ESP8266 board.

The problem is I can’t figure out how to read the value from V2 and use this inside the timer.setTimeout( value from V2, turn_on)
Right now it shows that valueV2 is not defined.

I am new to Blynk and IOT, so sorry in advance if my terminologies are not proper.


/* Fill-in your Template ID (only if using Blynk.Cloud) */
//#define BLYNK_TEMPLATE_ID   "YourTemplateID"


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

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

//Reading value from slider and changing intensity of LED1
BLYNK_WRITE(V1)
{
  int pinValue= param.asInt();
  analogWrite(D3,pinValue);
}

//Reading int value from slider and using that value as a duration till which a function executes
BLYNK_WRITE(V2)
{
   int valueV2= param.asInt();
}

BlynkTimer timer;

void turn_on()
{
  digitalWrite(D5,HIGH);
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setTimeout(valueV2*1000, turn_on);
}

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

You shouldn’t be using a timeout timer in this way. You probably need to use a timeout timer in a lambda function.

Also,you need to learn about variable scope, as variables like this…

Is local to the BLYNK_WRITE(V2) function, and it can’t be used outside of that function.

Pete.

I went through the article you attached above. It was very informative. In the updated code, I will make sure to include the LED2 turn_on command through the lambda function itself.
But the problem remains the same. I don’t want declare a fixed value inside the timer.setTimeout() function. Neither, do I want to declare a variable that I won’t be able to control from Blynk app itself.

Basically, I want to give an input from blynk (for example- 30) and the LED2 should turn_on exactly 30 minutes from the time the board was first activated. This value (30) will be changed for each operation. Thus, I can’t declare a variable in the code itself.
Is there any widget or feature in Blynk that I am not aware of and might help me to get the above results.
PS- I looked into timer and time input but they don’t exactly serve the above purpose.
Thanks for such a quick response above. :star_struck: :star_struck:

Of course you can. You declare the global variable, then assign a value to it in the BLYNK_WRITE(vPin) function for the slider, then use this variable (multiplied by 60,000 to convert from minutes to milliseconds) in your timeout timer.

Pete.

Is this what you meant?
If no, them I am really sorry. I couldn’t completely grasp how to assign the value to the global variable.

If yes, then I already executed this code once and now my board keeps disconnecting and reconnecting every 4-5 seconds. When I comment out the setTimeout function then the board works fine.


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

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

int break_time;

//Reading value from slider and changing intensity of LED1 (D4)
BLYNK_WRITE(V1)
{
  int pinValue= param.asInt();
  analogWrite(D4,pinValue);
}

//Reading int value from slider and using that value as a duration till which a function executes
BLYNK_WRITE(V2)
{
   int break_time= param.asInt();
}

BlynkTimer timer;

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setTimeout(break_time*1000, []() 
  {  
    // When the timer completes, any code here will be executed
    digitalWrite(D5,HIGH);
  });
}



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

Yes, this is correct.

The problem is that you have the timeout timer in your void setup.
void setup runs once at startup, at which point `break_time" will almost certainly be zero.

You need your lambda timer inside whichever function you want to use to trigger LED2 to come on. Having some way of turning LED2 off afterwards might also be handy.

You also need to declare your pins as outputs, using a pinMode statement in void setup.

Pete.