|
| 1 | +/* |
| 2 | + MIT License |
| 3 | +
|
| 4 | + Copyright (c) 2021 Felix Biego |
| 5 | +
|
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | + The above copyright notice and this permission notice shall be included in all |
| 14 | + copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +#include <ESP32Time.h> |
| 26 | + |
| 27 | +#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */ |
| 28 | +#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */ |
| 29 | + |
| 30 | +ESP32Time rtc; |
| 31 | + |
| 32 | + |
| 33 | +void wakeup_reason() { |
| 34 | + esp_sleep_wakeup_cause_t wakeup_reason; |
| 35 | + |
| 36 | + wakeup_reason = esp_sleep_get_wakeup_cause(); |
| 37 | + switch (wakeup_reason) |
| 38 | + { |
| 39 | + case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; |
| 40 | + case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; |
| 41 | + case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break; |
| 42 | + case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break; |
| 43 | + case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break; |
| 44 | + default : |
| 45 | + Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); |
| 46 | + rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30 |
| 47 | + //rtc.setTime(1609459200); // 1st Jan 2021 00:00:00 |
| 48 | + //rtc.offset = 7200; // change offset value |
| 49 | + |
| 50 | + break; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void setup() { |
| 55 | + Serial.begin(115200); |
| 56 | + |
| 57 | + wakeup_reason(); |
| 58 | + |
| 59 | + Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format |
| 60 | + |
| 61 | + esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); |
| 62 | + |
| 63 | + Serial.println("Going to sleep now"); |
| 64 | + Serial.flush(); |
| 65 | + esp_deep_sleep_start(); |
| 66 | +} |
| 67 | + |
| 68 | +void loop() { |
| 69 | + |
| 70 | +} |
0 commit comments