Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include "esp_sleep.h"
- const gpio_num_t led_pin = GPIO_NUM_2; // on-board LED (optional)
- RTC_DATA_ATTR uint32_t boot_count = 0; // persists through deep-sleep
- void setup() {
- Serial.begin(115200);
- pinMode(led_pin, OUTPUT);
- // 1) Detect wake-up reason
- esp_sleep_wakeup_cause_t wake_reason = esp_sleep_get_wakeup_cause();
- if (wake_reason == ESP_SLEEP_WAKEUP_TIMER) {
- boot_count++;
- Serial.printf("woke by timer #%u\n", boot_count); // print wake count
- } else {
- Serial.println("power-on or other reset");
- }
- // (Optional) toggle LED to show activity
- digitalWrite(led_pin, boot_count % 2);
- // 2) Main task:
- Serial.println("running main task");
- // 3) Configure next wake by timer in 5 seconds
- const uint64_t wake_time_us = 5ULL * 1000000ULL; // 5 s
- esp_sleep_enable_timer_wakeup(wake_time_us);
- Serial.println("entering deep sleep for 5 s");
- Serial.flush(); // ensure all data is sent
- delay(50); // allow USB-CDC re-enumeration
- esp_deep_sleep_start(); // enter deep sleep
- }
- void loop() {
- // never reached
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement