Skip to content

Commit

Permalink
Merge pull request #26 from djryanj/master
Browse files Browse the repository at this point in the history
Fix for MQTT brokers used by hass.io not connecting when anonymous
  • Loading branch information
CircuitSetup authored Feb 21, 2020
2 parents 37019fe + aebfe13 commit 6dec8bd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
25 changes: 19 additions & 6 deletions Software/EmonESP/src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,26 @@ boolean mqtt_connect()
String strID = String(ESP.getChipId());
#endif

if (mqttclient.connect(strID.c_str(), mqtt_user.c_str(), mqtt_pass.c_str())) { // Attempt to connect
DBUGS.println("MQTT connected");
mqttclient.publish(mqtt_topic.c_str(), "connected"); // Once connected, publish an announcement..

if (mqtt_user.length() == 0) {
if (mqttclient.connect(strID.c_str())) { // Attempt to connect
DBUGS.println("MQTT connected");
mqttclient.publish(mqtt_topic.c_str(), "connected"); // Once connected, publish an announcement..
} else {
DBUGS.print("MQTT failed: ");
DBUGS.println(mqttclient.state());
return (0);
}

} else {
DBUGS.print("MQTT failed: ");
DBUGS.println(mqttclient.state());
return (0);
if (mqttclient.connect(strID.c_str(), mqtt_user.c_str(), mqtt_pass.c_str())) { // Attempt to connect
DBUGS.println("MQTT connected");
mqttclient.publish(mqtt_topic.c_str(), "connected"); // Once connected, publish an announcement..
} else {
DBUGS.print("MQTT failed: ");
DBUGS.println(mqttclient.state());
return (0);
}
}
return (1);
}
Expand Down
25 changes: 18 additions & 7 deletions Software/EmonESP/src_solar/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,25 @@ boolean mqtt_connect()
#else
String strID = String(ESP.getChipId());
#endif

if (mqttclient.connect(strID.c_str(), mqtt_user.c_str(), mqtt_pass.c_str())) { // Attempt to connect
DBUGS.println("MQTT connected");
mqttclient.publish(mqtt_topic.c_str(), "connected"); // Once connected, publish an announcement..
if (mqtt_user.length() == 0) {
if (mqttclient.connect(strID.c_str())) { // Attempt to connect
DBUGS.println("MQTT connected");
mqttclient.publish(mqtt_topic.c_str(), "connected"); // Once connected, publish an announcement..
} else {
DBUGS.print("MQTT failed: ");
DBUGS.println(mqttclient.state());
return (0);
}

} else {
DBUGS.print("MQTT failed: ");
DBUGS.println(mqttclient.state());
return (0);
if (mqttclient.connect(strID.c_str(), mqtt_user.c_str(), mqtt_pass.c_str())) { // Attempt to connect
DBUGS.println("MQTT connected");
mqttclient.publish(mqtt_topic.c_str(), "connected"); // Once connected, publish an announcement..
} else {
DBUGS.print("MQTT failed: ");
DBUGS.println(mqttclient.state());
return (0);
}
}
return (1);
}
Expand Down

0 comments on commit 6dec8bd

Please sign in to comment.