10 µA31 mA240 mAGPIO34Wi-FiESP32RTCz zrouterbuf[0]buf[1]buf[2]buf[3]buf[4]buf[5]MQTT1void setup() {2 setCpuFrequencyMhz(80);3 save(analogRead(34));4 if (n >= 6 && sendAll())5 n = 0;6 sleepFor(600);7}radio on avg mA daysevery wake 0.86 146every 6th 0.19 642221022242239225122662280
Back: the node's board (battery, ESP32 with RTC timer, soil sensor, antenna, RTC memory), the router and the brokerFront: the firmware card and the battery-life cardTokens: wake pulses, readings, Wi-Fi join messages and the MQTT messageA battery sensor node: an ESP32 with its RTC timer, a soil sensor on GPIO34, a 3,000 mAh cell and six RTC memory cells, next to a Wi-Fi router and an MQTT broker. The RTC wakes the chip every 600 s; each wake keeps one reading in RTC memory and sleeps again. Every sixth wake the radio joins Wi-Fi and publishes all six readings in one message, then the buffer empties. If the router is down the join gives up after 10 s and the readings wait. Using the radio once an hour instead of every wake stretches the battery from about 146 to 642 days.

The node

  1. 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

Failures and the cost

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.