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

Blynk.delay() function

Hello, I have run into a lot of times that i need a delay. Yes, i know it’s better to use a timer but in some cases that just doesn’t work. I have made my own work around currently but think that it would be nice to have it within the library.

This is the work around that I’ve made:

void Blynk_Delay(int milli){
   int end_time = millis() + milli;
   while(millis() < end_time){
       Blynk.run();
   }
}

It seems to work super good, but it always makes the code a little bit more longer which to me can be very annoying, i like clean simple codes.

I hope this might help somebody out :slight_smile:

3 Likes

@FunguyPro
Super! Can I add something?
See:

void Blynk_Delay(int milli){
   int end_time = millis() + milli;
   while(millis() < end_time){
       Blynk.run();
       yield();
   }
}
1 Like

Great!

Very nice.
I think this function maybe very useful.

It is already available for a long time, but to use this, you need #define BLYNK_EXPERIMENTAL.

What is already available, the Blynk_Delay ? PLease expalin.
Thanks!

@vshymanskyy is there a place I can find documentation for the experimental functions?

Actually no problem to get documentations right from the code: https://github.com/blynkkk/blynk-library/blob/master/src/Blynk/BlynkApi.h

In this file all Blynk functions are documented using Doxygen notation. Connection-specifc functions (like begin() are not included).

@vshymanskyy, thanks

Hello,

I am using ESP8266 and this function (Blynk_Delay) works better.
I have added definitions:

#define BLYNK_PRINT Serial
#define BLYNK_EXPERIMENTAL

but it seems not to work.
So I have changed all my delay like this.

//delay(1000);
Blynk_Delay(1000);

void Blynk_Delay(int milli){
   int end_time = millis() + milli;
   while(millis() < end_time){
       Blynk.run();
       yield();
   }
}

Got any idea how to troubleshoot?

Hello, I came into trouble when the device is not connected, thus the delays became longer.
You may check if the device is connected to the server.
I have modified a bit. Please comment.

void Blynk_Delay(int milli)
{
  int end_time = millis() + milli;
  while (millis() < end_time)
  {
    if (Blynk.connected())
    {
      Blynk.run();
    }
    yield();
  }
}
1 Like

@mikekgr Just wondering, what is the function of yield() do? I can’t find it in Blynk documentation. Thanks

yield() is an ESP function to stop the software WDT kicking in.

1 Like

@Costas already gave answer on this, but let me to explain you with my way…
ESP8266 implement Watch Dog Timer that is, an auto increasing timer that IF pass a certain value -overflows- ( that means your sketch is stuck some how or there are big delay… ) then ESP8266 do restart itself like you have push hardware reset button.
yield() call internal ESP8266 functions ( like WiFi IP Stack etc ) in order to get run all pending internal system functions and at the same time take care WDT not to overflows and auto reset the ESP8266.

Is the above ok to you? If you have any other related question dont hesitate to ask me.

Best Regards,
Mike Kranidis

5 Likes

DOG not DOC. Feed the dog.

1 Like

Yes, I corrected my typo. Thanks Costas. ( you know all of us, the non native English speakers… )

Thanks @mikekgr & @Costas for the good explanation, so it’s a mechism to avoid so called “hang” issue on ESP, but yet come for the following question, isn’t Blynk.run function already has this function within its code?

There is software hanging and hardware hanging. The WDT is more hardware related while Blynk is more of the software side.

In b4 getting called out…

Necromancer here!

Just a quick question Costas, I see plenty of code examples with the loop constisting of
Blynk.run();
Timer.run();

but rarely a yield(), is this mostly done in the timer or blynk.run? Or just not needed somehow?

@Fettkeewl if your code is well designed there shouldn’t be any need for yield() but you will see it from time to time. You will also see delay(1) and it is acceptable up to perhaps 200 but ideally no higher, above this then SimpleTimer (or Photon equivalent etc) is recommended.