From 1c5292ac864661054c3a6c7a12e3f88941003e99 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sun, 20 Nov 2022 12:29:10 -0600 Subject: [PATCH] Init default mqtt configurations --- src/mesh/NodeDB.cpp | 5 +++++ src/mesh/NodeDB.h | 4 ++++ src/mqtt/MQTT.cpp | 25 ++++++++----------------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 8a7ee6086..9f092b346 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -204,6 +204,7 @@ void NodeDB::installDefaultModuleConfig() { DEBUG_MSG("Installing default ModuleConfig\n"); memset(&moduleConfig, 0, sizeof(ModuleConfig)); + moduleConfig.version = DEVICESTATE_CUR_VER; moduleConfig.has_mqtt = true; moduleConfig.has_range_test = true; @@ -213,6 +214,10 @@ void NodeDB::installDefaultModuleConfig() moduleConfig.has_external_notification = true; moduleConfig.has_canned_message = true; + strncpy(moduleConfig.mqtt.address, default_mqtt_address, sizeof(default_mqtt_address)); + strncpy(moduleConfig.mqtt.username, default_mqtt_username, sizeof(default_mqtt_username)); + strncpy(moduleConfig.mqtt.password, default_mqtt_password, sizeof(default_mqtt_password)); + initModuleConfigIntervals(); } diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index dcd518191..545c288ab 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -194,6 +194,10 @@ extern NodeDB nodeDB; #define default_min_wake_secs 10 #define default_screen_on_secs 60 * 10 +#define default_mqtt_address "mqtt.meshtastic.org" +#define default_mqtt_username "meshdev" +#define default_mqtt_password "large4cats" + inline uint32_t getConfiguredOrDefaultMs(uint32_t configuredInterval) { if (configuredInterval > 0) return configuredInterval * 1000; diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 194683939..f7193eb32 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -118,24 +118,16 @@ bool MQTT::connected() void MQTT::reconnect() { if (wantsLink()) { - const char *serverAddr = "mqtt.meshtastic.org"; // default hostname - int serverPort = 1883; // default server port - const char *mqttUsername = "meshdev"; - const char *mqttPassword = "large4cats"; + // Defaults + int serverPort = 1883; + const char *serverAddr = default_mqtt_address; + const char *mqttUsername = default_mqtt_username; + const char *mqttPassword = default_mqtt_password; if (*moduleConfig.mqtt.address) { - serverAddr = moduleConfig.mqtt.address; // Override the default - mqttUsername = - moduleConfig.mqtt.username; // do not use the hardcoded credentials for a custom mqtt server + serverAddr = moduleConfig.mqtt.address; + mqttUsername = moduleConfig.mqtt.username; mqttPassword = moduleConfig.mqtt.password; - } else { - // we are using the default server. Use the hardcoded credentials by default, but allow overriding - if (*moduleConfig.mqtt.username && moduleConfig.mqtt.username[0] != '\0') { - mqttUsername = moduleConfig.mqtt.username; - } - if (*moduleConfig.mqtt.password && moduleConfig.mqtt.password[0] != '\0') { - mqttPassword = moduleConfig.mqtt.password; - } } String server = String(serverAddr); @@ -148,8 +140,7 @@ void MQTT::reconnect() } pubSub.setServer(serverAddr, serverPort); - DEBUG_MSG("Connecting to MQTT server %s, port: %d, username: %s, password: %s\n", serverAddr, serverPort, mqttUsername, - mqttPassword); + DEBUG_MSG("Connecting to MQTT server %s, port: %d, username: %s, password: %s\n", serverAddr, serverPort, mqttUsername, mqttPassword); auto myStatus = (statusTopic + owner.id); bool connected = pubSub.connect(owner.id, mqttUsername, mqttPassword, myStatus.c_str(), 1, true, "offline"); if (connected) {