简体   繁体   English

无法连接到MQTT经纪人

[英]Can't connect to mqtt broker

I installed MQTT broker Mosquitto on my pi and are having some problems getting it to work with boxes in my network. 我在pi上安装了MQTT经纪人Mosquitto,在使其与网络中的框配合使用时遇到一些问题。 Locally, if I putty in to the RPi running the Mosquitto MQTT broker everything is OK. 在本地,如果我对运行Mosquitto MQTT代理的RPi进行腻子处理,一切正常。 I can use the client commands ( mosquitto_sub, mosquitto_pub ) to subscribe and publish to topics, no problem. 我可以使用客户端命令( mosquitto_sub, mosquitto_pub )来订阅和发布主题,这没有问题。 BUT, if I try to connect from another box, Win2k12 server with a python script it states it cant connect. 但是,如果我尝试从另一个盒子进行连接,则使用Python脚本的Win2k12服务器会指出它无法连接。

  • I've tried turning the firewall off in my router 我试图关闭路由器中的防火墙
  • I've tried turning the firewall off on my Win2k12 server 我试图关闭Win2k12服务器上的防火墙
  • I've added TCP 1883 to allowed ports outbound from my Win2k12 server 我已将TCP 1883添加到允许Win2k12服务器出站的端口

The Python script: Python脚本:

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    client.publish("test_mqtt", "test")
    client.subscribe("test")

def on_disconnect(client, userdata, rc):
    print("Disconnect, reason: " + str(rc))
    print("Disconnect, reason: " + str(client))

client = mqtt.Client("testclient")
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.connect("192.168.1.20", 1883, 60)
client.loop_forever()

The output here is 这里的输出是

Disconnect, reason: <paho.mqtt.client.Client object at 0x01F41EF0>
Disconnect, reason: 1

I've tried to have a look at the documentation but it only mentioned the flags, not defining what they are. 我试图看一下文档,但是只提到了标志,没有定义它们是什么。

The raspberry pi that is running Mosquitto is also running Node-red. 运行Mosquitto的树莓派也正在运行Node-red。 It has no problem connecting to the MQTT broker (both of them are running on the same rpi) 连接到MQTT代理没有问题(它们都在同一rpi上运行)

Has enyone set up MQTT on Raspberry Pi and got it to work with other devices? enyone是否在Raspberry Pi上设置了MQTT并将其与其他设备一起使用? I want it to work with a NodeMCU thingy, but when I had problems I started working on a python script to further debug the problem. 我希望它可以与NodeMCU一起使用,但是当我遇到问题时,我开始研究python脚本以进一步调试问题。

You can force the paho client to use the 3.1 level of the protocol by adding an option to the mqtt.Client constuctor: 您可以通过向mqtt.Client构造函数添加一个选项来强制Paho客户端使用协议的3.1级别:

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    client.publish("test_mqtt", "test")
    client.subscribe("test")

def on_disconnect(client, userdata, rc):
    print("Disconnect, reason: " + str(rc))
    print("Disconnect, reason: " + str(client))

client = mqtt.Client("testclient", protocol=mqtt.MQTTv31)
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.connect("192.168.1.20", 1883, 60)
client.loop_forever()

First you have to make sure that you can connect to the Raspberry Pi. 首先,您必须确保可以连接到Raspberry Pi。 You can try using libraries other than Paho or using one MQTT client: http://www.hivemq.com/blog/seven-best-mqtt-client-tools 您可以尝试使用Paho以外的库或使用一个MQTT客户端: http ://www.hivemq.com/blog/seven-best-mqtt-client-tools

The other thing you can try is setting both client and broker to use port 80 to see if they can communicate through that port. 您可以尝试的另一件事是将客户端和代理都设置为使用端口80,以查看它们是否可以通过该端口进行通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM