Second 'Blynk.virtualWrite' command takes much more time than first one

Hi.

I found that there is delay when using multiple ‘Blynk.virtualWrite’ command in a row.

As you can see in serial monitor, first virtualWrite command takes only 2 millisecond.
But, second and third virtualWrite command takes 68~69 millisecond.

It seems like that second and third virtualWrite is blocked by previous virtualWrite command.

  1. Could you tell me why this happens?

  2. If this happens because previous virtual command blocks next virtualWrite command, Is there a nonblocking virtualWrite function?

  3. Is there a way to minimize execution time when using multiple virtualWrite command in a row?

#define BLYNK_TEMPLATE_ID "***"
#define BLYNK_TEMPLATE_NAME "***"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG

#include "BlynkEdgent.h"

BlynkTimer timer;

void doVirtualWrite(){
  unsigned long start, end;

  start = millis();
  Blynk.virtualWrite(V0, 1);
  end = millis();
  Serial.printf("First virtualWrite Time: %d\n", end - start);

  start = millis();
  Blynk.virtualWrite(V1, 1);
  end = millis();
  Serial.printf("Second virtualWrite Time: %d\n", end - start);

  start = millis();
  Blynk.virtualWrite(V2, 1);
  end = millis();
  Serial.printf("Third virtualWrite Time: %d\n", end - start);
}

void setup()
{
  Serial.begin(115200);
  delay(100);

  timer.setInterval(5000L, doVirtualWrite);

  BlynkEdgent.begin();
}

void loop() {
  timer.run();
  BlynkEdgent.run();
}
[192] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on ESP32

 #StandWithUkraine    https://bit.ly/swua


----------------------------------------------------
 Device:    ***
 Firmware:  0.1.0 (build Nov 23 2023 15:30:31)
 Token:     *** - •••• - •••• - ••••
 Platform:  ESP32 @ 240MHz
 Chip rev:  3
 SDK:       v4.4.6-dirty
 Flash:     4096K
 Free mem:  243212
----------------------------------------------------

>[234] INIT => CONNECTING_NET
[234] Connecting to WiFi: ***
[3261] Using Dynamic IP: ***
[3262] CONNECTING_NET => CONNECTING_CLOUD
[3271] Connecting to blynk.cloud:443
[4568] Certificate OK
[4881] Ready (ping: 311ms).
[4950] CONNECTING_CLOUD => RUNNING
First virtualWrite Time: 3
Second virtualWrite Time: 68
Third virtualWrite Time: 69
First virtualWrite Time: 2
Second virtualWrite Time: 69
Third virtualWrite Time: 68
First virtualWrite Time: 2
Second virtualWrite Time: 68
Third virtualWrite Time: 67
First virtualWrite Time: 2
Second virtualWrite Time: 67
Third virtualWrite Time: 68
First virtualWrite Time: 2
Second virtualWrite Time: 68
Third virtualWrite Time: 68
First virtualWrite Time: 2
Second virtualWrite Time: 68
Third virtualWrite Time: 68

Have you tried this Group approach…

Pete.

Thank you for your reply.
But execution time is same whether using group or not.

#define BLYNK_TEMPLATE_ID "***"
#define BLYNK_TEMPLATE_NAME "***"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG

#include "BlynkEdgent.h"

BlynkTimer timer;

void doVirtualWrite(){
  unsigned long start, end;

  start = millis();
  Blynk.beginGroup();
  Blynk.virtualWrite(V0, 1);
  Blynk.virtualWrite(V1, 1);
  Blynk.virtualWrite(V2, 1);
  Blynk.endGroup();
  end = millis();
  
  Serial.printf("3 virtualWrite Time - Group: %d\n", end - start);
}

void setup()
{
  Serial.begin(115200);
  delay(100);

  timer.setInterval(5000L, doVirtualWrite);

  BlynkEdgent.begin();
}

void loop() {
  timer.run();
  BlynkEdgent.run();
}
[191] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on ESP32

 #StandWithUkraine    https://bit.ly/swua


----------------------------------------------------
 Device:   ***
 Firmware:  0.1.0 (build Nov 23 2023 16:50:32)
 Token:     *** - •••• - •••• - ••••
 Platform:  ESP32 @ 240MHz
 Chip rev:  3
 SDK:       v4.4.6-dirty
 Flash:     4096K
 Free mem:  243212
----------------------------------------------------

>[233] INIT => CONNECTING_NET
[233] Connecting to WiFi: ***
[3030] Using Dynamic IP: ***
[3031] CONNECTING_NET => CONNECTING_CLOUD
[3040] Connecting to blynk.cloud:443
[4374] Certificate OK
[4688] Ready (ping: 312ms).
[4757] CONNECTING_CLOUD => RUNNING
3 virtualWrite Time - Group: 141
3 virtualWrite Time - Group: 142
3 virtualWrite Time - Group: 141
3 virtualWrite Time - Group: 141
3 virtualWrite Time - Group: 143
3 virtualWrite Time - Group: 142
3 virtualWrite Time - Group: 143
3 virtualWrite Time - Group: 143
3 virtualWrite Time - Group: 144

Maybe it would help if you explained why this is an issue.
You can’t do more than 10 Blynk.virtualWrites (or similar) per second, so I assumed that your issue was about timestamps rather than overall speed.

Pete.

If I understand correctly, I can only send 10 request per second on single virtual pin.
But I can send 100 request per second to my device, as long as request number on same pin doesn’t exceed 10 request per second.

So, if I reduce interval of timer handler to 100ms, my device should send 30 request per second. (10 request per second on three virtual pin)

But when I run the code, I couldn’t send 30 request per second because timer doesn’t working correctly due to virtualWrite delay.
As you can see the timestamp in the serial, I can only send 15 request per second.(5 request per second on three virtual pin)

#define BLYNK_TEMPLATE_ID "***"
#define BLYNK_TEMPLATE_NAME "***"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG

#include "BlynkEdgent.h"

BlynkTimer timer;

void doVirtualWrite(){
  unsigned long start, end;

  start = millis();
  Blynk.beginGroup();
  Blynk.virtualWrite(V0, 1);
  Blynk.virtualWrite(V1, 1);
  Blynk.virtualWrite(V2, 1);
  Blynk.endGroup();
  end = millis();
  
  Serial.printf("3 virtualWrite Time - Group: %d\n", end - start);
}

void setup()
{
  Serial.begin(115200);
  delay(100);

  timer.setInterval(100L, doVirtualWrite);

  BlynkEdgent.begin();
}

void loop() {
  timer.run();
  BlynkEdgent.run();
}
19:19:15.981 -> [192] 
19:19:15.981 ->     ___  __          __
19:19:15.981 ->    / _ )/ /_ _____  / /__
19:19:15.981 ->   / _  / / // / _ \/  '_/
19:19:15.981 ->  /____/_/\_, /_//_/_/\_\
19:19:15.981 ->         /___/ v1.3.2 on ESP32
19:19:15.981 -> 
19:19:15.981 ->  #StandWithUkraine    https://bit.ly/swua
19:19:15.981 -> 
19:19:16.013 -> 
19:19:16.013 -> ----------------------------------------------------
19:19:16.013 ->  Device:   ***
19:19:16.013 ->  Firmware:  0.1.0 (build Nov 23 2023 19:16:41)
19:19:16.013 ->  Token:     *** - •••• - •••• - ••••
19:19:16.013 ->  Platform:  ESP32 @ 240MHz
19:19:16.013 ->  Chip rev:  3
19:19:16.013 ->  SDK:       v4.4.6-dirty
19:19:16.013 ->  Flash:     4096K
19:19:16.013 ->  Free mem:  243212
19:19:16.013 -> ----------------------------------------------------
19:19:16.013 -> 
19:19:16.013 -> >[235] INIT => CONNECTING_NET
19:19:16.056 -> 3 virtualWrite Time - Group: 0
19:19:16.056 -> [245] Connecting to WiFi: ***
19:19:17.368 -> [1592] Using Dynamic IP: ***
19:19:17.368 -> [1593] CONNECTING_NET => CONNECTING_CLOUD
19:19:17.414 -> 3 virtualWrite Time - Group: 0
19:19:17.414 -> [1603] Connecting to blynk.cloud:443
19:19:19.264 -> [3451] Certificate OK
19:19:19.669 -> [3864] Ready (ping: 411ms).
19:19:19.747 -> [3933] CONNECTING_CLOUD => RUNNING
19:19:19.951 -> 3 virtualWrite Time - Group: 207
19:19:20.152 -> 3 virtualWrite Time - Group: 209
19:19:20.355 -> 3 virtualWrite Time - Group: 207
19:19:20.574 -> 3 virtualWrite Time - Group: 206
19:19:20.761 -> 3 virtualWrite Time - Group: 207
19:19:20.978 -> 3 virtualWrite Time - Group: 206
19:19:21.197 -> 3 virtualWrite Time - Group: 207
19:19:21.382 -> 3 virtualWrite Time - Group: 207
19:19:21.600 -> 3 virtualWrite Time - Group: 210
19:19:21.818 -> 3 virtualWrite Time - Group: 207
19:19:22.035 -> 3 virtualWrite Time - Group: 209
19:19:22.220 -> 3 virtualWrite Time - Group: 207
19:19:22.436 -> 3 virtualWrite Time - Group: 209
19:19:22.654 -> 3 virtualWrite Time - Group: 207
19:19:22.856 -> 3 virtualWrite Time - Group: 208
19:19:23.058 -> 3 virtualWrite Time - Group: 207
19:19:23.264 -> 3 virtualWrite Time - Group: 206
19:19:23.483 -> 3 virtualWrite Time - Group: 207
19:19:23.653 -> 3 virtualWrite Time - Group: 206
19:19:23.897 -> 3 virtualWrite Time - Group: 208
19:19:24.084 -> 3 virtualWrite Time - Group: 208
19:19:24.304 -> 3 virtualWrite Time - Group: 207
19:19:24.508 -> 3 virtualWrite Time - Group: 207
19:19:24.726 -> 3 virtualWrite Time - Group: 207
19:19:24.929 -> 3 virtualWrite Time - Group: 208
19:19:25.145 -> 3 virtualWrite Time - Group: 205
19:19:25.331 -> 3 virtualWrite Time - Group: 210
19:19:25.561 -> 3 virtualWrite Time - Group: 209
19:19:25.748 -> 3 virtualWrite Time - Group: 207
19:19:25.966 -> 3 virtualWrite Time - Group: 207

I’m not sure whether those values were for Blynk Legacy or Blynk IoT, or whether they’ve changed since.

What’s the reason behind wanting to send so many commands per second from your device?

Pete.

It’s for Blynk IoT.

Actually, It’s for show you the problem I have in my other code.

In my other code, I have two timer.
Timer 1 is for creating 10Hz pulse.
Timer 2 is for sending 5 datas to blynk every 1 second.
Every time Timer 2 run it’s handler which has 5 blynkWrite function, timer 1 is blocked by timer 2 and pulse is crashed.

I didn’t upload my other code, because it is sperated to multiple files.
Should I upload my other code too?

The link you provided was to a comment posted in March 2020.
The Blynk IoT Beta programme didn’t start until 2021, so the comment was in relation to Blynk Legacy, not IoT.

No, but it may be helpful if you explain in detail the reason for needing to, or at least wanting to, upload so many data values so frequently.

Pete.

Oh. I didn’t know that. So Blynk Iot can run virtualWrite only 10 per second? not 100 request per second per device?

I send 5 data per second because I want to make sure data in my device keep sync with data in blynk server.
Do you think it’s bad approach?
Should I change logic to call virtualWrite only when data change?

I don’t know.

You still haven’t explained what your project is about, why you need to send so many data points per second, and why it’s necessary for your device and the Blynk server to be synchronised in this way.
Until you do then I can’t comment on the logic of your approach.

Pete.

Okay. I’ll try to explain my project as detailed as possible.

My device read pulse count generated from 2 other devices which are coin counter and bill counter.
And it send pulse count to blynk server, and generate pulse same number it read.
My device also take pulse count input from blynk app and generate that pulse too.

My device send input and output pulse count to blynk server using 4 virtual pins every second. So that user can see how many pulse is get inside and get outside.
And since pulse means money because it read pulse count from coin/bill counter, my device also send how many money has been collected to blynk server using another virtual pin every second

The reason why I send data every second is that it is the easiest way I can think of to show pulse count and collected money real time

Do you really think that the people viewing this data in the Blynk dashboard can really see these numbers changing 10 times per second?
In reality, updating these values less frequently will make the data more readable.

The sensible approach would seem to be to maintain an internal counter, then update the value(s) let’s say once every second, and reset the counter.

I do a similar thing with my wind speed meter, which generates about 30 pulses per second in a strong wind, and I store the number of pulses recieved in a 5 second period then upload the result (after converting this into equivalent wind speed reading) to Blynk.

Pete.

2 Likes

I think I might confuse you.

I’m not sending data 10 times per second.

I’m sending 5 data to blynk server once per every second.

If I only need to send 5 data to blynk server, then there is no problem.

But I also need to generate 10Hz pulse time to time. To generate 10Hz, I use software timer that has 50ms interval. Whenever timer expires, output level toggles so that it can generate pulse.

The problem is whenever my device send data to blynk sever, virtualWrite command takes almost 250ms. And since virtualWrite is blocking function, handler of software timer that generate pulse can’t be called. So the output pulse is crashed.

Currently, I fix this temporary by not calling virtualWrite function when pulse is generating. But if there is way to reduce virtualWrite execution time or non-blocking virtualWrite function, I don’t have to check whether pulse is generating now.

That seems to be a sensible approach.

Not as far as I’m aware.
I guess you could try using an ESP32 with different tasks running on different cores, but that’s getting quite deep and it depends how good your programming skills are.

Pete.

1 Like

Thank you for you reply.
I should try other ways to handle this starting from what you suggest.

Pete is right. To solve your issue, you need either to use different cores or use a different software approach that doesn’t rely/wait for virtualWrite to be finished/executed.

For our business clients, we do recommend using Blynk.NCP for that case - https://docs.blynk.io/en/blynk.ncp/overview.

Thank you for you reply.
When Blynk.NCP just came out, I just passed. But now there is reason to try it :slight_smile: