Browse Source

Fixed #14 MQTT Connection with Username an Password not working

fastled
Xose Pérez 8 years ago
parent
commit
adb290e9dd
1 changed files with 8 additions and 4 deletions
  1. +8
    -4
      code/src/mqtt.ino

+ 8
- 4
code/src/mqtt.ino View File

@ -57,6 +57,7 @@ void _mqttOnConnect(bool sessionPresent) {
// Build MQTT topics // Build MQTT topics
buildTopics(); buildTopics();
mqtt.setWill((mqttTopic + MQTT_HEARTBEAT_TOPIC).c_str(), MQTT_QOS, MQTT_RETAIN, (char *) "0");
// Say hello and report our IP and VERSION // Say hello and report our IP and VERSION
mqttSend((char *) MQTT_IP_TOPIC, (char *) getIP().c_str()); mqttSend((char *) MQTT_IP_TOPIC, (char *) getIP().c_str());
@ -112,12 +113,15 @@ void mqttConnect() {
mqtt mqtt
.setKeepAlive(MQTT_KEEPALIVE) .setKeepAlive(MQTT_KEEPALIVE)
.setCleanSession(false) .setCleanSession(false)
//.setWill("topic/online", 2, true, "no")
.setClientId(getSetting("hostname", HOSTNAME).c_str()); .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 { } else {
DEBUG_MSG(" anonymously\n"); DEBUG_MSG(" anonymously\n");
} }


Loading…
Cancel
Save