diff --git a/src/MqttPlugin.cpp b/src/MqttPlugin.cpp index 06ed736..39e03ba 100644 --- a/src/MqttPlugin.cpp +++ b/src/MqttPlugin.cpp @@ -35,17 +35,18 @@ void MqttPlugin::connect() if (client->connect(mqttConfig.clientName)) { // bind handlers to all local subscriptions - for (auto const &localSub : mediator->subscriptions) + if (!locallySubscribed) { - // bind all local topics to the MQTT upstream once - if (!locallySubscribed) + for (auto const &localSub : mediator->subscriptions) { + // bind all local topics to the MQTT upstream once subscribe(localSub.first.c_str(), bind(&MqttPlugin::upstreamHandler, this, localSub.first.c_str(), _1)); + // subscribe topic on remote queue to dispatch messages to local subscriptions + client->subscribe( + (String(mqttConfig.inTopicRoot) + String(localSub.first.c_str())) + .c_str()); } - // subscribe topic on remote queue to dispatch messages to local subscriptions - client->subscribe( - (String(mqttConfig.inTopicRoot) + String(localSub.first.c_str())) - .c_str()); + locallySubscribed = true; } PRINT_MSG(Serial, "MQTT", "connected"); }