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()