I am unable to send MQ sensor data to Blynk through Raspberry Pi Pico W. I am using Micropython. Raspberry Pi Pico W is connected to Blynk as it shows my device as online. But the sensor values are not displayed. The blynk.virtual_write is may be incorrect. Please help me to solve this issue.
Below is my code:
import time
import network
from machine import Pin, ADC
import BlynkLib
MQ_2_PIN = Pin(26, Pin.IN) # Example pin for MQ-2 sensor
MQ_135_PIN = Pin(27, Pin.IN) # Example pin for MQ-3 sensor
MQ_3_PIN = Pin(28, Pin.IN) # Example pin for MQ-135 sensor
mq2 = ADC(MQ_2_PIN)
mq3 = ADC(MQ_3_PIN)
mq135 = ADC(MQ_135_PIN)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("JioFi_2DF4F1D", "rgdr5nqtmp")
BLYNK_AUTH = 'JpHXM6_yUbG7JOQcIbFPwP_4LSJEtRzy'
blynk = BlynkLib.Blynk(BLYNK_AUTH)
wait = 10
while wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
wait -= 1
print('waiting for connection...')
time.sleep(1)
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
ip = wlan.ifconfig()[0]
print('IP:', IP)
blynk = BlynkLib.Blynk(BLYNK_AUTH)
@blynk.on('V8')
def read_virtual_pin8_handler():
print("8 handler")
mq2_value = mq2.read_u16()
blynk.virtual_write(8, mq2_value) # Virtual pin 8 for MQ-2 sensor
@blynk.on('V9')
def read_virtual_pin9_handler():
print("9 handler")
mq3_value = mq3.read_u16()
blynk.virtual_write(9, mq3_value)
@blynk.on('V10')
def read_virtual_pin10_handler():
print("10 handler")
mq135_value = mq135.read_u16()
blynk.virtual_write(10, mq135_value)
while True:
blynk.run() # Run Blynk
read_virtual_pin8_handler()
read_virtual_pin9_handler()
read_virtual_pin10_handler()
time.sleep(1) # Delay for stability