summaryrefslogtreecommitdiff
path: root/hello-t-energy-s3.ino
blob: dee6fdae0d1e65ad274e44d415fbfc86d60c1019 (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
// T-Energy-S3 MQTT Battery Monitor (Deep Sleep Edition)
// Wakes on button press, publishes voltage, then sleeps

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#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);

void printMac()
{
    Serial.printf("MAC Address: %s\n", WiFi.macAddress().c_str());
}

bool connectWifi()
{
    Serial.printf("Connecting to %s...\n", WIFI_SSID);
    WiFi.mode(WIFI_STA);
    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()
{
    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 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(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);

    // TLS setup
    secureClient.setInsecure();
    mqtt.setServer(MQTT_HOST, MQTT_PORT);

    if (connectWifi() && connectMqtt())
    {
        for (int i = 0; i < PUBLISH_COUNT; i++)
        {
            publishVoltage();
            if (i < PUBLISH_COUNT - 1)
            {
                delay(PUBLISH_DELAY_MS);
            }
        }
    }

    // Disconnect cleanly
    mqtt.disconnect();
    WiFi.disconnect(true);

    enterDeepSleep();
}

void loop()
{
    // Never reached - we sleep in setup()
}