Push widgets update with different frequency using Python set_user_task() or NodeJS

Hello to All Blynkers,

first I have to thank you for this super-easy library for iot projects. It’s real great job.

I started a month ago learning the blynk library using nodejs and python as well. As my project grows up and I use more widgets for many sensors I came into the following issue when I deal with blynk for python library which I found https://github.com/vshymanskyy/blynk-library-python.

What I want to do is to use user tasks in the same python script for updating more than one widgets with different reading rate eachone using push. I followed the easy example provided here https://github.com/vshymanskyy/blynk-library-python/blob/master/examples/07_user_task.py expanding it with a second set.user_task() function. The problem I face is that only one of the two user_task calls works at a time and this is the last one call.

My code looks like this


import BlynkLib
import time
BLYNK_AUTH = 'YourAuthToken'
blynk = BlynkLib.Blynk(BLYNK_AUTH)
def my_user_task():
    print('Action')
    blynk.virtual_write(2, time.ticks_ms() // 1000)
def my_user_task2():
    print('Action 2')
    blynk.virtual_write(3, time.ticks_ms() // 1000)

blynk.set_user_task(my_user_task, 3000)
blynk.set_user_task(my_user_task2, 5000)

blynk.run()

I’m not sure if this is the right way to do that and also I don’t want to use @blynk.VIRTUAL_READ for each widget with different reading rate as this way doesn’t update the superchart when you close the aplication.
What should I do to push updates to the widgets with different frequency on the same script?

Thank you in advance,
reset4

FYI, Blynk Python support is in Alpha… probably only @vshymanskyy really understands how it works :stuck_out_tongue:

But I think :thinking: this is the part that you need to make a distinction from the other task. i.e.

blynk.set_user_task(my_user_task, 3000) <–1st task

blynk.set_user_task(my_user_task2, 5000) ← 2nd task. Note the placement of the 2 to match your def my_user_task2():

Thank you for your reply. I made a typo posting the code here but not in my python script. So I will correct this right now.

**Note python blynk.set_user_task2(my_user_task, 5000) corrected to python blynk.set_user_task(my_user_task2, 5000)

Comparing the nodejs and python blynk libraries, as the python library is in the alpha version, do you propose the use of nodejs for better support and new features?

Well… it is probably in Beta stage :stuck_out_tongue: so… better, but still a lot of experimenting to get various Blynk commands and widgets to work properly… but there do seem to be a few more users working with Node.js.

For maximum support use C++ and an ESP. ESP32 has more features but some don’t work and some work badly. ESP8266 like WeMos D1 Mini is by far the easiest and also the cheapest.

I totally agree… but I suspect the OP is referring to RPi hardware (although unstated)… I don’t find the C++ option for it any clearer than Node.JS or Python… as in requires a big learning curve. And in that I have found javascript to be the better supported for web resources and Blynk so far.

Python is cross platform but yes support for any language on the Pi is currently quite poor and not for beginners that need assistance.

Well for Blynk it is. I am currently running stuff on mine with Linux, C+(+), JS and Python… in fact one of my Blynk scripts interacts or runs some of all four :stuck_out_tongue: Now that is confusing for a non-programmer (or at least totally untrained) like me :sweat_smile:

I really hope Blynk ramps up official support for the RPi. meanwhile I am working on a code testbench project, with hopes of offering some js examples for the Sketch Builder. Not an easy task though :weary:

Yes I meant specifically Blynk.

Pi itself has excellent support for a multitude of languages.

Why blynk python example on user task 07_user_task.py runs my raspberry processor about 99%? Is this normal?

Read answer here…

Thank you for your reply.
This method (set user task) that push the value to a widget every time interval at the application works well with the superchart (no need to have the application open to keep the history of a vpin in the superchart) I would like to ask you if there is a similar function to nodejs blynk library. I searched in the examples provided to nodejs but I havent found something similar so far.

Yes…

You can look at what various examples are available in this GitHub location…

And there are a few other NodeJS examples kicking around this forum, some from me.

In a nutshell…

var DisplaySeconds = new blynk.VirtualPin(9);  // Setup Display Widget for Seconds
var DisplayMinutes = new blynk.VirtualPin(11);  // Setup Display Widget for Minutes
var DisplayHours = new blynk.VirtualPin(12);  // Setup Display Widget for Hours

// Clock Time Displays
DisplaySeconds.on('read', function() {  // Widget calls for data
  DisplaySeconds.write(new Date().getSeconds());  // Sends the seconds 0-59 to the Display Widget
});
DisplayMinutes.on('read', function() {  // Widget calls for data
  DisplayMinutes.write(new Date().getMinutes());  // Sends the minutes 0-59 to the Display Widget
});
DisplayHours.on('read', function() {  // Widget calls for data
  DisplayHours.write(new Date().getHours());  // Sends the hours 0-23 to the Display Widget
});

Thank you for the reference code.

I have worked on this but I think this approach requires an always open application to keep the history of a widget value to the superchart. I’m not sure if I am missing something but I think that in order to have all the history of a virtual pin to the superchart you need to follow the similar approach as the method blynk.set_user_task(my_user_task, 3000) in python.
blynk.set_user_task(my_user_task, 3000).

with the nodejs approach and using the following method with reading rate of the widget to some sec (not push), I have missing points to the superchart when I close my app.

v9.on('read', function() {
  v9.write(new Date().getSeconds());
});

Opps, sorry… I am still learning NodeJS myself and forgot I had left those as widget polled for my own reference :blush:

If setting a Display Widget Widget to PUSH (required for the SuperChart) and sending sensor data all the time is what you want, then you can use these commands below to send the data, but I have not yet determined how to do it on a regular basis (AKA Timer) but that part is just Googling for methods of NodeJS timers, there is not really a Blynk thing for that.

Here is a routine that watches a GPIO pin for simple ON OFF input and toggles a couple of Widget LEDs accordingly… same “send data to widget” using blynk.virtualWrite(vPin,Data) command principle as what you are looking for… BUT you never use the V in the vPin.

const Gpio = require('pigpio').Gpio;
const button = new Gpio(17, {mode: Gpio.INPUT, pullUpDown: Gpio.PUD_DOWN, edge: Gpio.EITHER_EDGE});

button.on('interrupt', function (value) {
  if (value  == 0) {
    blynk.virtualWrite(2, 0);  // V2 Widget (BLUE) LED off
    blynk.virtualWrite(3, 255);  // V3 Widget (RED) LED on
  }   else if (value == 1) {
    blynk.virtualWrite(2, 255);  // V2 (BLUE) Widget LED on
    blynk.virtualWrite(3, 0);  // V3 (RED) Widget LED off
  }
});

And I guess this would be the equivalent of a BLYNK_WRITE() function… In my case rebooting the RPi based on a Button Widget press.

var RPiReboot = new blynk.VirtualPin(20);  // Setup Reboot Button

// Run RPi Reboot Command
RPiReboot.on('write', function(param) {  // Watches for V20 Button
  if (param == 1) {  // Runs the CLI command if the button on V520 is pressed
    process.exec('sudo /sbin/shutdown -r', function (msg) { console.log(msg) });
}
});

Hopefully something here will help your quest, at least with NodeJS :wink: