diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 9be2cd84a..e711cef95 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -74,6 +74,10 @@ void Mqtt::subscribe(const uint8_t device_type, const std::string & topic, mqtt_ // add to MQTT queue as a subscribe operation auto message = queue_subscribe_message(topic); + if (message == nullptr) { + return; + } + // register in our libary with the callback function. // We store both the original topic and the fully-qualified one mqtt_subfunctions_.emplace_back(device_type, std::move(topic), std::move(message->topic), std::move(cb)); @@ -556,7 +560,7 @@ std::shared_ptr Mqtt::queue_message(const uint8_t operation, // add MQTT message to queue, payload is a string std::shared_ptr Mqtt::queue_publish_message(const std::string & topic, const std::string & payload, bool retain) { - if (!enabled()) { + if (!enabled() || !connected()) { return nullptr; }; return queue_message(Operation::PUBLISH, topic, payload, retain);