Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Proper way to deepsleep....

    Scheduled Pinned Locked Moved LoPy
    deepsleep
    16 Posts 6 Posters 1.4k Views 8 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      Martinnn @jcaron
      last edited by

      @jcaron As you said this is an FAQ, I suggest you make a Tutorial/example code with proper documentation for it.

      1 Reply Last reply Reply Quote 0
      • P Offline
        PeterB
        last edited by

        There is another major problem with py.go_to_sleep() !!!!
        As said it switches off power to the ESP32, but be careful: without a proper shutdown of the ESP32, meaning it looses/corrupts data when using its filesystem before goring to sleep, even if that was more than 5 seconds ago.
        The machine.deepsleep() shuts down the ESP32 gracefully but then the pysense remains powered.

        John BairdJ 1 Reply Last reply Reply Quote 0
        • John BairdJ Offline
          John Baird @PeterB
          last edited by

          @PeterB Quick question - it's painful but, would a machine.deepsleep() of one second followed by py.go_to_sleep() fix the graceful shutdown question? Our periods of sleep are all in the 10-60 minute range so an extra second or so isn't an issue. If I get time I'll try it, but was wondering what you thought?

          T P 2 Replies Last reply Reply Quote 0
          • T Offline
            tuftec @John Baird
            last edited by

            @John-Baird Sounds like a practical work around to me.

            1 Reply Last reply Reply Quote 0
            • P Offline
              PeterB @John Baird
              last edited by PeterB

              @John-Baird said in Proper way to deepsleep....:

              @PeterB Quick question - it's painful but, would a machine.deepsleep() of one second followed by py.go_to_sleep() fix the graceful shutdown question? Our periods of sleep are all in the 10-60 minute range so an extra second or so isn't an issue. If I get time I'll try it, but was wondering what you thought?

              Nice idea.
              The issue then will be that the "py.go_to_sleep()" can not be put after the "machine.deepsleep()" directly because a wake from "machine.deepsleep()" will restart at boot.py.
              Then I should be able to recognise a wake from machine.deepsleep() from any of the wakes from "py.go_to_sleep()" , i.e. power up, accelerometer, time and external button press.
              Perhaps via "machine.reset_cause()" if that works in Pycom as it can distinguish between a power wake of the ESP32, which is expected to happen after any of the wakereasons by "py.go_to_sleep()", and a timed wakeup of the ESP32. In that case a power wake from pysense means a power wake of both and shlould not be followed by a "py.go_to_sleep()" but by the rest of the program.

              Badly documented by Pycom (again):
              According https://docs.micropython.org/en/latest/library/machine.html#machine-constants
              and https://docs.micropython.org/en/latest/library/machine.html
              the "machine.wake_reason()" should not be used for this, but the "machine.reset_cause()" as it leads to:
              machine.PWRON_RESET
              machine.HARD_RESET
              machine.WDT_RESET
              machine.DEEPSLEEP_RESET
              machine.SOFT_RESET
              and as "machine.wake_reason()" would most likely only lead to:
              machine.WLAN_WAKE
              machine.PIN_WAKE
              machine.RTC_WAKE

              1 Reply Last reply Reply Quote 0
              • M Offline
                misterlisty
                last edited by

                Not sure if the question is answered...if i use the pysense deepsleep, how do i get the esp32 to shutdown gracefully?

                P 1 Reply Last reply Reply Quote 0
                • P Offline
                  PeterB @misterlisty
                  last edited by

                  @misterlisty said in Proper way to deepsleep....:

                  Not sure if the question is answered...if i use the pysense deepsleep, how do i get the esp32 to shutdown gracefully?

                  As indicated below, first deepsleep the ESP32 for a very short while, shutting it down gracefully, and when that wakes go_to_sleep on pysense, which will not gracefully shutdown the ESP32, but nothing essential should have been done since te wakeup.

                  I am working on it now.

                  M 1 Reply Last reply Reply Quote 0
                  • M Offline
                    misterlisty @PeterB
                    last edited by

                    @PeterB Be interested in seeing your solution if you share..if i deepsleep the esp32, then the code after this command will no longer execute..when the esp32 wakes doesn't the boot.py start again?

                    P 1 Reply Last reply Reply Quote 0
                    • P Offline
                      PeterB @misterlisty
                      last edited by PeterB

                      @misterlisty said in Proper way to deepsleep....:

                      @PeterB Be interested in seeing your solution if you share..if i deepsleep the esp32, then the code after this command will no longer execute..when the esp32 wakes doesn't the boot.py start again?

                      Exactly. So what I do now at the start of main.py (so I do not use the "machine.wake_reason()" as thought earlier, but jist a special flag in flash):

                      INT_PIN = Pin("P9", Pin.IN, Pin.PULL_DOWN)
                      py = Pysense()
                      acc = LIS2HH12()
                      
                      ESP32sleep = pycom.nvs_get("ESP32sleep")
                      if ESP32sleep is None:
                          ESP32sleep = 0
                      pycom.nvs_set("ESP32sleep", 0)
                      if ESP32sleep > 0:
                          blink("red", 5, speed=0.1)
                          state = pycom.nvs_get("state")
                          if state is None:
                              state = STATE_NOT_SET
                          # enable wakeup source from INT pin (PIC_RC1 - pin#6 on External IO Header)
                          py.setup_int_pin_wake_up(True)
                          if state == STATE_WAIT_FOR_ACC:
                              py.setup_int_wake_up(True, False)
                              acc.enable_activity_interrupt(ACC_THRESHOLD, ACC_DURATION)
                      
                          py.setup_sleep(LONGSLEEP)
                          py.go_to_sleep()
                      
                      # REST OF NORMAL CODE FOLLOWS which is executed when it restarts
                      # without the instruction as indicated by "ESP32sleep" to go to sleep immediately via py.go_to_sleep()
                      # ...
                      

                      And for going to sleep I call the following function:

                      def gotosleep():
                          pycom.nvs_set("ESP32sleep", 1)
                          machine.deepsleep(1)
                      

                      It works partly as expected. The closure of the ESP32 is good in this way as I can see it does not make garbage of any files.
                      It does the wake up on a time-wake-up of pysense. I have problems with the external button and aacelerometer still.
                      Need to investigate that furrther.

                      M P 2 Replies Last reply Reply Quote 1
                      • M Offline
                        misterlisty @PeterB
                        last edited by

                        @PeterB Tell me if i'm wrong but i do not see how this resolves the issue. When you put the pycom to deepsleep, it shuts down the os, but on restart is still needs to load the OS to execute your code so how does this resolve the issue?

                        P 1 Reply Last reply Reply Quote 0
                        • P Offline
                          PeterB @misterlisty
                          last edited by

                          @misterlisty said in Proper way to deepsleep....:

                          @PeterB Tell me if i'm wrong but i do not see how this resolves the issue. When you put the pycom to deepsleep, it shuts down the os, but on restart is still needs to load the OS to execute your code so how does this resolve the issue?

                          Because it closes the OS gracefully with the machine.deepsleep(1), including file handling etc etc. I did before calling gotosleep().
                          Therafter it restarts, but as I do not do anything else than shown in my solution in the code I have given, before putting it to sleep via py.go_to_sleep(); It does not matter that py.go_to_sleep() then just kills the OS. I need py.go_to_sleep() as I need the sleepfunctions of the pysense board as well.

                          John BairdJ 1 Reply Last reply Reply Quote 1
                          • John BairdJ Offline
                            John Baird @PeterB
                            last edited by

                            Wow! Thanks for the followup on this @PeterB, that looks great. I'm glad the idea works, though concerned by the issues you are still having. I appreciate the code sample and will be integrating it soon. If I learn anything more, I'll pass it along.

                            1 Reply Last reply Reply Quote 0
                            • P Offline
                              PeterB @PeterB
                              last edited by PeterB

                              This post is deleted!
                              1 Reply Last reply Reply Quote 0

                              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                              With your input, this post could be even better 💗

                              Register Login
                              • 1 / 1
                              • First post
                                Last post

                              Pycom on Twitter

                              Pycom