Fix meter lifecycle and MQTT/HA integration bugs #1
+8
-3
@@ -14,6 +14,8 @@
|
|||||||
#define MQTT_MANUFACTURER "Baumann Enkataleiptics"
|
#define MQTT_MANUFACTURER "Baumann Enkataleiptics"
|
||||||
#define AUTH_USERNAME "admin"
|
#define AUTH_USERNAME "admin"
|
||||||
#define MQTT_PROBE_TIMEOUT_MS 400 // keep short: blocks loop()/handleClient() per reconnect attempt
|
#define MQTT_PROBE_TIMEOUT_MS 400 // keep short: blocks loop()/handleClient() per reconnect attempt
|
||||||
|
#define MQTT_PAYLOAD_AVAILABLE "online"
|
||||||
|
#define MQTT_PAYLOAD_NOT_AVAILABLE "offline"
|
||||||
|
|
||||||
struct MeterConfig {
|
struct MeterConfig {
|
||||||
int pin;
|
int pin;
|
||||||
@@ -256,6 +258,9 @@ static void mqttPublishDiscovery() {
|
|||||||
doc["name"] = name;
|
doc["name"] = name;
|
||||||
doc["state_topic"] = stat;
|
doc["state_topic"] = stat;
|
||||||
doc["command_topic"] = stat + "/set";
|
doc["command_topic"] = stat + "/set";
|
||||||
|
doc["availability_topic"] = String(mqttCfg.prefix) + "/status";
|
||||||
|
doc["payload_available"] = MQTT_PAYLOAD_AVAILABLE;
|
||||||
|
doc["payload_not_available"] = MQTT_PAYLOAD_NOT_AVAILABLE;
|
||||||
doc["min"] = meters[i].rangeMin;
|
doc["min"] = meters[i].rangeMin;
|
||||||
doc["max"] = meters[i].rangeMax;
|
doc["max"] = meters[i].rangeMax;
|
||||||
doc["step"] = 0.1;
|
doc["step"] = 0.1;
|
||||||
@@ -268,7 +273,7 @@ static void mqttPublishDiscovery() {
|
|||||||
|
|
||||||
char topic[128];
|
char topic[128];
|
||||||
snprintf(topic, sizeof(topic), "homeassistant/number/%s/config", objId.c_str());
|
snprintf(topic, sizeof(topic), "homeassistant/number/%s/config", objId.c_str());
|
||||||
char payload[512];
|
char payload[768];
|
||||||
serializeJson(doc, payload, sizeof(payload));
|
serializeJson(doc, payload, sizeof(payload));
|
||||||
bool pubOk = mqttClient.publish(topic, payload, true);
|
bool pubOk = mqttClient.publish(topic, payload, true);
|
||||||
Serial.printf("[MQTT] discovery %s -> %s (ok=%d)\n", topic, payload, pubOk);
|
Serial.printf("[MQTT] discovery %s -> %s (ok=%d)\n", topic, payload, pubOk);
|
||||||
@@ -323,11 +328,11 @@ static bool mqttConnect() {
|
|||||||
|
|
||||||
Serial.printf("[MQTT] connecting to %s:%d as %s\n", mqttCfg.host, mqttCfg.port, clientId);
|
Serial.printf("[MQTT] connecting to %s:%d as %s\n", mqttCfg.host, mqttCfg.port, clientId);
|
||||||
bool ok = mqttClient.connect(clientId, user, pass,
|
bool ok = mqttClient.connect(clientId, user, pass,
|
||||||
statusTopic, 0, true, "online: false");
|
statusTopic, 0, true, MQTT_PAYLOAD_NOT_AVAILABLE);
|
||||||
|
|
||||||
if (ok) {
|
if (ok) {
|
||||||
Serial.printf("[MQTT] connected to %s:%d\n", mqttCfg.host, mqttCfg.port);
|
Serial.printf("[MQTT] connected to %s:%d\n", mqttCfg.host, mqttCfg.port);
|
||||||
mqttClient.publish(statusTopic, "online: true", true);
|
mqttClient.publish(statusTopic, MQTT_PAYLOAD_AVAILABLE, true);
|
||||||
mqttSubscribe();
|
mqttSubscribe();
|
||||||
mqttPublishDiscovery();
|
mqttPublishDiscovery();
|
||||||
for (int i = 0; i < meterCount; i++)
|
for (int i = 0; i < meterCount; i++)
|
||||||
|
|||||||
Reference in New Issue
Block a user