🛠 Station météo autonome en deep sleep
Objectives
Build an autonomous weather station: measure temperature and humidity with an SHT31, then put the ESP32 to sleep between measurements. In deep sleep, the ESP32 drops to approximately 150 µA — only the timer and RTC remain active, and RTC memory preserves variables marked with RTC_DATA_ATTR. You will master wake-up causes, I2C, and power management discipline.
Hardware
- 1 ESP32 DevKit v1 board
- 1 SHT31 sensor via I2C (SDA GPIO21, SCL GPIO22, address 0x44)
- Optional: micro-ammeter in series with the power supply
Complete code
#include <Wire.h>
#include <Adafruit_SHT31.h>
Adafruit_SHT31 sht31 = Adafruit_SHT31;
RTC_DATA_ATTR uint32_t compteur = 0; // survit au deep sleep
void setup {
Serial.begin(115200);
delay(200);
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause;
Serial.printf("\nReveil : %d (4 = timer)\n", (int)cause);
Wire.begin(21, 22);
sht31.begin(0x44);
float t = sht31.readTemperature;
float h = sht31.readHumidity;
compteur++;
Serial.printf("Mesure %u : %.2f C, %.1f %%\n", compteur, t, h);
esp_sleep_enable_timer_wakeup(900ULL * 1000000ULL); // 15 min
Serial.println(F("Endormissement..."));
Serial.flush;
esp_deep_sleep_start;
}
void loop {} // jamais atteint : tout se passe dans setup
Expected verification
At each wake-up, the console displays a measurement with an increasing counter (RTC memory is indeed preserved), then the board stays silent for 15 minutes. With a micro-ammeter, you measure approximately 40 mA during measurement and approximately 150 µA during deep sleep.
Common errors
- Forgetting
Serial.flushbeforeesp_deep_sleep_start: the last lines are cut off. - Believing in deep sleep while the regulator and the SHT31 remain powered: the dev board consumes much more than 150 µA; this figure concerns the bare module.
- Variable without
RTC_DATA_ATTR: it resets to 0 at each wake-up. - Planning a button wake-up on a non-RTC pin:
ext0is only available on RTC GPIOs (0, 2, 4, 12-15, 25-27, 32-39).
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinorespiration.inoArduinodht22.inoArduinooled-i2c.inoArduinoserveur-web.inoArduino