外部唤醒 – 多个 GPIO

现在,您应该能够使用不同的按钮唤醒 ESP32,并确定哪个按钮导致唤醒。在此示例中,我们将使用GPIO 2和GPIO 15作为唤醒源。

图解的

将两个按钮连接到 ESP32。在这个例子中,我们使用的是GPIO 2和GPIO 15,但您可以将按钮连接到任何 RTC GPIO。

esp32如何测试voip esp32gpio_esp32如何测试voip

法典

您需要对我们之前使用的示例代码进行一些修改:

  • 创建要使用的位掩码GPIO 15和GPIO 2.我们之前已经向您展示了如何执行此操作;
  • 启用 ext1 作为唤醒源;
  • 使用esp_sleep_get_ext1_wakeup_status()函数来获取触发唤醒的 GPIO。

下一个草图实现了所有这些更改。

/*
Deep Sleep with External Wake Up
=====================================
This code displays how to use deep sleep with
an external trigger as a wake up source and how
to store data in RTC memory to use it over reboots

This code is under Public Domain License.

Hardware Connections
======================
Push Button to GPIO 33 pulled down with a 10K Ohm
resistor

NOTE:
======
Only RTC IO can be used as a source for external wake
source. They are pins: 0,2,4,12-15,25-27,32-39.

Author:
Pranav Cherukupalli <cherukupallip@gmail.com>
*/

#define BUTTON_PIN_BITMASK 0x8004 // GPIOs 2 and 15

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;
  }
}

/*
Method to print the GPIO that triggered the wakeup
*/
void print_GPIO_wake_up(){
  uint64_t GPIO_reason = esp_sleep_get_ext1_wakeup_status();
  Serial.print("GPIO that triggered the wake up: GPIO ");
  Serial.println((log(GPIO_reason))/log(2), 0);
}
  
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();

  //Print the GPIO used to wake up
  print_GPIO_wake_up();

  /*
  First we configure the wake up source
  We set our ESP32 to wake up for an external trigger.
  There are two types for ESP32, ext0 and ext1 .
  ext0 uses RTC_IO to wakeup thus requires RTC peripherals
  to be on while ext1 uses RTC Controller so doesnt need
  peripherals to be powered on.
  Note that using internal pullups/pulldowns also requires
  RTC peripherals to be turned on.
  */
  //esp_deep_sleep_enable_ext0_wakeup(GPIO_NUM_15,1); //1 = High, 0 = Low

  //If you were to use ext1, you would use it like
  esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);

  //Go to sleep now
  Serial.println("Going to sleep now");
  delay(1000);
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}

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

您可以在代码的开头定义 GPIO 掩码:

#define BUTTON_PIN_BITMASK 0x8004 // GPIOs 2 and 15

创建一个函数来打印导致唤醒的 GPIO:

void print_GPIO_wake_up(){
  int GPIO_reason = esp_sleep_get_ext1_wakeup_status();
  Serial.print("GPIO that triggered the wake up: GPIO ");
  Serial.println((log(GPIO_reason))/log(2), 0);
}

此函数返回以 2 为基数的数字,GPIO 数字作为指数:2^(GPIO)。因此,要获得十进制的 GPIO,您需要执行以下计算:

GPIO = log(RETURNED_VALUE)/log(2)

最后,启用 ext1 作为唤醒源:

esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);

测试草图

已连接两个按钮GPIO 2和GPIO 15,您可以将提供的代码上传到 ESP32。确保选择了正确的主板和 COM 端口。

ESP32 现在处于深度睡眠模式。您可以通过按下按钮将其唤醒。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-i7qUO0iN-1646140834628)(https://i0.wp.com/randomnerdtutorials.com/wp-content/uploads/2019/02/DSC01884.jpg?resize=700%2C364&quality=100&strip=all&ssl=1)]

以波特率为115200打开串行显示器。按下按钮可唤醒 ESP32。

您应该在串行监视器上获得类似的东西。

esp32如何测试voip esp32gpio_sed_02

结束语

在本文中,我们向您展示了如何在 ESP32 中使用深度睡眠,以及唤醒深度睡眠的不同方法。您可以使用计时器、触摸引脚或 GPIO 状态更改来唤醒 ESP32。

让我们总结一下我们对每个唤醒源所看到的内容:

定时器唤醒

  • 若要启用计时器唤醒,请使用esp_sleep_enable_timer_wakeup(time_in_us)功能;
  • 使用esp_deep_sleep_start()功能开始深度睡眠。

触摸唤醒

  • 要将触摸引脚用作唤醒源,首先,您需要使用以下命令将中断连接到触摸引脚:触摸附加中断(触摸引脚,回调,阈值)
  • 然后,使用以下命令启用触摸引脚作为唤醒源:esp_sleep_enable_touchpad_wakeup()
  • 最后,使用esp_deep_sleep_start()功能,可将 ESP32 置于深度睡眠模式。

外部唤醒

  • 您只能将 RTC GPIO 用作外部唤醒;
  • 您可以使用两种不同的方法:ext0 和 ext1;
  • ext0 允许您使用单个 GPIO 引脚唤醒 ESP32;
  • ext1 允许您使用多个 GPIO 引脚唤醒 ESP32。

结束

有志者,事竟成,破釜沉舟,百二秦川终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。 – 蒲松龄

esp32如何测试voip esp32gpio_sed_03