外部唤醒 – 多个 GPIO
现在,您应该能够使用不同的按钮唤醒 ESP32,并确定哪个按钮导致唤醒。在此示例中,我们将使用GPIO 2和GPIO 15作为唤醒源。
图解的
将两个按钮连接到 ESP32。在这个例子中,我们使用的是GPIO 2和GPIO 15,但您可以将按钮连接到任何 RTC GPIO。
法典
您需要对我们之前使用的示例代码进行一些修改:
- 创建要使用的位掩码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_EXT