Search code examples
toit

Deep sleep for a specific time


I need to activate a peripheral for a specific time, then sleep for some time, and then deactivate the peripheral again.

I could do this with a simple sleep but that would keep my ESP32 awake and burn battery. Is there a way to go into deep sleep for the required time and then wake up again?

Ideally I would simple schedule to run a deactivate program after a certain duration.

What's the best way to do this?


Solution

  • Toit has the following example for putting an ESP32 into a deep sleep for 10 seconds.

    // Copyright (C) 2021 Toitware ApS.
    // Use of this source code is governed by a Zero-Clause BSD license that can
    // be found in the examples/LICENSE file.
    
    import esp32
    
    main:
      run_time ::= Duration --us=esp32.total_run_time
      sleep_time ::= Duration --us=esp32.total_deep_sleep_time
      print "Awake for $(run_time - sleep_time) so far"
      print "Slept for $sleep_time so far"
      esp32.deep_sleep (Duration --s=10)