From 7a67f81e401ab533ae0a0087e16dadd7652e6184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sat, 31 Dec 2016 04:52:14 +0100 Subject: [PATCH] #14 More reliable MQTT connection --- code/espurna/mqtt.ino | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 6e5a4fb7..8b9dc84a 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -134,31 +134,20 @@ void mqttConnect() { if (!mqtt.connected()) { - String host = getSetting("mqttServer", MQTT_SERVER); - String port = getSetting("mqttPort", MQTT_PORT); - String user = getSetting("mqttUser"); - String pass = getSetting("mqttPassword"); - - if (host.length() == 0) return; - - DEBUG_MSG("[MQTT] Connecting to broker at %s", (char *) host.c_str()); - - mqtt.setServer(host.c_str(), port.toInt()); - mqtt - .setKeepAlive(MQTT_KEEPALIVE) - .setCleanSession(false); - - if ((user != "") && (pass != "")) { - DEBUG_MSG(" as user '%s'.\n", (char *) user.c_str()); - char username[user.length()+1]; - user.toCharArray(username, user.length()+1); - char password[pass.length()+1]; - pass.toCharArray(password, pass.length()+1); - mqtt.setCredentials(username, password); - } else { - DEBUG_MSG(" anonymously\n"); + char * host = strdup(getSetting("mqttServer", MQTT_SERVER).c_str()); + if (strlen(host) == 0) return; + unsigned int port = getSetting("mqttPort", MQTT_PORT).toInt(); + char * user = strdup(getSetting("mqttUser").c_str()); + char * pass = strdup(getSetting("mqttPassword").c_str()); + + DEBUG_MSG("[MQTT] Connecting to broker at %s", host); + mqtt.setServer(host, port); + mqtt.setKeepAlive(MQTT_KEEPALIVE).setCleanSession(false); + if ((strlen(user) > 0) && (strlen(pass) > 0)) { + DEBUG_MSG(" as user '%s'.", user); + mqtt.setCredentials(user, pass); } - + DEBUG_MSG("\n"); mqtt.connect(); }