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. 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.
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?
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
Aside from the various topics here and what is on the GitHub page… nope 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!