Nodemcu DHT 11 - mqtt problem

I do have one other question…

How do you handle the login process for your MQTT broker?

I keep getting errors on connecting to the broker then the nodeMCU resets and it does it continuously. I can only think that it’s because there is no authentication credentials being supplied.

this is the error from serial monitor:

WiFi connected
IP address: 
192.168.1.145
Connecting to 192.168.1.125 as Arduino-sensor1-ESP
MQTT connect failed
Will reset and try again...
Abort called

ctx: cont 
sp: 3ffef5f0 end: 3ffef7f0 offset: 01b0

>>>stack>>>
3ffef7a0:  3ffe8458 3ffee688 3ffee794 40201f53  
3ffef7b0:  3ffe8918 9101a8c0 feefeffe feefeffe  
3ffef7c0:  feefeffe feefeffe feefeffe 3ffee7c0  
3ffef7d0:  3fffdad0 00000000 3ffee7b8 40204268  
3ffef7e0:  feefeffe feefeffe 3ffee7d0 40100718  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v6000001c
~ld
⸮

To be honest, I don’t. My MQTT broker doesn’t use any authentication on my internal network. I opted not to. After all, at least in my situation, the data being published via MQTT isn’t mission critical, etc. In my opinion, it was well worth it. Having auth on the broker would just cause me more headaches.

But, understanding that everyone doesn’t share my viewpoint, I’ve tweaked the code a bit to allow for authentication. In the code posted on my site, find this block:

  if (client.connect((char *)clientName.c_str())) {
    Serial.println("Connected to MQTT broker");
  } else {
    Serial.println("MQTT connect failed");
    Serial.println("Will reset and try again...");
    abort();
  }

Then replace it with this block:

  if (client.connect((char *)clientName.c_str(), "user", "pass")) {
    Serial.println("Connected to MQTT broker");
  } else {
    Serial.println("MQTT connect failed");
    Serial.println("Will reset and try again...");
    abort();
  }

The syntax for this looks ok to me, but it is UNTESTED! Please let me know if it works for you!

I assume you’re using PubSubClient (haven’t looked directly at the code sorry, just following the discussion); if so, you can add to the else:

else {
      Serial.print("MQTT connect failed, rc=");
      Serial.println(client.state());
}

…client.state() will give you the return code of the current state, which will help users to debug the problem: see https://pubsubclient.knolleary.net/api.html#state.

-4 : MQTT_CONNECTION_TIMEOUT - the server didn’t respond within the keepalive time
-3 : MQTT_CONNECTION_LOST - the network connection was broken
-2 : MQTT_CONNECT_FAILED - the network connection failed
-1 : MQTT_DISCONNECTED - the client is disconnected cleanly
0 : MQTT_CONNECTED - the client is connected
1 : MQTT_CONNECT_BAD_PROTOCOL - the server doesn’t support the requested version of MQTT
2 : MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier
3 : MQTT_CONNECT_UNAVAILABLE - the server was unable to accept the connection
4 : MQTT_CONNECT_BAD_CREDENTIALS - the username/password were rejected
5 : MQTT_CONNECT_UNAUTHORIZED - the client was not authorized to connect