Below is the code to turn a pin LED on/off every alternate cycle of deepsleep - meaning the first time esp32 sleeps, the LED will be ON, esp32 wakes up and then when it sleeps again the LED will be OFF, and so on. This prints everything correctly but the LED on GPIO 32 is always ON/HIGH.
Any idea what is going on?
#include <driver/rtc_io.h>
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 15 /* Time ESP32 will go to sleep (in seconds) */
const int OUTPIN = 32;
RTC_DATA_ATTR int bootCount = 0;
RTC_DATA_ATTR int pinStatus = 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",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("\nBoot number: " + String(bootCount));
// Set pin 32 as an output
pinMode(OUTPIN, OUTPUT);
rtc_gpio_set_direction((gpio_num_t)OUTPIN, RTC_GPIO_MODE_OUTPUT_ONLY);
// Make pin 32 high
if (pinStatus == 1) {
Serial.println("Should be LOW when sleep");
digitalWrite(OUTPIN, LOW);
rtc_gpio_set_level((gpio_num_t)OUTPIN, LOW);
pinStatus = 0;
}
else if (pinStatus == 0) {
Serial.println("Should be HIGH when sleep");
digitalWrite(OUTPIN, HIGH);
rtc_gpio_set_level((gpio_num_t)OUTPIN, HIGH);
pinStatus = 1;
}
else {
Serial.println("Floating");
}
delay(1000);
//this works but why doesn't it work in the if statement above?
//digitalWrite(OUTPIN, LOW);
//rtc_gpio_set_direction((gpio_num_t)OUTPIN, RTC_GPIO_MODE_OUTPUT_ONLY);
//rtc_gpio_set_level((gpio_num_t)OUTPIN, HIGH);
gpio_hold_en((gpio_num_t) OUTPIN);
gpio_deep_sleep_hold_en();
//Print the wakeup reason for ESP32
print_wakeup_reason();
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
}
void loop(){
if (millis() > 15000) {
Serial.println("Going to sleep now");
delay(1000);
Serial.flush();
esp_deep_sleep_start();;
}
}
Boot number: 1
Should be HIGH when sleep
Wakeup was not caused by deep sleep: 0
Setup ESP32 to sleep for every 15 Seconds
Going to sleep now
Boot number: 2
Should be LOW when sleep
Wakeup caused by timer
Setup ESP32 to sleep for every 15 Seconds
Going to sleep now
Boot number: 3
Should be HIGH when sleep
Wakeup was not caused by deep sleep: 0
Setup ESP32 to sleep for every 15 Seconds
Going to sleep now
Boot number: 4
Should be LOW when sleep
Wakeup caused by timer
Setup ESP32 to sleep for every 15 Seconds
Going to sleep now