Skip to content

Instantly share code, notes, and snippets.

@Tech500
Last active October 23, 2022 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tech500/41ca5fdef19b8f9e169c2ed711247779 to your computer and use it in GitHub Desktop.
Save Tech500/41ca5fdef19b8f9e169c2ed711247779 to your computer and use it in GitHub Desktop.
/*
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>
Reference article: https://randomnerdtutorials.com/esp32-external-wake-up-deep-sleep/
*/
#include <WiFi.h>
#include "driver/adc.h"
#include <esp_wifi.h>
#include <esp_bt.h>
#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex --See reference article.
float currentMillis, startMillis, elapsed;
// How many minutes the ESP should be awake
#define AWAKE_TIME 1 //Minutes
const unsigned long period = (AWAKE_TIME * 60L * 1000L); //the value is a number of milliseconds in 15 Minutes.
//const unsigned long period = 60000; //the value is a number of milliseconds in 5 seconds
#define SLEEP_LED 26 //LED is off in deep sleep
RTC_DATA_ATTR int bootCount = 0;
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\n", wakeup_reason); break;
}
}
void setup(){
Serial.begin(115200);
while(!Serial){};
startMillis = millis(); //initial start time
//Print the wakeup reason for ESP32
print_wakeup_reason();
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
/*
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_sleep_enable_ext0_wakeup(GPIO_NUM_33,1); //1 = High, 0 = Low
pinMode(SLEEP_LED,OUTPUT);
digitalWrite(SLEEP_LED,HIGH); //SLEEP_LED will be Off in deep sleep
}
void loop(){
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
elapsed = currentMillis - startMillis;
Serial.print(" Minutes: ");
Serial.print(((elapsed/1000)/60), 1);
Serial.println(" Awake time");
Serial.println("");
Serial.println("Going to Deep Sleep");
gotoDeepSleep();
}
}
void gotoDeepSleep()
{
//Serial.println("Going to sleep...");
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
btStop();
adc_power_off(); //turn off adc
esp_wifi_stop(); //turn off wifi
esp_bt_controller_disable(); //turn off bluetooth
// Goto deep sleep
esp_deep_sleep_start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment