How to use deep sleep in a loop



  • Hi all,

    I have a LoPy4 with a pytrack.
    I have a loop that essentially looks like this:

    init_timer = time.time()
    while True:
        final_timer = time.time()
        diff = final_timer - init_timer
            if diff > 300:
                do some stuff here
                init_timer = time.time()
    

    I'd like to incorporate this:
    machine.deepsleep(300*1000)

    into the code to make it deep sleep while it's waiting for those 5 minutes but it doesn't seem to be working.

    Can anyone help with this?

    Thanks





  • @devinv See the docs of deepsleep for PyTrack.



  • @jcaron actually machine.deepsleep does work.
    it put my sipy to sleep and restart the program
    But the pytrack is not sleeping and is consuming a lot.
    what should I use to put both of them to deepsleep?



  • @devinv Are you sure it doesn't wake up, or do you just not see it? If you have very little code with no visible effect before you go back to sleep, you may not even notice it wakes up.

    So for instance the following code:

    machine.deepsleep(10 * 1000)
    

    is a perfectly valid deep sleep loop, but you will have a hard time noticing the SiPy waking up!

    Also, are you using machine.deepsleep or Pytrack-assisted deep sleep? The latter will shut down USB, and it'll take a few seconds for USB to come back up and to be able to connect again, so there again, if you don't keep the device awake for several seconds you won't notice it waking up.

    The best option to actually confirm it's working is probably to turn on the LED on boot and wait for at least a few hundred milliseconds before going to deep sleep. Alternately, monitoring current consumption should also show a spike on wake up.

    Share your code (along with the version of the SiPy firmware and Pytrack firmware, and the Pytrack libraries if you use them) if you want feedback.



  • my sipy (on a pytrack) never wakes up when sent to DeepSleep
    Do you have a ultra simple code which send it to Deep Sleep for a few secondes which works?
    Thanks



  • @james-matthews Depending on your needs, you could use pycom.nvs_set or one or several files stored on flash.

    Note that there are caveats with the latter option (number of writes to flash, not crashing the filesystem...).

    You could also use an external battery-powered NVRAM/RTC.



  • Thanks for the help everyone, getting rid of the loop and using machine.deepsleep(300*1000) at the end of the code worked perfectly. The next problem I have, how do I save the state & reload a state?

    Thanks



  • @james-matthews just to clarify, deep sleep achieves very low power consumption by powering down nearly everything on the chip, including the cores and all associated state (registers) and RAM.

    So once it exists deep sleep, it doesn't remember a thing, and it has to start from scratch again, so you need to maintain any state you need across sleep in long-term storage (if you need any state at all).



  • @james-matthews when the device wakes up from machine.deepsleep(), runs through the reset stages. So you cannot simply put deepsleep() into a loop like you would do with sleep(). Before deepsleep() you have to save your execution stage, after reset check the reset cause, and then resume the state you code had before deepsleep().



  • Hello @james-matthews , I don't know exactly what you want to do, but deep sleep is always used as the last function of the program, that is, you do what you want and as last instruction you put machine.deepsleep(300000). Your code will run and then the module will sleep for 5 minutes, when that time ends will be restarted and run again from the beginning, ie reset.

    Therefore, I don't think you need while nor time in your code.

    Greetings,


 

Pycom on Twitter