diff options
| author | manuel <manuel@mausz.at> | 2020-12-28 10:18:12 +0100 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2020-12-28 10:18:12 +0100 |
| commit | 02c8fb3defc449e37f9a28cf2181e35e2b7f15b9 (patch) | |
| tree | a0a0330d4ae845389b6b6f49b9e00385c57b1c2a | |
| parent | 1783df2792956e63de1bbae3ed7b036bdefec9a1 (diff) | |
| download | arduino-02c8fb3defc449e37f9a28cf2181e35e2b7f15b9.tar.gz arduino-02c8fb3defc449e37f9a28cf2181e35e2b7f15b9.tar.bz2 arduino-02c8fb3defc449e37f9a28cf2181e35e2b7f15b9.zip | |
LR stripes: reorganize mqtt topics
| -rw-r--r-- | oliver/lr_stripes/src/main.cpp | 25 |
1 files changed, 13 insertions, 12 deletions
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 @@ | |||
| 13 | //#define MQTT_PASSWORD "" | 13 | //#define MQTT_PASSWORD "" |
| 14 | #define MQTT_BASE "home/livingroom/ledstripe" | 14 | #define MQTT_BASE "home/livingroom/ledstripe" |
| 15 | 15 | ||
| 16 | |||
| 17 | enum sensor_type : uint8_t | 16 | enum sensor_type : uint8_t |
| 18 | { | 17 | { |
| 19 | SENSOR_RELAY = (1u << 0), | 18 | SENSOR_RELAY = (1u << 0), |
| @@ -49,8 +48,8 @@ struct sensor_t sensors[] = { | |||
| 49 | .type = SENSOR_RELAY | SENSOR_BUTTON | SENSOR_DIMMER, | 48 | .type = SENSOR_RELAY | SENSOR_BUTTON | SENSOR_DIMMER, |
| 50 | .relay = { | 49 | .relay = { |
| 51 | .pin = D5, | 50 | .pin = D5, |
| 52 | .mqtt_sub = MQTT_BASE, | 51 | .mqtt_sub = MQTT_BASE "/power" , |
| 53 | .mqtt_pub = MQTT_BASE "/status" | 52 | .mqtt_pub = MQTT_BASE "/power/status" |
| 54 | }, | 53 | }, |
| 55 | .button = { | 54 | .button = { |
| 56 | .pin = D0, | 55 | .pin = D0, |
| @@ -78,7 +77,7 @@ struct sensor_t sensors[] = { | |||
| 78 | // - increment number + build | 77 | // - increment number + build |
| 79 | // - scp .pio/build/$ENV/firmware.bin manuel@mausz.at:public_html/coding/.firmware/olilrstripes.bin | 78 | // - scp .pio/build/$ENV/firmware.bin manuel@mausz.at:public_html/coding/.firmware/olilrstripes.bin |
| 80 | // - reboot device or send "fwupdate" to mqtt_topic_cmd | 79 | // - reboot device or send "fwupdate" to mqtt_topic_cmd |
| 81 | // - fw update state is published in mqtt_topic_state | 80 | // - fw update state is published in mqtt_topic_status |
| 82 | #define FIRMWARE_VERSION 1 | 81 | #define FIRMWARE_VERSION 1 |
| 83 | //#define FIRMWARE_URL "" | 82 | //#define FIRMWARE_URL "" |
| 84 | 83 | ||
| @@ -87,9 +86,9 @@ struct sensor_t sensors[] = { | |||
| 87 | 86 | ||
| 88 | const String mqtt_clientId = "LrStripes-" + String(ESP.getChipId()) | 87 | const String mqtt_clientId = "LrStripes-" + String(ESP.getChipId()) |
| 89 | + "/v" + STR(FIRMWARE_VERSION); | 88 | + "/v" + STR(FIRMWARE_VERSION); |
| 90 | const char* mqtt_topic_ping = MQTT_BASE "/ping"; | 89 | const char* mqtt_topic_ping = MQTT_BASE "/ping"; |
| 91 | const char* mqtt_topic_cmd = MQTT_BASE "/command"; | 90 | const char* mqtt_topic_status = MQTT_BASE "/status"; |
| 92 | const String mqtt_topic_state = String(MQTT_BASE) + "/" + String(ESP.getChipId()); | 91 | const char* mqtt_topic_cmd = MQTT_BASE "/command"; |
| 93 | 92 | ||
| 94 | bool mqttLoop(); | 93 | bool mqttLoop(); |
| 95 | void mqttCallback(char *topic, byte *payload, unsigned int length); | 94 | void mqttCallback(char *topic, byte *payload, unsigned int length); |
| @@ -172,11 +171,13 @@ bool mqttLoop() | |||
| 172 | if (!mqtt.connected()) | 171 | if (!mqtt.connected()) |
| 173 | { | 172 | { |
| 174 | Serial.printf_P(PSTR("Connecting to MQTT\n")); | 173 | Serial.printf_P(PSTR("Connecting to MQTT\n")); |
| 175 | if (!mqtt.connect(mqtt_clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) | 174 | if (!mqtt.connect(mqtt_clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD, |
| 175 | mqtt_topic_status, 0, true, "offline")) | ||
| 176 | return false; | 176 | return false; |
| 177 | 177 | ||
| 178 | Serial.printf_P(PSTR("MQTT connected\n")); | 178 | Serial.printf_P(PSTR("MQTT connected\n")); |
| 179 | mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); | 179 | mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); |
| 180 | mqtt.publish(mqtt_topic_status, "online", true); | ||
| 180 | mqtt.subscribe(mqtt_topic_ping); | 181 | mqtt.subscribe(mqtt_topic_ping); |
| 181 | mqtt.subscribe(mqtt_topic_cmd); | 182 | mqtt.subscribe(mqtt_topic_cmd); |
| 182 | 183 | ||
| @@ -373,7 +374,7 @@ void checkFirmwareUpdate() | |||
| 373 | BearSSL::WiFiClientSecure update_client; | 374 | BearSSL::WiFiClientSecure update_client; |
| 374 | update_client.setInsecure(); | 375 | update_client.setInsecure(); |
| 375 | 376 | ||
| 376 | mqtt.publish(mqtt_topic_state.c_str(), "fwupdate running"); | 377 | mqtt.publish(mqtt_topic_status, "fwupdate running"); |
| 377 | ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); | 378 | ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); |
| 378 | ESPhttpUpdate.rebootOnUpdate(true); | 379 | ESPhttpUpdate.rebootOnUpdate(true); |
| 379 | t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); | 380 | t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); |
| @@ -384,18 +385,18 @@ void checkFirmwareUpdate() | |||
| 384 | Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), | 385 | Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), |
| 385 | ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); | 386 | ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); |
| 386 | String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); | 387 | String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); |
| 387 | mqtt.publish(mqtt_topic_state.c_str(), tmp.c_str()); | 388 | mqtt.publish(mqtt_topic_status, tmp.c_str()); |
| 388 | } | 389 | } |
| 389 | break; | 390 | break; |
| 390 | 391 | ||
| 391 | case HTTP_UPDATE_NO_UPDATES: | 392 | case HTTP_UPDATE_NO_UPDATES: |
| 392 | Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); | 393 | Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); |
| 393 | mqtt.publish(mqtt_topic_state.c_str(), "fwupdate noupdates"); | 394 | mqtt.publish(mqtt_topic_status, "fwupdate noupdates"); |
| 394 | break; | 395 | break; |
| 395 | 396 | ||
| 396 | case HTTP_UPDATE_OK: | 397 | case HTTP_UPDATE_OK: |
| 397 | Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); | 398 | Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); |
| 398 | mqtt.publish(mqtt_topic_state.c_str(), "fwupdate ok"); | 399 | mqtt.publish(mqtt_topic_status, "fwupdate ok"); |
| 399 | break; | 400 | break; |
| 400 | } | 401 | } |
| 401 | } | 402 | } |
