I'm working on a project on an ESP32 with MicroPython 1.14, in which I would like to combine blynk and other asynchronous tasks. I'm using blynklib_mp.mpy to reduce the RAM usage. My problem arises when I try to combine blynk and uasyncio.
The 'pure' blynk code (based on their examples - https://github.com/blynkkk/lib-python/b ... ual_pin.py) works great and seamlessly with the app via my local blynk server, which is running on a Raspberry Pi 3+.
Code: Select all
import blynklib_mp as blynklib
import network
import time
from machine import Pin
import dht
import gc
sensor = dht.DHT22(Pin(23))
led = Pin(2, Pin.OUT)
WIFI_SSID = 'XXX'
WIFI_PASS = 'XXX'
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASS)
# check if board connected
connect_status = wifi.isconnected()
# initialize Blynk
blynk = blynklib.Blynk(token = "XXX", server = 'X.X.X.X, port = XX)
WRITE_EVENT_PRINT_MSG = "[WRITE_VIRTUAL_PIN_EVENT] Pin: V{} Value: '{}'"
T_COLOR = '#f5b041'
H_COLOR = '#85c1e9'
ERR_COLOR = '#444444'
T_VPIN = 3
H_VPIN = 4
# register handler for virtual pin V4 write event
@blynk.handle_event('read V{}'.format(T_VPIN))
def read_handler(vpin):
temperature = 0
humidity = 0
# read sensor data
try:
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
except OSError as o_err:
print("Unable to get DHT22 sensor data: '{}'".format(o_err))
# change widget values and colors according read results
if temperature > 25 or humidity > 50:
blynk.set_property(T_VPIN, 'color', 'red')
blynk.set_property(H_VPIN, 'color', 'red')
blynk.virtual_write(T_VPIN, temperature)
blynk.virtual_write(H_VPIN, humidity)
else:
# show widgets aka 'disabled' that mean we had errors during read sensor operation
blynk.set_property(T_VPIN, 'color', T_COLOR)
blynk.set_property(H_VPIN, 'color', H_COLOR)
@blynk.handle_event('write V2')
def button_handler(pin, value):
if value == ['1']:
print(WRITE_EVENT_PRINT_MSG.format(pin, value))
print('LED allumee')
led(1)
else:
print(WRITE_EVENT_PRINT_MSG.format(pin, value))
print('LED eteinte')
led(0)
###########################################################
# infinite loop that waits for event
###########################################################
while True:
blynk.run()
Code: Select all
import blynklib_mp as blynklib
import network
import time
from machine import Pin
import dht
import uasyncio
import gc
sensor = dht.DHT22(Pin(23))
led = Pin(2, Pin.OUT)
WIFI_SSID = 'XXX'
WIFI_PASS = 'XXX'
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASS)
# check if board connected
connect_status = wifi.isconnected()
# initialize Blynk
blynk = blynklib.Blynk(token = "XXX", server = 'X.X.X.X, port = XX)
WRITE_EVENT_PRINT_MSG = "[WRITE_VIRTUAL_PIN_EVENT] Pin: V{} Value: '{}'"
T_COLOR = '#f5b041'
H_COLOR = '#85c1e9'
ERR_COLOR = '#444444'
T_VPIN = 3
H_VPIN = 4
# register handler for virtual pin V4 write event
@blynk.handle_event('read V{}'.format(T_VPIN))
def read_handler(vpin):
temperature = 0
humidity = 0
# read sensor data
try:
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
except OSError as o_err:
print("Unable to get DHT22 sensor data: '{}'".format(o_err))
# change widget values and colors according read results
if temperature > 25 or humidity > 50:
blynk.set_property(T_VPIN, 'color', 'red')
blynk.set_property(H_VPIN, 'color', 'red')
blynk.virtual_write(T_VPIN, temperature)
blynk.virtual_write(H_VPIN, humidity)
else:
# show widgets aka 'disabled' that mean we had errors during read sensor operation
blynk.set_property(T_VPIN, 'color', T_COLOR)
blynk.set_property(H_VPIN, 'color', H_COLOR)
@blynk.handle_event('write V2')
def button_handler(pin, value):
if value == ['1']:
print(WRITE_EVENT_PRINT_MSG.format(pin, value))
print('LED allumee')
led(1)
else:
print(WRITE_EVENT_PRINT_MSG.format(pin, value))
print('LED eteinte')
led(0)
async def run_blynk():
while True:
blynk.run()
await uasyncio.sleep_ms(5)
async def read_temp(sensor):
while True:
sensor.measure()
temperature = sensor.temperature()
#humidity = sensor.humidity()
print(temperature)
await uasyncio.sleep_ms(1000)
###########################################################
# infinite loop that waits for event
###########################################################
# Setup async app-run
def set_global_exception():
def handle_exception(loop, context):
import sys
sys.print_exception(context["exception"])
sys.exit()
loop = uasyncio.get_event_loop()
loop.set_exception_handler(handle_exception)
async def main():
set_global_exception()
uasyncio.create_task(read_temp(sensor))
await blynk.run()
try:
asyncio.run(main())
finally:
asyncio.new_event_loop()
gc.collect()
gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
Code: Select all
Traceback (most recent call last):
File "uasyncio/core.py", line 1, in run_until_complete
File "main.py", line 89, in read_temp
File "dht.py", line 17, in measure
OSError: [Errno 110] ETIMEDOUT
MicroPython v1.14 on 2021-02-02; ESP32 module with ESP32
Code: Select all
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5008
ho 0 tail 12 room 4
load:0x40078000,len:10600
ho 0 tail 12 room 4
load:0x40080400,len:5684
entry 0x400806bc
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ for Python v0.2.6
21.2
21.3
21.2
21.3
21.3
21.2
21.3
21.3
21.3
[WRITE_VIRTUAL_PIN_EVENT] Pin: V2 Value: '['1']'
LED allumee
21.3
[WRITE_VIRTUAL_PIN_EVENT] Pin: V2 Value: '['0']'
LED eteinte
21.3
Unable to get DHT22 sensor data: '[Errno 110] ETIMEDOUT'
21.3
Traceback (most recent call last):
File "main.py", line 105, in <module>
File "uasyncio/core.py", line 1, in run
File "uasyncio/core.py", line 1, in run_until_complete
File "uasyncio/core.py", line 1, in run_until_complete
File "main.py", line 102, in main
File "main.py", line 87, in read_temp
File "dht.py", line 17, in measure
OSError: [Errno 110] ETIMEDOUT
MicroPython v1.14 on 2021-02-02; ESP32 module with ESP32
I'm at a loss here of making these two, blynk and async work together and would be grateful for any input or suggestions you have.