Connection to Homeassistant MQTT Broker refused

Posted on
  • I've setup an mosquitto mqtt broker on my homeassist. I now want to have my esp32 connect to it and afterwards control a lamp over BLE.

    I'm currently already stuck trying to connect to my homeassistant mqtt broker. I've validated the broker is working with mqtt.fx by subscribing to the same ip/topic with the same credentials. When I now try to subscribe to the same broker I always get the following error:

    Connection refused, unknown return code: NaN.
    

    This is how my code looks like:

    var wifi = require('Wifi');
    
    var ssid = 'ssidname';
    var password = 'mypassword';
    var port = 80;
    
    var mqttServer = "192.168.86.42";
    var mqttConfig = {
        client_id : getSerial(),
        username: "mqtt-user",
        password: "my-mqtt-password",
        keep_alive: 60,
        port: 1883,
    };
    
    var topic = "/lrcontroller/";
    var message = "hello, world";
    
    var mqtt = require("MQTT").create(mqttServer, mqttConfig /*optional*/);
    
    function mqttConnect() {
    
      console.log("connecting mqtt");
    
      mqtt.on('error', (error) => {
        console.log("mqtt error: "+error);
      });
    
      mqtt.on('connected', function() {
        console.log("MQTT connected");
        setTimeout(function() {
          mqtt.subscribe(topic+"#");
        }, 1000);
      });
    
      mqtt.on('disconnected', function() {
        console.log("MQTT disconnected... reconnecting.");
        setTimeout(function() {
          mqtt.connect();
        }, 1000);
      });
    
      mqtt.on('publish', function (pub) {
        console.log("topic: "+pub.topic);
        console.log("message: "+pub.message);
      });
    
      mqtt.connect();
    
    }
    
    wifi.connect(ssid, {password: password}, function(error) {
      if(error){
        console.log("wifi errors: "+error);
      }
    });
    
    wifi.on('connected', (info) => {
      console.log("wifi connected to " + info.gw + " with the ip: " + info.ip + "! ");
      mqttConnect();
    });
    
    wifi.on('disconnected', (error) => {
      console.log("wifi disconnected!");
      setTimeout(() => {
        console.log("wifi reconnecting ...");
        wifi.connect(ssid, {password: password}, function(error) {
          if(error){
            console.log("wifi errors: "+error);
          }
        });
      }, 1000);
    });
    

    I am wondering if I am missing something here?

    Also when I flash to my esp32 with the Espruino IDE, the console logs are not shown because wifi seems to still be connected after te restart. So to validate my code I always call wifi.disconnect();.

    Would really appreciate any help!

  • Did you validate while running on the same computer?

    I know on Raspberry Pi for the past few years mosquitto has been set up so that it'll only accept local connections to itself by default, and you have to manually configure it to allow connections from outside

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Connection to Homeassistant MQTT Broker refused

Posted by Avatar for GhosTz @GhosTz

Actions