/*
Deep Sleep with Touch Wake Up
=====================================
This code displays how to use deep sleep with
a touch 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.
Author:
Pranav Cherukupalli <cherukupallip@gmail.com>
*/#define Threshold 40 /* Greater the value, more the sensitivity */RTC_DATA_ATTRintbootCount=0;touch_pad_ttouchPin;
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/voidprint_wakeup_reason(){esp_sleep_wakeup_cause_twakeup_reason;wakeup_reason=esp_sleep_get_wakeup_cause();switch(wakeup_reason){caseESP_SLEEP_WAKEUP_EXT0:Serial.println("Wakeup caused by external signal using RTC_IO");break;caseESP_SLEEP_WAKEUP_EXT1:Serial.println("Wakeup caused by external signal using RTC_CNTL");break;caseESP_SLEEP_WAKEUP_TIMER:Serial.println("Wakeup caused by timer");break;caseESP_SLEEP_WAKEUP_TOUCHPAD:Serial.println("Wakeup caused by touchpad");break;caseESP_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 touchpad by which ESP32
has been awaken from sleep
*/voidprint_wakeup_touchpad(){touchPin=esp_sleep_get_touchpad_wakeup_status();switch(touchPin){case0:Serial.println("Touch detected on GPIO 4");break;case1:Serial.println("Touch detected on GPIO 0");break;case2:Serial.println("Touch detected on GPIO 2");break;case3:Serial.println("Touch detected on GPIO 15");break;case4:Serial.println("Touch detected on GPIO 13");break;case5:Serial.println("Touch detected on GPIO 12");break;case6:Serial.println("Touch detected on GPIO 14");break;case7:Serial.println("Touch detected on GPIO 27");break;case8:Serial.println("Touch detected on GPIO 33");break;case9:Serial.println("Touch detected on GPIO 32");break;default:Serial.println("Wakeup not by touchpad");break;}}
voidsetup(){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 and touchpad too
print_wakeup_reason();print_wakeup_touchpad();//Setup interrupt on Touch Pad 3 (GPIO15)
touchAttachInterrupt(T3,callback,Threshold);//Configure Touchpad as wakeup source
esp_sleep_enable_touchpad_wakeup();//Go to sleep now
Serial.println("Going to sleep now");esp_deep_sleep_start();Serial.println("This will never be printed");}
初回のブートは、Wakeup was not caused by deep sleep / Wakeup not by touchpad と表示され、2回目のブート(ディープスリープからの復帰)は、Wakeup caused by touhpad / Touch detected on GPIO 15 と表示されました。