summaryrefslogtreecommitdiff
path: root/hello-t-energy-s3.ino
diff options
context:
space:
mode:
Diffstat (limited to 'hello-t-energy-s3.ino')
-rw-r--r--hello-t-energy-s3.ino153
1 files changed, 63 insertions, 90 deletions
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 <WiFi.h>
#include <WiFiClientSecure.h>
@@ -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()
}