diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | README.md | 30 | ||||
| -rw-r--r-- | config.example.h | 9 | ||||
| -rw-r--r-- | hello-t-energy-s3.ino | 167 |
4 files changed, 195 insertions, 12 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e56cf2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.h @@ -1,6 +1,6 @@ # hello-t-energy-s3 -Minimal test sketch for LilyGo T-Energy-S3 board. +T-Energy-S3 MQTT battery monitor. Connects to WiFi and publishes battery voltage over MQTT/TLS. ## Setup (one-time) @@ -12,8 +12,21 @@ arduino-cli config add board_manager.additional_urls https://espressif.github.io # Install ESP32 core arduino-cli core update-index arduino-cli core install esp32:esp32 + +# Install PubSubClient library +arduino-cli lib install "PubSubClient" +``` + +## Configuration + +Copy `config.example.h` to `config.h` and fill in your values: + +```bash +cp config.example.h config.h ``` +Then edit `config.h` with your WiFi and MQTT settings. + ## Build & Upload ```bash @@ -33,8 +46,19 @@ arduino-cli monitor --port /dev/ttyACM0 --config baudrate=115200 ## Expected Output ``` -Hello T-Energy-S3! -Battery: 4200 mV +=== T-Energy-S3 MQTT Monitor === + +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 ... ``` diff --git a/config.example.h b/config.example.h new file mode 100644 index 0000000..07eb416 --- /dev/null +++ b/config.example.h @@ -0,0 +1,9 @@ +// Copy this file to config.h and fill in your values + +#define WIFI_SSID "your-wifi-ssid" + +#define MQTT_HOST "mqtt.example.com" +#define MQTT_PORT 8883 +#define MQTT_USER "your-username" +#define MQTT_PASS "your-password" +#define MQTT_TOPIC "your/topic" 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; + } } |
