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.
Could you tell me why this happens?
If this happens because previous virtual command blocks next virtualWrite command, Is there a nonblocking virtualWrite function?
Is there a way to minimize execution time when using multiple virtualWrite command in a row?
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.
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)
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.
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?
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.
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.
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.
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 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.