The node
Our capstone: an ESP32 on a 3,000 mAh battery reads a soil sensor and reports over Wi-Fi to an MQTT broker. It must last a long time, so its life is one cycle: sleep, wake, read, sometimes send, sleep.
One hour in its life
After 600 s the RTC timer wakes the chip. Deep sleep had switched off the processors and most RAM, so the chip boots again and setup() runs from line 1. Only RTC memory kept its values.
The CPU drops to 80 MHz to save current. analogRead(34) gives 2210, out of 0 to 4095. GPIO34 is on ADC1 on purpose: Wi-Fi also uses ADC2. save() puts the reading in buf[0] in RTC memory; n becomes 1.
n is 1, not 6, so line 4 skips the radio and the chip goes straight back to sleep. Every 10 minutes the same: wake, read, save, sleep, about a second awake. After one hour n reaches 6.
Now n is 6, so sendAll() runs. WiFi.begin() joins the router: a fresh join every time, because deep sleep dropped the connection. The loop waits at most 10 seconds. The radio draws the most current of anything here.
The node connects to the broker over TLS on port 8883, checking its certificate, and publishes all 6 readings as one message to garden/node-1/soil. One radio session carries an hour of data.
sendAll() returned true, so n = 0 empties the buffer. sleepFor(600) turns the radio off, sets the RTC timer and enters deep sleep. The current falls to about 10 µA until the next wake.
Failures and the cost
Next hour the router is off. The join gives up after 10 s and sendAll() returns false, so n is not cleared: the readings wait in RTC memory for the next try. Never wait forever on a battery.
Sending on every wake averages 0.86 mA: the battery lasts about 146 days. Sending every sixth wake averages 0.19 mA: about 642 days, 4.4 times longer. Same chip, same sensor: only the radio plan changed.
What happens
;; step 1/9 · node
board ESP32 DevKitC, Arduino-ESP32 core 3.x
sensor soil probe on GPIO34
battery 3,000 mAh lithium-ion cell
network Wi-Fi -> MQTT broker, topic garden/node-1/soil
cycle sleep -> wake -> read -> (send) -> sleep
// ESP32 DevKitC, Arduino-ESP32 core 3.x. Soil sensor on GPIO34 (ADC1).
// Wakes every 600 s; publishes 6 readings at a time over MQTT + TLS.
#include <WiFi.h>
#include <NetworkClientSecure.h>
#include <PubSubClient.h>
#include "secrets.h" // WIFI_SSID, WIFI_PASS, BROKER, CA_CERT
const int MAXN = 12;
RTC_DATA_ATTR int n = 0; // kept in RTC memory
RTC_DATA_ATTR int buf[MAXN]; // through deep sleep
NetworkClientSecure net;
PubSubClient mqtt(net);
void save(int v) {
if (n == MAXN) { // full: drop the oldest
memmove(buf, buf + 1, (MAXN - 1) * sizeof(int));
n--;
}
buf[n++] = v;
}
bool sendAll() {
WiFi.begin(WIFI_SSID, WIFI_PASS);
for (int i = 0; i < 100 && WiFi.status() != WL_CONNECTED; i++)
delay(100); // at most 10 s
if (WiFi.status() != WL_CONNECTED) return false;
net.setCACert(CA_CERT);
mqtt.setServer(BROKER, 8883);
if (!mqtt.connect("node-1")) return false;
String msg = "[";
for (int i = 0; i < n; i++) {
if (i) msg += ',';
msg += buf[i];
}
msg += "]";
bool ok = mqtt.publish("garden/node-1/soil", msg.c_str());
mqtt.disconnect();
return ok;
}
void sleepFor(uint64_t s) {
WiFi.disconnect(true); // radio off
esp_sleep_enable_timer_wakeup(s * 1000000ULL);
esp_deep_sleep_start(); // never returns
}
void setup() {
setCpuFrequencyMhz(80);
save(analogRead(34));
if (n >= 6 && sendAll())
n = 0;
sleepFor(600);
}
void loop() {} // never reached: every wake boots into setup()
The card shows only setup(). The whole sketch adds save(), which keeps up to 12 readings in RTC memory and drops the oldest when full, sendAll(), and sleepFor(). loop() is empty: every wake from deep sleep boots into setup() again. Currents are from the ESP32 datasheet v5.3: about 31 mA awake at 80 MHz (dual-core, radio off), 240 mA transmitting 802.11b at +19.5 dBm, 10 µA in deep sleep with the RTC timer and RTC memory on. The 1 s awake and 2 s radio times are assumptions, as in the lesson "Power budgets and battery life"; measure your own board.