deep_sleep.c

ESP32-H2 · File : deep_sleep.c · Language : C · Use case : Sommeil profond minuté sur horloge RTC

A battery 802.15.4 node sleeps 99.9% of the time: the RTC timer wakes it, the program works for a fraction of a second and sleeps again. RTC_NOINIT_ATTR keeps the wake counter in powered RTC memory.

Hardware required

An ESP32-H2 board (RISC-V 96 MHz, IEEE 802.15.4 radio only — no Wi-Fi) such as a DevKitM-1, powered over USB. None; a multimeter in µA range between battery and GND to check sleep current.

Wiring

No wiring. On battery, power the module directly: the DevKit regulators dominate sleep consumption.

How it works

esp_sleep_enable_timer_wakeup arms the slow RTC clock; esp_deep_sleep_start cuts CPU, RAM and radio. Waking is a real reset — only RTC and NOINIT segments survive. Program it with ESP-IDF (idf.py set-target esp32h2); the USB-Serial/JTAG port (GPIO26/27) acts as console and flash port. Hold Boot + Reset for download mode if needed.

Going further

Also wake on a sensor (ext1 across several pins), or adapt the period to 802.15.4 network quality.

deep_sleep.c

// deep_sleep.c — reveil toutes les 60 s, compteur en RTC memory.
#include "esp_sleep.h"
#include "esp_rtc.h"
#include "driver/gpio.h"
#include "esp_log.h"

RTC_NOINIT_ATTR int nb_reveils;      // survit au deep sleep

static const char *TAG = "dodo";

void app_main(void)
{
    esp_log_level_set("*", ESP_LOG_INFO);
    nb_reveils++;
    ESP_LOGI(TAG, "Reveil n°%d — travail court puis 60 s de sommeil", nb_reveils);

    // (lecture capteur + envoi 802.15.4 ici, en < 100 ms)

    // Etteindre la radio et les peripheriques avant de dormir
    ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(60ULL * 1000000ULL));
    ESP_LOGI(TAG, "deep sleep maintenant (%u us)", (unsigned)(60e6));
    fflush(stdout);
    esp_deep_sleep_start();
    // jamais atteint : au reveil, reprise complete de app_main
}

← Back to the board