diff options
| author | Yuval Adam <_@yuv.al> | 2026-01-15 11:28:16 +0100 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2026-01-15 11:28:16 +0100 |
| commit | 7f36a29aa97919fdf394b19cb325a900800fb080 (patch) | |
| tree | cd3ad05632411e77fbee16893875b965e0c8d89a /hello-t-energy-s3.ino | |
| parent | fd7c017a6fab43eb1e6fbc39a36b716359a4dc29 (diff) | |
Wifi mqtt and config
Diffstat (limited to 'hello-t-energy-s3.ino')
| -rw-r--r-- | hello-t-energy-s3.ino | 167 |
1 files changed, 158 insertions, 9 deletions
diff --git a/hello-t-energy-s3.ino b/hello-t-energy-s3.ino index fd0a05d..77f33a2 100644 --- a/hello-t-energy-s3.ino +++ b/hello-t-energy-s3.ino @@ -1,21 +1,170 @@ -// Minimal T-Energy-S3 test -// Reads battery voltage and prints to USB serial +// T-Energy-S3 MQTT Battery Monitor +// Connects to WiFi, publishes battery voltage over MQTT/TLS + +#include <WiFi.h> +#include <WiFiClientSecure.h> +#include <PubSubClient.h> +#include "config.h" #define BATTERY_PIN 3 -void setup() { +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.begin(WIFI_SSID); + + int attempts = 0; + while (WiFi.status() != WL_CONNECTED && attempts < 20) + { + delay(500); + Serial.print("."); + attempts++; + } + + if (WiFi.status() == WL_CONNECTED) + { + Serial.printf("\nConnected! IP: %s\n", WiFi.localIP().toString().c_str()); + return true; + } + else + { + Serial.println("\nWiFi connection failed"); + return false; + } +} + +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(); + + if (mqtt.connect(clientId.c_str(), MQTT_USER, MQTT_PASS)) + { + Serial.println("MQTT connected!"); + return true; + } + else + { + Serial.printf("MQTT failed, rc=%d\n", mqtt.state()); + return false; + } +} + +void setup() +{ Serial.begin(115200); - delay(2000); // Wait for USB CDC to initialize + delay(2000); - Serial.println("Hello T-Energy-S3!"); + Serial.println("\n=== T-Energy-S3 MQTT Monitor ===\n"); 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 + secureClient.setInsecure(); + + mqtt.setServer(MQTT_HOST, MQTT_PORT); + + connectWifi(); + if (WiFi.status() == WL_CONNECTED) + { + connectMqtt(); + } } -void loop() { - int mv = analogReadMilliVolts(BATTERY_PIN) * 2; // Voltage divider on board - Serial.printf("Battery: %d mV\n", mv); - delay(2000); +void loop() +{ + unsigned long now = millis(); + + // Check WiFi every 5 seconds + if (now - lastWifiCheck >= 5000) + { + if (WiFi.status() != WL_CONNECTED) + { + Serial.println("WiFi disconnected, reconnecting..."); + connectWifi(); + } + lastWifiCheck = now; + } + + // Maintain MQTT connection + if (WiFi.status() == WL_CONNECTED) + { + if (!mqtt.connected()) + { + connectMqtt(); + } + mqtt.loop(); + } + + // Publish voltage every 5 seconds + if (now - lastPublish >= 5000) + { + int mv = analogReadMilliVolts(BATTERY_PIN) * 2; + Serial.printf("Battery: %d mV\n", mv); + + 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; + } } |
