home-assistant / addons

:heavy_plus_sign: Docker add-ons for Home Assistant
https://home-assistant.io/hassio/
Apache License 2.0
1.51k stars 1.46k forks source link

Cannot connect NodeMcu to Mosquitto broker on Home Assistant. Same code connects to test.mosquitto.org just fine. #1009

Closed WongGendheng closed 4 years ago

WongGendheng commented 4 years ago

Hey there,

I searched far and wide for a solution to connect my NodeMcu to my Hass.io Mosquitto broker, yet I'm always receiving the "Connecting to MQTT...failed with state -2" error message. Connecting to test.mosquitto.org works just fine. I hope to gain new insights from this post; anything that could help me is highly appreciated.

My setup:

--Home Assistant-- I have a fresh install of Home Assistant 0.104.2 running on a Raspberry Pi 3B. I installed Home Assistant with the latest image I flashed with Etcher - nothing fancy. I installed the Mosquitto broker through the official add-on store. Apart from that I installed the Configurator and SSH server add-ons through the official store.

--NodeMcu-- I programmed my NodeMcu with version 1.8.9 of the Arduino IDE. It can connect to the test.mosquitto.org server without username and password. Which makes me confident that my code actually works.

My MQTT setup:

1) The MQTT config in the add-on is configured as follows:

{
  "logins": [
    {
      "username": "dustinmqtt",
      "password": "dustinmqtt"
    }
  ],
  "anonymous": false,
  "customize": {
    "active": false,
    "folder": "mosquitto"
  },
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem",
  "require_certificate": false
}

2) Ports are configured like this: ports

3) Here are the current logs I found in the add-on:

[00:14:06] INFO: Setup mosquitto configuration
[00:14:06] WARNING: SSL not enabled - No valid certs found!
[00:14:06] INFO: Found local users inside config
[00:14:07] INFO: Initialize Hass.io Add-on services
[00:14:07] INFO: Initialize Home Assistant discovery
[00:14:07] INFO: Start Mosquitto daemon
1579562047: mosquitto version 1.6.3 starting
1579562047: Config loaded from /etc/mosquitto.conf.
1579562047: Loading plugin: /usr/share/mosquitto/auth-plug.so
1579562047: |-- *** auth-plug: startup
1579562047:  ├── Username/password checking enabled.
1579562047:  ├── TLS-PSK checking enabled.
1579562047:  └── Extended authentication not enabled.
1579562047: Opening ipv4 listen socket on port 1883.
1579562047: Opening ipv6 listen socket on port 1883.
1579562047: Opening websockets listen socket on port 1884.
1579562047: Warning: Mosquitto should not be run as root/administrator.
1579562047: New connection from 172.30.32.1 on port 1883.
1579562048: Socket error on client <unknown>, disconnecting.
1579562048: New connection from 172.30.32.1 on port 1883.
[INFO] found homeassistant on local database
1579562049: New client connected from 172.30.32.1 as auto-93844951-7806-6148-9232-476E94FED042 (p2, c1, k60, u'homeassistant').
1579563847: Saving in-memory database to /data/mosquitto.db.

4) Under configuration --> user, I created the user "dustinmqtt" with "dustinmqtt" as password. It has administrator privileges.

5) Under configuration --> integrations, the "MQTT: Mosquitto broker" is listed .

6) My configuration.yaml is fresh and nothing has been added.

Testing MQTT broker succesful:

I have several reasons to assume my MQTT broker is working correctly: 1) Under developer tools --> MQTT, I can publish packages and listen to topics. 2) With MQTT.fx 1.7.1, I can connect to my MQTT broker through port 1883, with the IP of Home Assistant and the username and password "dustinmqtt". Messages I publish in MQTT.fx can be seen in the developer tools of Home Assistant and vice versa. 3) I can run the following commands and see the expected results:

mosquitto_sub -h 192.168.0.109 -d -p 1883 -u dustinmqtt -P dustinmqtt -t home/garden/fountain
mosquitto_pub -h 192.168.0.109 -d -p 1883 -u dustinmqtt -P dustinmqtt -t home/garden/fountain -m "OFF"

My code on the NodeMcu:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "routerSSID"; 
const char* password =  "routerPW";

const char* mqttServer = "192.168.0.106";
const int mqttPort = 1883;
const char* mqttUser = "dustinmqtt";
const char* mqttPassword = "dustinmqtt";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");

  client.setServer(mqttServer, mqttPort);

  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");

    if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
      Serial.println("connected");
      } else {
        Serial.print("failed with state ");
        Serial.print(client.state());
        delay(2000);
      }
  }

  client.publish("esp/test", "Hello from ESP8266");
  client.subscribe("esp/test");
}

void loop() {
  client.loop();
}

The code compiles perfectly and does connect to test.mosquitto.org. All I needed to do was to comment out the parts of the code that deal with mqttUser and mqttPassword. This is why I think the problem lies somewhere along the lines of "logging into my MQTT broker with username and password". What confuses me is that I can connect to my MQTT broker with my username and password just fine while using MQTT.fx.

My questions: 1) Is there anything I can do to troubleshoot? Im losing my mind here. 2) The username and password are passed to client.connect() as const char*. Could there be a problem with that in connection with using Mosquitto on Home Assistant? Though according to the pupsubclient documentation this is the correct way to do it: boolean connect (clientID, username, password) 3) Are there any known issues of pubsubclient not working with Mosquitto broker that I might be missing? 4) Is there anything I could add to my configuration.yaml to make this problem go away? I tried adding the following, but several sources said I do not actually need to add this. Either way, it didnt affect my situation:

# Example configuration.yaml entry
mqtt:
  broker: IP_ADDRESS_BROKER

Cheers Dustin

johanson commented 4 years ago

If you have already created the user you're trying to login with, there's no need for "logins": [] in your broker config. In fact, I believe it's a list of users that you want the add on to create for you. Try removing it, restart the broker and login again wirh your nodemcu to see if that helps.

WongGendheng commented 4 years ago

@johanson Thank you very much for bothering with my post. In fact, a user on Reddit spotted the mistake and - I feel ashamed just writing this - it was a wrong IP adress.

192.168.0.106 used to be my Home Assistant instance before I made a full whipe on the SDcard and reinstalled Home Assistant. While 192.168.0.106 was up, the code was not working properly, hence I focused on getting it to work. Somewhere around that time my code didn't work I made the clean install of Home Assistant. My mistake was to keep copying the header part of the code from the old sketch to the new one - for convenience.

Now, everything works as expected - except for my ego, which is a bit fragile at the moment from not spotting this easy mistake.

Closing now.

vanarebane commented 3 years ago

My problem was that the WiFi was not in the channel the ESP like. So changing the WiFi AP to channel 6 made it work right away. Thanks for the sample code