use global mqtt #3

Merged
tliu93 merged 1 commits from bugfix/light_mqtt_nullptr into main 2025-08-29 10:25:42 +02:00
3 changed files with 14 additions and 16 deletions

View File

@@ -14,8 +14,8 @@ const struct {
} Availability; } Availability;
Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Mqtt* mqttClient, std::string uniqueId) Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, std::string uniqueId)
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(nullptr), pinWW(nullptr), mqttClient(mqttClient) { : pinR(pinR), pinG(pinG), pinB(pinB), pinCW(nullptr), pinWW(nullptr) {
lightInfo.uniqueId = uniqueId; lightInfo.uniqueId = uniqueId;
lightType = LightType::rgb; lightType = LightType::rgb;
uint8_t bits = pinR->getLedResolutionBits(); uint8_t bits = pinR->getLedResolutionBits();
@@ -24,8 +24,8 @@ Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Mqtt* mqttClient, std::string uniq
subscribeToMqttTopics(); subscribeToMqttTopics();
} }
Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, Mqtt* mqttClient, std::string uniqueId) Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, std::string uniqueId)
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(pinCW), pinWW(pinWW), mqttClient(mqttClient) { : pinR(pinR), pinG(pinG), pinB(pinB), pinCW(pinCW), pinWW(pinWW) {
lightInfo.uniqueId = uniqueId; lightInfo.uniqueId = uniqueId;
lightType = LightType::rgbww; lightType = LightType::rgbww;
uint8_t bits = pinR->getLedResolutionBits(); uint8_t bits = pinR->getLedResolutionBits();
@@ -76,7 +76,7 @@ void Light::publishInitialState() {
std::string configJson; std::string configJson;
serializeJson(configInfo, configJson); serializeJson(configInfo, configJson);
mqttClient->publish(lightInfo.discoveryTopic, configJson); Mqtt::publish(lightInfo.discoveryTopic, configJson);
std::string stateJson; std::string stateJson;
JsonDocument stateInfo; JsonDocument stateInfo;
@@ -95,8 +95,8 @@ void Light::publishInitialState() {
JsonDocument availabilityInfoDoc; JsonDocument availabilityInfoDoc;
availabilityInfoDoc["availability"] = Availability.available; availabilityInfoDoc["availability"] = Availability.available;
serializeJson(availabilityInfoDoc, availabilityJson); serializeJson(availabilityInfoDoc, availabilityJson);
mqttClient->publish(lightInfo.stateTopic, stateJson); Mqtt::publish(lightInfo.stateTopic, stateJson);
mqttClient->publish(lightInfo.availabilityTopic, availabilityJson); Mqtt::publish(lightInfo.availabilityTopic, availabilityJson);
} }
void Light::operatePin() { void Light::operatePin() {
@@ -159,7 +159,7 @@ void Light::operatePin() {
} }
void Light::subscribeToMqttTopics() { void Light::subscribeToMqttTopics() {
mqttClient->subscribe(lightInfo.commandTopic, [this](uint8_t* payload, int length) { Mqtt::subscribe(lightInfo.commandTopic, [this](uint8_t* payload, int length) {
std::string command(reinterpret_cast<char*>(payload), length); std::string command(reinterpret_cast<char*>(payload), length);
handleCommand(command); handleCommand(command);
}); });
@@ -241,11 +241,11 @@ void Light::publishCurrentState() {
std::string stateJson; std::string stateJson;
serializeJson(stateInfo, stateJson); serializeJson(stateInfo, stateJson);
Serial.println("Publishing current state: " + String(stateJson.c_str())); Serial.println("Publishing current state: " + String(stateJson.c_str()));
mqttClient->publish(lightInfo.stateTopic, stateJson); Mqtt::publish(lightInfo.stateTopic, stateJson);
std::string attributeJson; std::string attributeJson;
serializeJson(attributeInfo, attributeJson); serializeJson(attributeInfo, attributeJson);
Serial.println("Publishing current attributes: " + String(attributeJson.c_str())); Serial.println("Publishing current attributes: " + String(attributeJson.c_str()));
mqttClient->publish(lightInfo.jsonAttributesTopic, attributeJson); Mqtt::publish(lightInfo.jsonAttributesTopic, attributeJson);
} }
uint32_t Light::correctGamma(uint32_t originalPwm) { uint32_t Light::correctGamma(uint32_t originalPwm) {

View File

@@ -2,7 +2,6 @@
#include <cstdint> #include <cstdint>
#include "pin.h" #include "pin.h"
#include "mqtt.h"
struct LightInfo { struct LightInfo {
std::string uniqueId; std::string uniqueId;
@@ -65,9 +64,9 @@ enum ActiveMode {
class Light { class Light {
public: public:
Light(Pin* pinR, Pin* pinG, Pin* pinB, Mqtt* mqttClient, std::string uniqueId); Light(Pin* pinR, Pin* pinG, Pin* pinB, std::string uniqueId);
Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Mqtt* mqttClient, std::string uniqueId); Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, std::string uniqueId);
Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, Mqtt* mqttClient, std::string uniqueId); Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, std::string uniqueId);
void subscribeToMqttTopics(); void subscribeToMqttTopics();
void publishInitialState(); void publishInitialState();
void publishCurrentState(); void publishCurrentState();
@@ -97,7 +96,6 @@ private:
Pin* pinB; Pin* pinB;
Pin* pinCW; Pin* pinCW;
Pin* pinWW; Pin* pinWW;
Mqtt* mqttClient;
LightInfo lightInfo; LightInfo lightInfo;
DeviceInfo deviceInfo; DeviceInfo deviceInfo;
LightType lightType = onOff; // Default light type LightType lightType = onOff; // Default light type

View File

@@ -40,7 +40,7 @@ void setup() {
network->registerMDNS(); network->registerMDNS();
Mqtt::connect("10.238.75.81", 1883, "smart_rgb_client", "mqtt", "mqtt"); Mqtt::connect("10.238.75.81", 1883, "smart_rgb_client", "mqtt", "mqtt");
delay(1000); // Wait for MQTT connection to stabilize delay(1000); // Wait for MQTT connection to stabilize
light = new Light(pinR, pinG, pinB, pinCW, pinWW, mqttClient, "smart_rgb_light"); light = new Light(pinR, pinG, pinB, pinCW, pinWW, "smart_rgb_light");
initializeScheduler(); initializeScheduler();
} }