Hello,
at the risk of posting this to the wrong topic, I'm looking for some
help if possible - and I'm not able to create my own topic for some reason.
I have started working with micropython fairly recently, so please forgive my ignorance. I'm trying to implement something similar to matty trentini's uasyncio encoder example (
https://gist.github.com/mattytrentini/1 ... b0a4284de9).
More precisely, I'd like to do the following:
1) Use an encoder to change and set (with button push) a specific temperature (let's call it target temperature)
2) Use a DHT22 (and later potentially a thermocouple) to monitor the current temp
3) Alert (either with led, buzzer, etc to the fact that the target temp was reached)
4) Display information on an OLED
I understand that the encoder function and the temperature measurements have to run simultaneously for this to work - this is where
comes in.
Here is what I have so far
Code: Select all
import uasyncio
from machine import Pin, I2C
import machine
import time
from rotary_irq_esp import RotaryIRQ
import dht
sensor = dht.DHT22(Pin(14))
r = RotaryIRQ(pin_num_clk=12,
pin_num_dt=13,
min_val=0,
max_val=60,
reverse=True,
range_mode=RotaryIRQ.RANGE_WRAP)
led = machine.Pin(2, Pin.OUT)
async def blink(led):
while True:
led.on()
await uasyncio.sleep(1)
led.off()
await uasyncio.sleep(1)
async def encoder(r):
val_old = r.value()*0.5
while True:
val_new = r.value()*(0.5)
if val_old != val_new:
val_old = val_new
print('result =', val_new)
await uasyncio.sleep_ms(50)
async def sensor_measure(sensor):
while True:
sensor.measure()
temp = sensor.temperature()
print('Temperature: %3.1f C' %temp)
await uasyncio.sleep(5)
loop = uasyncio.get_event_loop()
loop.create_task(blink(led))
loop.create_task(encoder(r))
loop.create_task(sensor_measure(sensor))
# this is now all running at the same time!!! progress!!
loop.run_forever()
Display is not implemented yet. It seems to work BUT everything is running on an endless loop and the editor is showing me an infinite loop of constant temp measurements, which can be interrupted by turning the encoder. What I would like to happen is for the temperature measurements to happen and then when I turn the encoder the value shows up. Do I have to save the temp measurements in a list or variable to be able to compare to the target temperature?
Any help would be greatly appreciated here!