arduino2/esp32_deep_sleep/esp32_deep_sleep.ino
2021-12-26 14:42:41 +01:00

85 lines
2.8 KiB
C++

#include "SSD1306.h" // alias for `#include "SSD1306Wire.h" // Board Handling
// jfs Wemos lolin32
// jfs Heltec WiFi kit 32 (weisses Board)
// #define HELTEC
// Initialize the OLED display using Wire library
#ifdef HELTEC
SSD1306 display(0x3c, 4, 15);
#else
SSD1306 display(0x3c, 5, 4);
#endif
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 30 /* Time ESP32 will go to sleep (in seconds) */
#define OLED
RTC_DATA_ATTR int bootCount = 0;
#define BUTTON_PIN_BITMASK 0x1000000000000000
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;
}
}
String reason = " Don't know ! ";
void get_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : reason = "using RTC_IO"; break;
case ESP_SLEEP_WAKEUP_EXT1 : reason = "using RTC_CNTL"; break;
case ESP_SLEEP_WAKEUP_TIMER : reason = "by timer"; break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : reason = "by touchpad"; break;
case ESP_SLEEP_WAKEUP_ULP : reason = "by ULP program"; break;
default : reason = "Dont know"; break;
}
}
void setup(){
Serial.begin(115200);
delay(10); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
print_wakeup_reason();
get_wakeup_reason();
#ifdef OLED
#ifdef HELTEC
pinMode(16,OUTPUT); digitalWrite(16, LOW); delay(50); digitalWrite(16, HIGH);
#endif
display.init();
//display.flipScreenVertically();
display.clear();
display.drawString(0,10,String(bootCount));
display.drawString(0,20,reason);
display.display();
#endif
Serial.println("Boot number: " + String(bootCount));
esp_sleep_enable_ext0_wakeup(GPIO_NUM_15,1); //1 = High, 0 = Low
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");
Serial.println("Going to sleep now");
delay(1000);
Serial.flush();
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop(){}