uasyncio and deepsleep after inactivity

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
yneptik
Posts: 2
Joined: Thu Jun 24, 2021 12:44 pm

uasyncio and deepsleep after inactivity

Post by yneptik » Thu Jun 24, 2021 2:59 pm

Good afternoon

I'am quite new to micropython and working/playing with a esp32, trying to make an automatic door opener for a chicken coop.
I plan to use an oled screen and a rotary encoder to choose the time of opening, set the date, ...

I would like to put the esp32 in deepsleep mode after a certain time of inactivity, for example 1minute.

Here is my code dealing with the motor and the switches using uasyncio :

Code: Select all

import machine
import time
import esp32
import uasyncio as asyncio
from primitives.switch import Switch

motPWM = machine.PWM(machine.Pin(33), freq=50)
motPWM.freq(1000)
motDir = machine.Pin(32, machine.Pin.OUT)
dutyCycle = 512 #doit etre entre 0 and 1023 
openButton = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP)
doorHigh = machine.Pin(18, machine.Pin.IN, machine.Pin.PULL_UP)


async def startMot () :
    if not doorHigh.value() :
        motDir.value(0) #deroulement garcette
        motPWM.duty(dutyCycle)
    await asyncio.sleep(0)

async def stopMot () :
    motPWM.duty(0)
    await asyncio.sleep(0)

async def dummy_app() :
    await asyncio.sleep(0)
    
swOpen = Switch(openButton)
swHigh = Switch(doorHigh)
    
swOpen.close_func(startMot)
swHigh.close_func(stopMot)

while True :
    asyncio.run(dummy_app())
How can I have a routine? function? running in back with a timer, that timer is reset to 0 every time the open button is used (and also when the rotary encoder and its switch are used). When the timer reach 60 seconds the esp32 is put in deepsleep mode.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: uasyncio and deepsleep after inactivity

Post by davef » Thu Jun 24, 2021 8:39 pm

Just a "heads-up". I was looking at using deepsleep on the ESP32 until I ran across a note in the datasheet errata saying that coming out of deepsleep could cause random "crashes".

I would check which revision(s) it applies to and confirm the revision of your chip.

yneptik
Posts: 2
Joined: Thu Jun 24, 2021 12:44 pm

Re: uasyncio and deepsleep after inactivity

Post by yneptik » Mon Jun 28, 2021 5:29 am

Thank you for answer, I think about it and decided not to go in deep sleep, my system will be powered most of the time from AC network.

However there will be a backup battery in case of power outage, for that reason I'm still looking for how to have a way (using coro?) to count 1 minutes after the last human action (rotary encoder / switch action) after that minute it will put at 0 a GPIO pin that drives a transistor powering the screen.

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

Re: uasyncio and deepsleep after inactivity

Post by pythoncoder » Mon Jun 28, 2021 9:03 am

That is easily done. See my delay_ms class. This implements a software timer that can be retriggered (like a watchdog). So, implement one of those with a 1 minute period and a callback which turns off the transistor. Each human interaction turns the transistor on and retriggers the timer.
Peter Hinch
Index to my micropython libraries.

Post Reply