Using machine.deepsleep() with uasyncio

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
technodict
Posts: 5
Joined: Wed Nov 30, 2016 8:19 am

Using machine.deepsleep() with uasyncio

Post by technodict » Wed Oct 14, 2020 9:58 am

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())


User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Using machine.deepsleep() with uasyncio

Post by pythoncoder » Thu Oct 15, 2020 9:42 am

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.
Peter Hinch
Index to my micropython libraries.

technodict
Posts: 5
Joined: Wed Nov 30, 2016 8:19 am

Re: Using machine.deepsleep() with uasyncio

Post by technodict » Thu Oct 15, 2020 10:21 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Using machine.deepsleep() with uasyncio

Post by pythoncoder » Thu Oct 15, 2020 12:35 pm

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.
Peter Hinch
Index to my micropython libraries.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Using machine.deepsleep() with uasyncio

Post by kevinkk525 » Thu Oct 15, 2020 6:29 pm

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.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

technodict
Posts: 5
Joined: Wed Nov 30, 2016 8:19 am

Re: Using machine.deepsleep() with uasyncio

Post by technodict » Thu Oct 15, 2020 6:33 pm

Could you post an example code snippet? i am expecially interested how to handle wakeup from deepsleep

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Using machine.deepsleep() with uasyncio

Post by pythoncoder » Fri Oct 16, 2020 8:04 am

There is information on Pyboards and deepsleep here.
Peter Hinch
Index to my micropython libraries.

Post Reply