From aebfe13f3dfb83ad06793e2e53cfe44d9cb7f3c4 Mon Sep 17 00:00:00 2001 From: Ryan Jacobs <ryan@ryanjjacobs.com> Date: Wed, 5 Feb 2020 19:27:25 -0700 Subject: [PATCH] fix for not connecting to anon hass.io MQTT broker --- Software/EmonESP/src/mqtt.cpp | 25 +++++++++++++++++++------ Software/EmonESP/src_solar/mqtt.cpp | 25 ++++++++++++++++++------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Software/EmonESP/src/mqtt.cpp b/Software/EmonESP/src/mqtt.cpp index c647da5..14cad96 100644 --- a/Software/EmonESP/src/mqtt.cpp +++ b/Software/EmonESP/src/mqtt.cpp @@ -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); } diff --git a/Software/EmonESP/src_solar/mqtt.cpp b/Software/EmonESP/src_solar/mqtt.cpp index 058c7e6..abcf899 100644 --- a/Software/EmonESP/src_solar/mqtt.cpp +++ b/Software/EmonESP/src_solar/mqtt.cpp @@ -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); }