我安装了Home Assistant,我有一些ESP模块可以连接到wi-fi和MQTT服务器(它在端口1883上嵌入HA) . 这里有一些来自configuration.yaml的代码:

mqtt:
    username: "homeassistant"
    password: ""
light:
  - platform: mqtt_json
    name: mqtt_json_light_2
    state_topic: "home/rgb1"
    command_topic: "home/rgb1/set"
    brightness: true
    rgb: true
    effect: true
    effect_list: [colorfade_slow, colorfade_fast, flash]
    optimistic: false
    qos: 0

一切正常,用户名和密码连接到MQTT(192.168.43.184:1883)就可以了 .

之后,我使用paho-mqtt.js在此服务器上创建测试页面 . 我可以用http://192.168.43.184:8123/local/index.html运行这个页面 . 所以...相同的服务器但不是https,所以mb这是该页面上没有连接的主要原因 .

简单的测试代码:

<!DOCTYPE html>
    <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
      <script src="js/paho-mqtt.js" type="text/javascript"></script>
      <script type="text/javascript">
        //sample HTML/JS script that will publish/subscribe to topics in the Google Chrome Console
        //by Matthew Bordignon @bordignon on twitter.
        var wsbroker = "127.0.0.1";  //mqtt websocket enabled brokers
        var wsport = 1883; // or for above
        var client = new Paho.MQTT.Client(wsbroker, wsport,
            "myclientid_" + parseInt(Math.random() * 100, 10));
        client.onConnectionLost = function (responseObject) {
          console.log("connection lost: " + responseObject.errorMessage);
        };
        client.onMessageArrived = function (message) {
          console.log(message.destinationName, ' -- ', message.payloadString);
        };
        var options = {
          timeout: 3,
          userName: "homeassistant",
          password: "",
          onSuccess: function () {
            console.log("mqtt connected");
          },
          onFailure: function (message) {
            console.log("Connection failed: " + message.errorMessage);
          }
        };
      function init() {
          client.connect(options);
      }
        </script>
      </head>
      <body onload="init();">
      </body>
    </html>

页面正在HA服务器上运行,因此地址可以是localhost或127.0.0.1或192.168.43.184 - 这不起作用,我只有这个:

连接失败:AMQJSC0001E连接超时 .

但是我可以用我的ESP / rasberi等连接结束 . 我甚至可以用http://192.168.43.184:8123/dev-mqtt发送消息 . 我的js不是什么东西?