From d89630a34013210f7d56d6b6a92c643ffeb66a40 Mon Sep 17 00:00:00 2001 From: Torrie Fischer Date: Sun, 19 Feb 2023 18:43:26 +0100 Subject: [PATCH] platform: arduino: mqtt: allow configuration through json --- src/platform/arduino/MQTTTelemetry.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/platform/arduino/MQTTTelemetry.cpp b/src/platform/arduino/MQTTTelemetry.cpp index b0acfce..8722143 100644 --- a/src/platform/arduino/MQTTTelemetry.cpp +++ b/src/platform/arduino/MQTTTelemetry.cpp @@ -108,6 +108,7 @@ MQTTTelemetry::MQTTTelemetry() : BufferedInputSource("MQTT"), m_logPrinter(this) { m_debugTopic = String("renderbug/") + Platform::deviceID(); + memset(m_hostBuf, 0, sizeof(m_hostBuf)); } void @@ -201,9 +202,15 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt) void MQTTTelemetry::handleConfigChange(const InputEvent& event) { - const JsonObject& obj = event.as(); - strncpy(m_hostBuf, obj["mqtt.ip"].as().c_str(), sizeof(m_hostBuf)); - m_mqtt.disconnect(); + Log.notice("Config change in mqtt"); + const JsonObject* obj = event.as(); + if (obj->containsKey("mqtt.ip")) { + const char* newIP = (*obj)["mqtt.ip"].as().c_str(); + Log.notice("ip: %s", newIP); + strncpy(m_hostBuf, newIP, sizeof(m_hostBuf)); + m_hostBuf[sizeof(m_hostBuf)-1] = 0; + m_mqtt.disconnect(); + } } void @@ -217,6 +224,7 @@ MQTTTelemetry::loop() void MQTTTelemetry::handleEvent(const InputEvent& evt) { + BufferedInputSource::handleEvent(evt); OnlineTaskMixin::handleEvent(evt); ConfigTaskMixin::handleEvent(evt); } @@ -225,7 +233,11 @@ void MQTTTelemetry::onOnline() { m_needHeartbeat = true; - m_mqtt.setServer(m_hostBuf, 1883); + if (strlen(m_hostBuf) > 0) { + m_mqtt.setServer(m_hostBuf, 1883); + } else { + Log.warning("mqtt.ip is not configured, no connection possible"); + } m_mqtt.setBufferSize(1024); m_mqtt.setCallback(&MQTTTelemetry::s_callback); }