From 1b544949b8c26367f91538047d570fe9e3e18ad4 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Thu, 15 Jan 2026 12:40:15 +0100 Subject: Implement deep sleep with button wakeup --- README.md | 19 +++++-- hello-t-energy-s3.ino | 153 +++++++++++++++++++++----------------------------- 2 files changed, 76 insertions(+), 96 deletions(-) diff --git a/README.md b/README.md index 3f405cd..46050c5 100644 --- a/README.md +++ b/README.md @@ -43,22 +43,29 @@ If your port differs from `/dev/ttyACM0`, override it: make flash PORT=/dev/ttyUSB0 ``` +## Behavior + +1. On boot: connects to WiFi and MQTT, publishes voltage 3 times +2. Enters deep sleep +3. Press BOOT button to wake +4. Repeat + ## Expected Output ``` === T-Energy-S3 MQTT Monitor === +Initial boot MAC Address: AA:BB:CC:DD:EE:FF -Scanning WiFi... -Found 5 networks: - 1: MyNetwork (-45 dBm) - ... - Connecting to MyNetwork... Connected! IP: 192.168.1.100 Connecting to MQTT mqtt.example.com:8883... MQTT connected! Battery: 4200 mV Published to my/topic: 4200 -... +Battery: 4200 mV +Published to my/topic: 4200 +Battery: 4200 mV +Published to my/topic: 4200 +Entering deep sleep... press button to wake ``` diff --git a/hello-t-energy-s3.ino b/hello-t-energy-s3.ino index 77f33a2..dee6fda 100644 --- a/hello-t-energy-s3.ino +++ b/hello-t-energy-s3.ino @@ -1,5 +1,5 @@ -// T-Energy-S3 MQTT Battery Monitor -// Connects to WiFi, publishes battery voltage over MQTT/TLS +// T-Energy-S3 MQTT Battery Monitor (Deep Sleep Edition) +// Wakes on button press, publishes voltage, then sleeps #include #include @@ -7,47 +7,22 @@ #include "config.h" #define BATTERY_PIN 3 +#define BUTTON_PIN GPIO_NUM_0 +#define PUBLISH_COUNT 3 +#define PUBLISH_DELAY_MS 1000 WiFiClientSecure secureClient; PubSubClient mqtt(secureClient); -unsigned long lastPublish = 0; -unsigned long lastWifiCheck = 0; - void printMac() { Serial.printf("MAC Address: %s\n", WiFi.macAddress().c_str()); } -void scanWifi() -{ - Serial.println("Scanning WiFi..."); - int n = WiFi.scanNetworks(); - if (n == 0) - { - Serial.println("No networks found"); - } - else - { - Serial.printf("Found %d networks:\n", n); - for (int i = 0; i < n; i++) - { - Serial.printf(" %d: %s (%d dBm)%s\n", - i + 1, - WiFi.SSID(i).c_str(), - WiFi.RSSI(i), - WiFi.encryptionType(i) == WIFI_AUTH_OPEN ? "" : " *"); - } - } - Serial.println(); -} - bool connectWifi() { - if (WiFi.status() == WL_CONNECTED) - return true; - Serial.printf("Connecting to %s...\n", WIFI_SSID); + WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID); int attempts = 0; @@ -72,9 +47,6 @@ bool connectWifi() bool connectMqtt() { - if (mqtt.connected()) - return true; - Serial.printf("Connecting to MQTT %s:%d...\n", MQTT_HOST, MQTT_PORT); String clientId = "t-energy-" + WiFi.macAddress(); @@ -91,80 +63,81 @@ bool connectMqtt() } } +void publishVoltage() +{ + int mv = analogReadMilliVolts(BATTERY_PIN) * 2; + Serial.printf("Battery: %d mV\n", mv); + + char payload[16]; + snprintf(payload, sizeof(payload), "%d", mv); + if (mqtt.publish(MQTT_TOPIC, payload)) + { + Serial.printf("Published to %s: %s\n", MQTT_TOPIC, payload); + } + else + { + Serial.println("Publish failed"); + } + mqtt.loop(); +} + +void enterDeepSleep() +{ + Serial.println("Entering deep sleep... press button to wake"); + Serial.flush(); + + esp_sleep_enable_ext0_wakeup(BUTTON_PIN, LOW); + esp_deep_sleep_start(); +} + void setup() { Serial.begin(115200); - delay(2000); + delay(1000); Serial.println("\n=== T-Energy-S3 MQTT Monitor ===\n"); + // Check wake reason + esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause(); + if (wakeup_reason == ESP_SLEEP_WAKEUP_EXT0) + { + Serial.println("Woke up from button press!"); + } + else + { + Serial.println("Initial boot"); + } + + printMac(); + pinMode(BATTERY_PIN, INPUT); analogReadResolution(12); analogSetPinAttenuation(BATTERY_PIN, ADC_11db); - WiFi.mode(WIFI_STA); - WiFi.disconnect(); - delay(100); - - printMac(); - scanWifi(); - - // TLS setup - skip cert verification for now + // TLS setup secureClient.setInsecure(); - mqtt.setServer(MQTT_HOST, MQTT_PORT); - connectWifi(); - if (WiFi.status() == WL_CONNECTED) + if (connectWifi() && connectMqtt()) { - connectMqtt(); - } -} - -void loop() -{ - unsigned long now = millis(); - - // Check WiFi every 5 seconds - if (now - lastWifiCheck >= 5000) - { - if (WiFi.status() != WL_CONNECTED) + for (int i = 0; i < PUBLISH_COUNT; i++) { - Serial.println("WiFi disconnected, reconnecting..."); - connectWifi(); + publishVoltage(); + if (i < PUBLISH_COUNT - 1) + { + delay(PUBLISH_DELAY_MS); + } } - lastWifiCheck = now; } - // Maintain MQTT connection - if (WiFi.status() == WL_CONNECTED) - { - if (!mqtt.connected()) - { - connectMqtt(); - } - mqtt.loop(); - } + // Disconnect cleanly + mqtt.disconnect(); + WiFi.disconnect(true); - // Publish voltage every 5 seconds - if (now - lastPublish >= 5000) - { - int mv = analogReadMilliVolts(BATTERY_PIN) * 2; - Serial.printf("Battery: %d mV\n", mv); + enterDeepSleep(); +} - if (mqtt.connected()) - { - char payload[16]; - snprintf(payload, sizeof(payload), "%d", mv); - if (mqtt.publish(MQTT_TOPIC, payload)) - { - Serial.printf("Published to %s: %s\n", MQTT_TOPIC, payload); - } - else - { - Serial.println("Publish failed"); - } - } - lastPublish = now; - } +void loop() +{ + // Never reached - we sleep in setup() } -- cgit v1.3.1