PicoW_HomeAssistant_Starter icon indicating copy to clipboard operation
PicoW_HomeAssistant_Starter copied to clipboard

Missing MQTT_LOGIN & MQTT_PASSWORD still indicates connected to MQTT broker.

Open JacobChrist opened this issue 1 year ago • 6 comments

If MQTT_LOGIN & MQTT_PASSWORD left blank:

#define MQTT_LOGIN ""
#define MQTT_PASSWORD ""

Application still indicates that it connects to the MQTT Broker but Controls do not work.

--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
21:39:44.381 > Connected to
21:39:44.381 > Tajin
21:39:44.382 > Connecting to MQTT
21:39:44.383 > Connected to MQTT broker

If I add the LOGIN and PASSWORD everything works fine.

JacobChrist avatar May 21 '23 04:05 JacobChrist

Interesting. I'll have a look at that, thanks for the report.

daniloc avatar May 21 '23 18:05 daniloc

I had a similar issue, I fudged the IP address for MQTT, but it still said "Connected to MQTT broker" in the serial monitor.

Looking at the code, I would blame the upstram library, because it only returns false if it's missing a unique ID or if the connection is already initialized.

timmillwood avatar Aug 25 '23 11:08 timmillwood

@timmillwood thanks for digging into this, this JUST bit me again a few weeks back

Any chance I could trouble you for a link to the offending lines in arduino-home-assistant? AHA's author has gone quiet and I've been working on a fork to address my own issues with it.

daniloc avatar Aug 25 '23 15:08 daniloc

@daniloc https://github.com/dawidchyrzynski/arduino-home-assistant/blob/main/src/HAMqtt.cpp#L89-L97

timmillwood avatar Aug 25 '23 15:08 timmillwood

If you follow the call path from this point in HAIntegration.cpp:

    if (mqtt.begin(MQTT_BROKER, MQTT_LOGIN, MQTT_PASSWORD) == true) {
        Serial.print("Connected to MQTT broker\n");
    } else {
        Serial.print("Could not connect to MQTT broker\n");
    }

calls:

bool HAMqtt::begin(const IPAddress serverIp, const char* username, const char* password)

which calls:

bool HAMqtt::begin(const char* serverHostname, const uint16_t serverPort, const char* username, const char* password)

which sets a call back function:

_mqtt->setCallback(onMessageReceived);

Attempts to connecting to the mqtt server only happens in the

void HAMqtt::connectToServer()

function which is called in the HAMqtt:loop() function.

Probably would would be better is to interpret the result from mqtt.begin not as a successful connection but a successful instantiation of the object.

So maybe change this line:

Serial.print("Could not connect to MQTT broker");

to this:

Serial.print("Could (re)instantiate MQTT object");

The (re) is because it looks like you would get a false if you tried to call mqtt.begin() twice as well.

JacobChrist avatar Nov 04 '23 16:11 JacobChrist

I found this note in the ArduinoHA documentation this morning that confirms intent.

  • Note
  • Connection to the MQTT broker is established asynchronously. The HAMqtt::begin method just sets the parameters of the connection. The connection attempt is made during the loop cycle.

JacobChrist avatar Nov 05 '23 15:11 JacobChrist