Okay, so I was able to implement a first test case for a class like you mentioned, Peter.
This works
Code: Select all
from machine import Pin, Timer, I2C
from rotary_irq_esp import RotaryIRQ
import ssd1306
import dht
import network
import time
import uasyncio
sensor = dht.DHT22(Pin(14))
led = Pin(2, Pin.OUT)
class temp_reader():
sleep = 1 #static instance variable for testing
def __init__(self, led):
self.led = led
async def blink(self):
while True:
self.led.on()
await uasyncio.sleep(temp_reader.sleep)
self.led.off()
await uasyncio.sleep(temp_reader.sleep)
async def cancel(self):
await uasyncio.sleep(10) #stops the loop after 10 seconds
def main():
test = temp_reader(led) #instantiates instance of class
loop = uasyncio.get_event_loop()
loop.create_task(test.blink())
loop.run_until_complete(test.cancel()) #makes the whole thing run until cancel function kicks in
main()
The weird thing is that I sometimes have to re-boot the ESP32 and on top of that I sometimes get this error and it won't reboot until I take it out of the bread-board off all connections. This is just one example of similar messages in an endless loop.
Code: Select all
rst:0x10 (RTCWDT_RTC_RESET),boot:0x33 (SPI_FAST_FLASH_BOOT)
flash read err, 1000
ets_main.c 371
ets Jun 8 2016 00:22:57
Now when I try to add the temperature sensor to the mix, the behavior is a little weirder:
Code: Select all
from machine import Pin, Timer, I2C
from rotary_irq_esp import RotaryIRQ
import ssd1306
import dht
import network
import time
import uasyncio
sensor = dht.DHT22(Pin(14))
led = Pin(2, Pin.OUT)
class temp_reader():
sleep = 1 #static instance variable for testing
def __init__(self, led, sensor):
self.led = led
self.sensor = sensor
async def blink(self):
while True:
self.led.on()
await uasyncio.sleep(temp_reader.sleep)
self.led.off()
await uasyncio.sleep(temp_reader.sleep)
async def temp(self):
while True:
self.sensor.measure()
temp = self.sensor.temperature()
print('Temperature: %3.1f C' %temp)
await uasyncio.sleep(5)
async def cancel(self):
await uasyncio.sleep(10) #stops the loop after 10 seconds
def main():
test = temp_reader(led, sensor)
loop = uasyncio.get_event_loop()
loop.create_task(test.blink())
loop.create_task(test.temp())
loop.run_until_complete(test.cancel()) #makes the whole thing run until cancel function kicks in
main()
Sometimes it works...
Ready to download this file,please wait!
.........
download ok
exec(open('./main.py').read(),globals())
Temperature: 21.6 C
Temperature: 21.6 C
More often than not, it gives me either this error:
Ready to download this file,please wait!
.........
download ok
exec(open('./main.py').read(),globals())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 48, in <module>
File "<string>", line 46, in main
File "/lib/uasyncio/core.py", line 180, in run_until_complete
File "/lib/uasyncio/core.py", line 109, in run_forever
File "<string>", line 31, in temp
File "dht.py", line 16, in measure
OSError: [Errno 110] ETIMEDOUT
Or it says reflush-tree false and I disconnect and connect and if the ESP32 is in the bread-board, it wants me to re-burn micropython until I take it out of the bread-board. Any idea why I'm seeing this erratic behavior?
I think once this runs reliably, it should be on to the encoder co-routine...
As always, any feedback is more than welcome!