Page 1 of 1
Using machine.deepsleep() with uasyncio
Posted: Wed Oct 14, 2020 9:58 am
by technodict
I an scheduling two coroutines. One which reads sensor data and the other uploads this sensor data via MQTT. I am using uasyncio and sched libraries as mentioned here
https://github.com/peterhinch/micropyth ... onous-code
I read the data from the sensor measure coroutine every 20 mins and the upload data corotine runs once every 4 hours. I need to use machine.deepsleep() in between these long running schedules. Can anyone help me with a small snippet how can i make this work ?
i am positng a small code example to what i have written
Code: Select all
import *
async def measure():
temperature = round(si.temperature(),1)
storeData()
async def upload():
uploadMqtt()
async def main():
asyncio.create_task(schedule(measure, hrs=None, mins=range(0,60,20)))
asyncio.create_task(schedule(upload, hrs=(0.60.4), mins=None))
await asyncio.sleep(100000000) #some infinitey large number
asyncio.run(main())
Re: Using machine.deepsleep() with uasyncio
Posted: Thu Oct 15, 2020 9:42 am
by pythoncoder
The behaviour of machine.deepsleep is highly platform dependent. On Pyboards a wake from deepsleep is similar to a reboot: no program state is retained (there are ways to retain some data).
This means that uasyncio would be started from scratch each time it woke.
In practice uasyncio and low power modes don't work well together. When uasyncio V2 was current I experimented with combining uasyncio with lightsleep with mixed results. It was a kludge which I haven't carried over to V3. Damien is working on a radical change to the mechanism behind select.poll: hopefully this will have some lightsleep support.
But I can't envisage any way to combine uasyncio and deepsleep because uasyncio relies on a task queue which (on a Pyboard) would have to be re-created on wakeup.
Re: Using machine.deepsleep() with uasyncio
Posted: Thu Oct 15, 2020 10:21 am
by technodict
Thank you for your reply.
What would be then a better way to achieve this behavior ?
I wish to for Eg: Read sensor data every 1 hour and store it to a file and every 10 hours upload that data via MQTT to some server. In between the device needs to be in deepsleep.. I am using a Pycom Gpy as the development board in this case.
Re: Using machine.deepsleep() with uasyncio
Posted: Thu Oct 15, 2020 12:35 pm
by pythoncoder
I have no experience of Pycom products. You may be better off asking in
their forum.
On the assumption that their hardware is similar to a Pyboard I would only use
uasyncio if there is a need for it in those periods when the board is awake, in which case you will be starting it from scratch each time. I think what you want to do should be straightforward, but the mechanism for waking from deepsleep may be specific to Pycom boards.
Re: Using machine.deepsleep() with uasyncio
Posted: Thu Oct 15, 2020 6:29 pm
by kevinkk525
I'd stay with uasyncio but program it like it reboots every time. So connect, sync the time, check the schedule (e.g. wakup between minutes 0-15 -> publish to broker, else save to file) and sleep again. If you are familiar with uasyncio and its modules, it might be more difficult to write code in sync just for one board that uses deepsleep. I wouldn't want to introduce other sources of error by using a sync mqtt client when I'm using the async all the time.
To make sure a publish goes through correctly, you could subscribe to that topic and go to deepsleep when you receive a message on that topic. Might want to build some failsafes too though to prevent errors from keeping your device active.
Re: Using machine.deepsleep() with uasyncio
Posted: Thu Oct 15, 2020 6:33 pm
by technodict
Could you post an example code snippet? i am expecially interested how to handle wakeup from deepsleep
Re: Using machine.deepsleep() with uasyncio
Posted: Fri Oct 16, 2020 8:04 am
by pythoncoder
There is information on Pyboards and deepsleep
here.