天天看点

ESP32定时器睡眠模式

定时器唤醒

ESP32 可以进入深度睡眠模式,然后在预定义的时间段内唤醒。如果您正在运行需要时间戳或日常任务的项目,同时保持低功耗,则此功能特别有用。

ESP32定时器睡眠模式

ESP32 RTC 控制器具有一个内置计时器,可用于在预定义的时间后唤醒 ESP32。

启用定时器唤醒

让 ESP32 在预定义的时间后唤醒非常简单。在 Arduino IDE 中,您只需在以下函数中以微秒为单位指定休眠时间:

法典

让我们使用库中的示例来看看它是如何工作的。打开 Arduino IDE,转到 ESP32 >深度睡眠>文件>示例,然后打开 TimerWakeUp 草图。

/*
Simple Deep Sleep with Timer Wake Up
=====================================
ESP32 offers a deep sleep mode for effective power
saving as power is an important factor for IoT
applications. In this mode CPUs, most of the RAM,
and all the digital peripherals which are clocked
from APB_CLK are powered off. The only parts of
the chip which can still be powered on are:
RTC controller, RTC peripherals ,and RTC memories

This code displays the most basic deep sleep with
a timer to wake it up and how to store data in
RTC memory to use it over reboots

This code is under Public Domain License.

Author:
Pranav Cherukupalli <[email protected]>
*/

#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  5        /* Time ESP32 will go to sleep (in seconds) */

RTC_DATA_ATTR int bootCount = 0;

/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason(){
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch(wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
  }
}

void setup(){
  Serial.begin(115200);
  delay(1000); //Take some time to open up the Serial Monitor

  //Increment boot number and print it every reboot
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  //Print the wakeup reason for ESP32
  print_wakeup_reason();

  /*
  First we configure the wake up source
  We set our ESP32 to wake up every 5 seconds
  */
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
  " Seconds");

  /*
  Next we decide what all peripherals to shut down/keep on
  By default, ESP32 will automatically power down the peripherals
  not needed by the wakeup source, but if you want to be a poweruser
  this is for you. Read in detail at the API docs
  http://esp-idf.readthedocs.io/en/latest/api-reference/system/deep_sleep.html
  Left the line commented as an example of how to configure peripherals.
  The line below turns off all RTC peripherals in deep sleep.
  */
  //esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
  //Serial.println("Configured all RTC Peripherals to be powered down in sleep");

  /*
  Now that we have setup a wake cause and if needed setup the
  peripherals state in deep sleep, we can now start going to
  deep sleep.
  In the case that no wake up sources were provided but deep
  sleep was started, it will sleep forever unless hardware
  reset occurs.
  */
  Serial.println("Going to sleep now");
  delay(1000);
  Serial.flush(); 
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}

void loop(){
  //This is not going to be called
}
           

查看原始代码

让我们看一下此代码。第一条评论描述了在深度睡眠和定时器唤醒期间关闭电源的内容。

In this mode CPUs, most of the RAM,
and all the digital peripherals which are clocked
from APB_CLK are powered off. The only parts of
the chip which can still be powered on are:
RTC controller, RTC peripherals ,and RTC memories
           

使用定时器唤醒时,将通电的部件是RTC控制器、RTC外设和RTC存储器。

定义睡眠时间

前两行代码定义了 ESP32 将休眠的时间段。

#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ 
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */
           

此示例使用从微秒到秒的转换因子,以便您可以在TIME_TO_SLEEP在几秒钟内变量。在这种情况下,该示例会将 ESP32 置于深度睡眠模式 5 秒钟。

将数据保存在 RTC 存储器上

借助 ESP32,您可以将数据保存在 RTC 存储器上。ESP32 在 RTC 部分具有 8kB SRAM,称为 RTC 快速存储器。保存在此处的数据在深度睡眠期间不会被删除。但是,当您按下重置按钮(ESP32 开发板上标有 EN 的按钮)时,它将被擦除。

要将数据保存在RTC存储器上,您只需添加RTC_DATA_ATTR在变量定义之前。该示例保存启动计数变量。此变量将计算 ESP32 从深度睡眠中醒来的次数。

唤醒原因

然后,代码定义print_wakeup_reason()函数,打印 ESP32 从睡眠状态唤醒的原因。

void print_wakeup_reason(){
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch(wakeup_reason){
    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
  }
}
           

设置()

在setup()是您应该放置代码的位置。在深度睡眠中,素描永远不会到达loop()陈述。所以,你需要把所有的草图都写在setup().

此示例首先以波特率为 115200 初始化串行通信。

然后,启动计数变量在每次重新启动时都会增加一个,并且该数字将打印在串行监视器中。

++bootCount;
Serial.println("Boot number: " + String(bootCount));
           

然后,代码调用print_wakeup_reason()函数,但您可以调用任何要执行所需任务的函数。例如,您可能希望每天唤醒一次 ESP32,以便从传感器读取值。

接下来,代码使用以下函数定义唤醒源:

此函数接受以微秒为单位的睡眠时间作为参数,如前所述。

在我们的例子中,我们有以下内容:

然后,在执行所有任务后,esp 通过调用以下函数进入睡眠状态:

循环()

这loop()部分为空,因为 ESP32 将在到达代码的这一部分之前进入休眠状态。因此,您需要将所有草图写在setup().

将示例草图上传到 ESP32。确保选择了正确的主板和 COM 端口。

测试计时器唤醒

以波特率为115200打开串行显示器。

ESP32定时器睡眠模式

每隔 5 秒,ESP 唤醒,在串行监视器上打印一条消息,然后再次进入深度睡眠状态。

每次 ESP 唤醒启动计数变量增加。它还会打印唤醒原因,如下图所示。

但是,请注意,如果按下 ESP32 开发板上的 EN 按钮,它会再次将引导计数重置为 1。

您可以修改提供的示例,而不是打印消息,您可以让 ESP 执行任何其他任务。计时器唤醒功能可用于使用 ESP32 执行定期任务,例如日常任务,而不会消耗太多电量。