From 42713c1bb113a3fb3af2841c9bd0dc7a8d41138e Mon Sep 17 00:00:00 2001 From: Matt Way Date: Tue, 13 Feb 2024 11:39:17 +1100 Subject: [PATCH] Publish battery level to MQTT --- src/main.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5fd788d..b228079 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,6 +45,7 @@ void startFetchingPosition(uint16_t shadeID, int8_t targetPosition); void publishPosition(const String& shadeName, const uint8_t position); void publishState(const String& shadeName, const String& state); +void publishBattery(const String& shadeName, const uint8_t battery); void publishDiscoveryTopics(); @@ -130,6 +131,15 @@ void processPacket(const Packet *packet) { publishPosition(shades[i].name, position); } } + } else if (field.identifier == 0x42) { + for (size_t i = 0; i < shades.size(); i++) { + if (packet->source == shades[i].ID) { + uint8_t value = std::get(field.value); + uint8_t battery = uint8_t(((float)value / 200) * 100); + + publishBattery(shades[i].name, battery); + } + } } } } @@ -207,9 +217,11 @@ bool sendFetchPosition(uint16_t destination) { std::vector fields; - uint8_t identifier = 0x50; // position - FieldType type = FieldType::FETCH; - fields.push_back(Field{identifier, type, false, std::monostate{}}); + // position + fields.push_back(Field{0x50, FieldType::FETCH, false, std::monostate{}}); + + // battery + fields.push_back(Field{0x42, FieldType::FETCH, false, std::monostate{}}); packet.parameters = FieldsParameters {fields}; @@ -331,6 +343,10 @@ void publishPosition(const String& shadeName, const uint8_t position) { client.publish("hotdog/" + shadeName + "/position", String(position), true); } +void publishBattery(const String& shadeName, const uint8_t battery) { + client.publish("hotdog/" + shadeName + "/battery", String(battery), true); +} + void publishDiscoveryTopics() { char buffer[1024];