fixing mqtt task

This commit is contained in:
Peter Buchegger 2022-03-19 22:18:07 +01:00
parent 3fbed7b963
commit 8d494c0832
2 changed files with 6 additions and 16 deletions

View file

@ -46,29 +46,19 @@ bool MQTTTask::loop(System &system) {
topic = topic + "/"; topic = topic + "/";
} }
topic = topic + system.getUserConfig()->callsign; topic = topic + system.getUserConfig()->callsign;
system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, getName(), "Send MQTT with topic: '%s', data: %s", topic, r);
logPrintD("Send MQTT with topic: \"");
logPrintD(topic);
logPrintD("\", data: ");
logPrintlnD(r);
_MQTT.publish(topic.c_str(), r.c_str()); _MQTT.publish(topic.c_str(), r.c_str());
} }
_MQTT.loop(); _MQTT.loop();
return true; return true;
} }
bool MQTTTask::connect(const System &system) { bool MQTTTask::connect(System &system) {
logPrintI("Connecting to MQTT broker: "); system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_INFO, getName(), "Connecting to MQTT broker: %s on port %d", system.getUserConfig()->mqtt.server, system.getUserConfig()->mqtt.port);
logPrintI(system.getUserConfig()->mqtt.server);
logPrintI(" on port ");
logPrintlnI(String(system.getUserConfig()->mqtt.port));
if (_MQTT.connect(system.getUserConfig()->callsign.c_str(), system.getUserConfig()->mqtt.name.c_str(), system.getUserConfig()->mqtt.password.c_str())) { if (_MQTT.connect(system.getUserConfig()->callsign.c_str(), system.getUserConfig()->mqtt.name.c_str(), system.getUserConfig()->mqtt.password.c_str())) {
logPrintI("Connected to MQTT broker as: "); system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_INFO, getName(), "Connected to MQTT broker as: %s", system.getUserConfig()->callsign);
logPrintlnI(system.getUserConfig()->callsign);
return true; return true;
} }
logPrintlnI("Connecting to MQTT broker faild. Try again later."); system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_INFO, getName(), "Connecting to MQTT broker failed. Try again later.");
return false; return false;
} }

View file

@ -20,7 +20,7 @@ private:
WiFiClient _client; WiFiClient _client;
PubSubClient _MQTT; PubSubClient _MQTT;
bool connect(const System &system); bool connect(System &system);
}; };
#endif #endif