/*
Deep Sleep with External Wake Up
=====================================
Este código muestra cómo usar el modo de suspensión profunda con un disparador externo
como fuente de activación y cómo almacenar datos en la memoria RTC para usarlos
durante los reinicios.
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 <[email protected]>
*/
#include "driver/rtc_io.h"
#define BUTTON_PIN_BITMASK(GPIO) (1ULL << GPIO) // 2 ^ GPIO_NUMBER in hex
#define USE_EXT0_WAKEUP 1 // 1 = EXT0 wakeup, 0 = EXT1 wakeup
#define WAKEUP_GPIO GPIO_NUM_35 // Only RTC IO are allowed - ESP32 Pin example
RTC_DATA_ATTR int bootCount = 0;
const int buttonPin0 = 12; // boton placa
/*
Método para imprimir el motivo por el cual el ESP32 se despertó del modo de suspensión.
*/
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); //Tómese un tiempo para abrir el Monitor Serial
pinMode(buttonPin0, INPUT);
//Incrementa el número de arranque e imprímelo en cada reinicio
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Imprima el motivo de activación del ESP32
//
print_wakeup_reason();
/*
Primero configuramos la fuente de activación.
Configuramos nuestro ESP32 para que se active con un disparador externo.
Hay dos tipos de ESP32: ext0 y ext1.
ext0 usa RTC_IO para activarse, por lo que requiere que los periféricos RTC estén encendidos,
mientras que ext1 usa el controlador RTC, por lo que no necesita que los periféricos
estén encendidos.
Tenga en cuenta que el uso de pullups/pulldowns internos también requiere
que los periféricos RTC estén encendidos.
*/
#if USE_EXT0_WAKEUP
esp_sleep_enable_ext0_wakeup(WAKEUP_GPIO, 0); //1 = High, 0 = Low
// Configurar el pullup/down mediante RTCIO para vincular los pines de activación al nivel inactivo durante el sueño profundo.
// EXT0 reside en el mismo dominio de energía (RTC_PERIPH) que el pullup/down de E/S del RTC.
// No es necesario mantener ese dominio de energía explícitamente, a diferencia de EXT1.
//rtc_gpio_pullup_dis(WAKEUP_GPIO);
//rtc_gpio_pulldown_en(WAKEUP_GPIO);
#else // EXT1 WAKEUP
//Si usaras ext1, lo harías así
esp_sleep_enable_ext1_wakeup_io(BUTTON_PIN_BITMASK(WAKEUP_GPIO), ESP_EXT1_WAKEUP_ANY_HIGH);
/*
Si no hay pull-up/pulldown externos, se conectan los pines de activación a un nivel
inactivo con pull-up/pulldown internos a través de RTC IO durante el sueño profundo.
Sin embargo, RTC IO depende del dominio de energía RTC_PERIPH. Mantener este dominio
de energía activado aumentará el consumo de energía. Sin embargo, si desactivamos
el dominio RTC_PERIPH o si algunos chips carecen de él, usaremos la función HOLD
para mantener el pull-up y pull-down en los pines durante el sueño profundo.
*/
// rtc_gpio_pulldown_en(WAKEUP_GPIO); //GPIO33 está vinculada a GND para despertar en ALTO
// rtc_gpio_pullup_dis(WAKEUP_GPIO); // Deshabilite PULL_UP para permitir que se active en ALTO
#endif
delay(5000);
Serial.println("Despierto");
//Go to sleep now
//Serial.println("Me voy a dormir ahora");
//// esp_deep_sleep_start();
// Serial.println("Esto nunca se imprimirá");
}
void loop() {
int buttonState0 = digitalRead(buttonPin0);
if (buttonState0 == LOW ) {//HIGH
Serial.println("Me voy a domir");
delay(2000);
esp_deep_sleep_start();
}
//This is not going to be called
}
/*
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 <[email protected]>
*/
/*
#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex
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 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
//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
}*/