From 02c8fb3defc449e37f9a28cf2181e35e2b7f15b9 Mon Sep 17 00:00:00 2001 From: manuel Date: Mon, 28 Dec 2020 10:18:12 +0100 Subject: LR stripes: reorganize mqtt topics --- oliver/lr_stripes/src/main.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'oliver/lr_stripes/src/main.cpp') diff --git a/oliver/lr_stripes/src/main.cpp b/oliver/lr_stripes/src/main.cpp index 3fe2238..83095bb 100644 --- a/oliver/lr_stripes/src/main.cpp +++ b/oliver/lr_stripes/src/main.cpp @@ -13,7 +13,6 @@ //#define MQTT_PASSWORD "" #define MQTT_BASE "home/livingroom/ledstripe" - enum sensor_type : uint8_t { SENSOR_RELAY = (1u << 0), @@ -49,8 +48,8 @@ struct sensor_t sensors[] = { .type = SENSOR_RELAY | SENSOR_BUTTON | SENSOR_DIMMER, .relay = { .pin = D5, - .mqtt_sub = MQTT_BASE, - .mqtt_pub = MQTT_BASE "/status" + .mqtt_sub = MQTT_BASE "/power" , + .mqtt_pub = MQTT_BASE "/power/status" }, .button = { .pin = D0, @@ -78,7 +77,7 @@ struct sensor_t sensors[] = { // - increment number + build // - scp .pio/build/$ENV/firmware.bin manuel@mausz.at:public_html/coding/.firmware/olilrstripes.bin // - reboot device or send "fwupdate" to mqtt_topic_cmd -// - fw update state is published in mqtt_topic_state +// - fw update state is published in mqtt_topic_status #define FIRMWARE_VERSION 1 //#define FIRMWARE_URL "" @@ -87,9 +86,9 @@ struct sensor_t sensors[] = { const String mqtt_clientId = "LrStripes-" + String(ESP.getChipId()) + "/v" + STR(FIRMWARE_VERSION); -const char* mqtt_topic_ping = MQTT_BASE "/ping"; -const char* mqtt_topic_cmd = MQTT_BASE "/command"; -const String mqtt_topic_state = String(MQTT_BASE) + "/" + String(ESP.getChipId()); +const char* mqtt_topic_ping = MQTT_BASE "/ping"; +const char* mqtt_topic_status = MQTT_BASE "/status"; +const char* mqtt_topic_cmd = MQTT_BASE "/command"; bool mqttLoop(); void mqttCallback(char *topic, byte *payload, unsigned int length); @@ -172,11 +171,13 @@ bool mqttLoop() if (!mqtt.connected()) { Serial.printf_P(PSTR("Connecting to MQTT\n")); - if (!mqtt.connect(mqtt_clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) + if (!mqtt.connect(mqtt_clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD, + mqtt_topic_status, 0, true, "offline")) return false; Serial.printf_P(PSTR("MQTT connected\n")); mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); + mqtt.publish(mqtt_topic_status, "online", true); mqtt.subscribe(mqtt_topic_ping); mqtt.subscribe(mqtt_topic_cmd); @@ -373,7 +374,7 @@ void checkFirmwareUpdate() BearSSL::WiFiClientSecure update_client; update_client.setInsecure(); - mqtt.publish(mqtt_topic_state.c_str(), "fwupdate running"); + mqtt.publish(mqtt_topic_status, "fwupdate running"); ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); ESPhttpUpdate.rebootOnUpdate(true); t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); @@ -384,18 +385,18 @@ void checkFirmwareUpdate() Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); - mqtt.publish(mqtt_topic_state.c_str(), tmp.c_str()); + mqtt.publish(mqtt_topic_status, tmp.c_str()); } break; case HTTP_UPDATE_NO_UPDATES: Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); - mqtt.publish(mqtt_topic_state.c_str(), "fwupdate noupdates"); + mqtt.publish(mqtt_topic_status, "fwupdate noupdates"); break; case HTTP_UPDATE_OK: Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); - mqtt.publish(mqtt_topic_state.c_str(), "fwupdate ok"); + mqtt.publish(mqtt_topic_status, "fwupdate ok"); break; } } -- cgit v1.2.3