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

Button changes from Python but not the physical pin

In my setup I have the following parts:

  • local blynk server

  • flashed sonoff controlling a light

  • blynk app with a button

  • python program running on an RPI

The blynk app has a button that controls the light attached to the sonoff. This works without a problem. In the python program I wish to see the state of the light (mirroring the button in the blynk app) as well as change the light. As a test I have made a small program that changes the light every 5 seconds, see below.

If I change the light with the app button, the light actually changes via the sonoff device and I get a notification via the write_handler in the python program. But if I change the light from the python program via virtual_write, the button in the blynk app does change, but the actual light does not change. How can I make the sonoff actually change the light when I change it in the python program? Additionally, I had expected the read_handler function to be called from the blynk server, but it is never called, why?

import blynklib
import threading

blynk = blynklib.Blynk("My token", server='My local server', port=8080, heartbeat=15)

current_state = 0

# This works, but only for the button in the app, not the actual light attached to pin V2 of the Sonoff
def change_button():
    global current_state
    global blynk
    if current_state == 0:
        current_state = 1
    else:
        current_state = 0
    blynk.virtual_write(2, current_state)
    # restart the timer
    timer = threading.Timer(5.0, change_button)
    timer.start()

# This function is never called
@blynk.handle_event("read V2")
def read_handler(vpin):
    print('read_handler')
    blynk.virtual_write(2, current_state)

# This works
@blynk.handle_event("write V2")
def write_handler(pin, value):
    print('state of V2 is set to ', value)
    global current_state
    current_state = value

# just a timer to make the button change every 5 seconds
timer = threading.Timer(5.0, change_button)
timer.start()

while True:
    blynk.run()

As with C++ the Python READ (widget controlled flow) and WRITE (code controlled flow) functions are different ways of controlling the data flow… but only one or the other can be used per vPin (based on the widget being set to a Reading Rate or PUSH).

You appear to be trying to use both methods on the same vPin… I don’t think that will work… although Blynk’s Python commands are not well documented, so perhaps if the widget is set to a reading rate as the primary flow control, then the write function can change the value?? Not really sure if/how that can help with whatever you are trying to do…

On that note, typically you could just control the Sonoff from the App directly (perhaps with a routine to also use it’s physical button)… and leave the RPi as a seperate device controlling separate things.

Cross-device data/control sharing is usually done with Bridge… but Blynk’s Python Library doesn’t seem to have that function.