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

Newbie question - blynk.virtual_write - Python syntax

Before creating the topic

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.


void loop()

Sorry, having problems getting this onto the forum - bear with me please, it’s my first post.

Anyway, I have my Blynk app on an Andoid 8 Moto E5 and that seems to be fine. I have 3 buttons and one LED. All are virtual pins.
My code is written in Python and is running on a RPi 3B+.
The buttons operate as virtual writes to the Python code and they switch pins on the RPi and that works great!
My problem is getting the virtual read to work.
I have looked at the sample code in 02_virtual_read.py but I don’t see how to get the function to be called.

By the way, just to complicate matters the virtual pins make calls to MQTT, but that doesn’t appear to be part of my issue.

Here is my code …

import BlynkLib, time
from gpiozero import PWMLED
import paho.mqtt.client as mqtt

red = PWMLED(21)                                      # set up LEDs to pulse using PWM
yellow = PWMLED(20)                                   # for 201 r=3  y=4  g=2
green = PWMLED(16)                                    # for Pi3 r=21 y=20 g=16

BLYNK_AUTH = '61cc1d16f6e0490db628c25423adba54'       # Blynk code
blynk = BlynkLib.Blynk(BLYNK_AUTH)                    #Initialise Blynk
status = 0                                            # This is for the virtual read for the LED

mqttc=mqtt.Client()
mqttc.connect("192.168.1.203", 1883,60)               #sub Q on 203, port 1883, 60 sec keep alive

@blynk.VIRTUAL_WRITE(1)                               #Register Virtual Pin
def my_write_handler(value):
    global status
    if int(value)==1:
        red.value=1                                   # Switch on LED
        status=255
        print('ON  RED'+str(status))                  # Print update
        mqttc.connect("192.168.1.203", 1883,60)       # connect
        mqttc.publish("hello/world", "RED on" ,1)     # Publish to MQ try QOS 1
        mqttc.publish("highmount/heating", "R3on" ,1) # Publish to 199
        mqttc.disconnect()                            # disconnect
    else:
        red.value=0
        status=0
        print('OFF RED'+str(status))
        mqttc.connect("192.168.1.203", 1883,60) 
        mqttc.publish("hello/world", "RED off" ,1)
        mqttc.publish("highmount/heating", "R3off" ,1)     # Publish to 199
        mqttc.disconnect()
        
@blynk.VIRTUAL_READ(4)                              # Now get the app to read this..
def v4_read_handler():
    global status
    print('VREAD'+str(status))
    blynk.virtual_write(4, status // 1000)

blynk.run()                                     # Start Blynk (this call should never return)
mqttc.loop_forever()                            # try this to keep it sending messages

So what I am looking for is the syntax to get the VIRTUAL_READ to get read? How do I do that?
I tried adding the “//1000” but I don’t know what that does.
Is there any more detailed documentation beyond the stuff at https://github.com/vshymanskyy/blynk-library-python ?
Thanks
Bruce

Sorry, I don’t think my code formatted as intended. I saw the instruction “Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators” and did try 3 single quotes, but is doesn’t seem to have worked.
How do I fix it?

You need backticks: ```

OK Sorry, but I will learn!
Let me try again, here is the code …

import BlynkLib, time
from gpiozero import PWMLED
import paho.mqtt.client as mqtt

red = PWMLED(21)                                      # set up LEDs to pulse using PWM
yellow = PWMLED(20)                                   # for 201 r=3  y=4  g=2
green = PWMLED(16)                                    # for Pi3 r=21 y=20 g=16

BLYNK_AUTH = '61cc1d16f6e0490db628c25423adba54'       # Blynk code
blynk = BlynkLib.Blynk(BLYNK_AUTH)                    #Initialise Blynk
status = 0                                            # This is for the virtual read for the LED

mqttc=mqtt.Client()
mqttc.connect("192.168.1.203", 1883,60)               #sub Q on 203, port 1883, 60 sec keep alive

@blynk.VIRTUAL_WRITE(1)                               #Register Virtual Pin
def my_write_handler(value):
    global status
    if int(value)==1:
        red.value=1                                   # Switch on LED
        status=255
        print('ON  RED'+str(status))                  # Print update
        mqttc.connect("192.168.1.203", 1883,60)       # connect
        mqttc.publish("hello/world", "RED on" ,1)     # Publish to MQ try QOS 1
        mqttc.publish("highmount/heating", "R3on" ,1) # Publish to 199
        mqttc.disconnect()                            # disconnect
    else:
        red.value=0
        status=0
        print('OFF RED'+str(status))
        mqttc.connect("192.168.1.203", 1883,60) 
        mqttc.publish("hello/world", "RED off" ,1)
        mqttc.publish("highmount/heating", "R3off" ,1)     # Publish to 199
        mqttc.disconnect()
        
@blynk.VIRTUAL_READ(4)                              # Now get the app to read this..
def v4_read_handler():
    global status
    print('VREAD'+str(status))
    blynk.virtual_write(4, status // 1000)

blynk.run()                                     # Start Blynk (this call should never return)
mqttc.loop_forever()                            # try this to keep it sending messages

OK, I think I have found a way to do it.

I was assuming that the …

@blynk.VIRTUAL_READ(4)     # Now get the app to read this..
def v4_read_handler(): 

was going to run when updated - but I was wrong!

So I have put in a section to update the LED using a “set_user_task” like this …

    global status
    print('VREAD'+str(status))
    blynk.virtual_write(4, status)
    
blynk.set_user_task(update_led, 1000)   # run task to update LED 

So I still have a bit of work to do to incorporate this into my main central heating controller, but at least I have something that seems to work.

By the way, I would still like some documentation for the Python library - does it exist?
Bruce

Aside from the various topics here and what is on the GitHub page… nope :wink: But most of it is learning Python more so than Blynk, and for that Google is our friend.

Blast!
I suspected that was the case.
Wonder if we could document the library and contribute it on GitHub? However, I don’t know where to start, but I guess I can learn!

Yes… the developer even asks for contributions…