Quick example I wrote, logs a DHT22 every 30 seconds AND blinks an LED every 500ms, in parallel without blocking:
Code: Select all
import uasyncio as asyncio
from machine import Pin
import dht
led = Pin(25, Pin.OUT) # onboard LED on Pico
sensor = dht.DHT22(Pin(15))
async def blink():
while True:
led.toggle()
await asyncio.sleep_ms(500)
async def log_sensor():
while True:
sensor.measure()
print(f't={sensor.temperature()}C h={sensor.humidity()}%')
await asyncio.sleep(30)
async def main():
asyncio.create_task(blink())
asyncio.create_task(log_sensor())
while True:
await asyncio.sleep(60) # keep main loop alive
asyncio.run(main())Code: Select all
await asyncio.sleep()May nakapag-port na ba ng more complex stuff (web server, MQTT) sa MicroPython using asyncio? Curious how it scales.