From adb290e9ddcecaafa3155f170ab8227bd1e52d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 23 Dec 2016 12:39:25 +0100 Subject: [PATCH] Fixed #14 MQTT Connection with Username an Password not working --- code/src/mqtt.ino | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/code/src/mqtt.ino b/code/src/mqtt.ino index f1507512..3e41e48d 100644 --- a/code/src/mqtt.ino +++ b/code/src/mqtt.ino @@ -57,6 +57,7 @@ void _mqttOnConnect(bool sessionPresent) { // Build MQTT topics buildTopics(); + mqtt.setWill((mqttTopic + MQTT_HEARTBEAT_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, (char *) "0"); // Say hello and report our IP and VERSION mqttSend((char *) MQTT_IP_TOPIC, (char *) getIP().c_str()); @@ -112,12 +113,15 @@ void mqttConnect() { mqtt .setKeepAlive(MQTT_KEEPALIVE) .setCleanSession(false) - //.setWill("topic/online", 2, true, "no") .setClientId(getSetting("hostname", HOSTNAME).c_str()); - if ((user != "") & (pass != "")) { - DEBUG_MSG(" as user %s.\n", (char *) user.c_str()); - mqtt.setCredentials(user.c_str(), pass.c_str()); + 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"); }