uasyncio + Switch class + Timers = Big mess #12598
Replies: 4 comments 4 replies
-
What is goal of Your code ? You defined async method but there is no ''' await''' call. edit: read about Writing interrupt handlers -> also what time accuracy with timer do You want achieve? in ESP32 with micropython timer's ISR is a software (not hardware) so maybe simpler is use just asyncio ( I uses interchangeably 'events' controlled just by timers/asnyncio combined or just by pure async and there was no difference in functionality. |
Beta Was this translation helpful? Give feedback.
-
Hello, in the OP it seems you need a delay of exactly 5000 milliseconds. The code you posted blocks asyncio during its execution (no await). Also, relay.value(1) could be done once before the loop. It seems that code should be equivalent to:
However, if other tasks are executing, asyncio.sleep_ms may take longer than 5000 ms if some other asyncio task starts just before the 5000 seconds elapse.
(edit) The 200 ms is a figure that must be larger than the longest block of an asyncio task. The time.sleep_ms blocks, but asyncio is free to do its scheduling during most of the time. With time.sleep_us() precision could be even better. |
Beta Was this translation helpful? Give feedback.
-
It rarely makes sense to use timers with from machine import Pin
import time
from primitives import Switch # class is stored in primitives folder on esp32
import uasyncio as asyncio
Relay_value = 0
off_task = None
async def def timed_off():
global Relay_value
print('turn-off relay by timer')
await asyncio.sleep(5)
Relay_value = 0
print('timer callback one shot after 5 sec')
def closed_switch():
global off_task
print('turn on relay')
Relay_value = 1
off_task = asyncio.create_task(timed_off()) # Turn relay off after 5s
print('switch closed',stop_flag )
def opened_switch(): # No need for async def here
global Relay_value, off_task
print('turn-off relay by external interrupt')
Relay_value = 0
if off_task is not None:
off_task.cancel()
off_task = None
print('switch opened',stop_flag)
async def my_app():
pin = Pin(4, Pin.IN, Pin.PULL_UP)
print(pin.value())
sw = Switch(pin)
sw.close_func(closed_switch)
sw.open_func(opened_switch)
print(sw.__call__()) # of pin.value() # print sw value( state)
await asyncio.sleep(100) # Dummy application code
print('run')
asyncio.run(my_app()) # Run main application code |
Beta Was this translation helpful? Give feedback.
-
Hello everyone! Sorry for not to respond for so long, very busy and holidays :) Thank you very much to all people, finally I've followed the uasyncio timer tips of @peterhinch and all works great. Thank you again. Greetings! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everybody.
I have this code:
The problem is when I want to use timers. With a simple "while" using switch class "interrupts" and defining a countdown inside the "while! this code is working fine, but when I define a timer to control the time accuracy the switch class "interrupts" doesn't respond any more.
Could you help me please?
Thank you very much.
PD: My boad is a ESP32 Devkit.1
Beta Was this translation helpful? Give feedback.
All reactions