Evaluating a second virtual pin on Python in write-handler-event

Hello Everyone,

I´m using blynk for a while now to control my two garage doors. According to the linked tutorial, I was able to start and stop both doors (TuerL & TuerR) by using this code:


# TuerL control through V0 virtual pin
@blynk.on("V0")
def v0_write_handler(value):
#    global led_switch
    if int(value[0]) is not 0:
        GPIO.output(TuerL, GPIO.LOW)
    else:
        GPIO.output(TuerL, GPIO.HIGH)
        
# TuerR control through V1 virtual pin
@blynk.on("V1")
def v1_write_handler(value):
#    global led_switch
    if int(value[0]) is not 0:
        GPIO.output(TuerR, GPIO.LOW)
    else:
        GPIO.output(TuerR, GPIO.HIGH)
        

As travel of the door takes a while and this is very hard to judge if you are not in front of the door, I wanted to create a signal for 22 seconds after the start of the movement that says “doors might be busy” (“BESCHAEFTIGT”).

I did this using a sleep period for 22 seconds and this also works.
The only problem is, that I can’t stop the door or start the other one while the program is “sleeping”.
For this reason, I thought of using shorter sleep-periods within a while loop. But I have no idea how to code the conditions.

# TuerL control through V0 virtual pin
@blynk.on("V0")
def v0_write_handler(value):
#    global led_switch
    if int(value[0]) is not 0:
        GPIO.output(TuerL, GPIO.LOW)
        print('linke Tuer aktiv')
        iL=0
        blynk.virtual_write(4, 'BESCHAEFTIGT')
        while **???**:
            sleep(1)
            iL += 1
        blynk.virtual_write(4, 'BEREIT')
    else:
        GPIO.output(TuerL, GPIO.HIGH)
        print('linke Tuer passiv')

# TuerR control through V1 virtual pin
@blynk.on("V1")
def v1_write_handler(value):
#    global led_switch
    if int(value[0]) is not 0:
        GPIO.output(TuerR, GPIO.LOW)
        iR = 0
        blynk.virtual_write(4, 'BESCHAEFTIGT')
        while **???**:
            iR += 1
            sleep(1)
        blynk.virtual_write(4, 'BEREIT')
    else:
        GPIO.output(TuerR, GPIO.HIGH)
        print('rechte Tuer passiv')

Basically the conditions for “TuerL” are:

  • iL <22
  • V0 is 0
  • V1 is 0

Can you please advise me he the code for the while-loop should look like?

Thank you very much in advance.

Michael

I know virtually nothing about Python, but I a see why you’re having a problem.

The Blynk library used by the device only knows about changes made to virtual datastream values via the app when blynk.run() is executed.

blynk.run() instructs the device to go and do a handshake with the Blynk server. This does two things - (1) tells the server that the device is still alive and (2) picks-up info about changes to datastream values.
If a datastream value has changed then the call to blynk.run() will also cause the corresponding virtual pin write handler to be called.

The solution may be as simple as putting blynk.run() in your while loop (I don’t know enough about Python to know if that will work).

A better solution might be to use a non-blocking BlynkTimer (see the timer example in the Python library examples).
I’d probably have the timer call a function every second and if a flag that indicates that the door is currently moving is true then increment a counter variable. When the counter reaches 22 then clear the “door is moving” flag and reset your counter to zero.

Pete.

Hello Pete,

thank you very much for your response. Can you please tell me, which " timer example in the Python library examples" you mean?

I wasn´t able to find it so far.

Are you sure you’re using the correct Python library?

Here are the examples…

and the one I was referring to is 04_timer.py

Pete.