summaryrefslogtreecommitdiff
path: root/hello-t-energy-s3.ino
blob: 77f33a23047685d95ce1797f563c5c9a9a77d101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// 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

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);

    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()
{
    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;
    }
}