MQTT Client 一直掉线
MQTT Client keeps on diconnecting
我有多个 python 文件。一个文件包含与 MQTT 相关的所有代码并具有一些函数,而另一个文件导入 MQTT 文件并在事件发生时调用函数。 MQTT 文件仅发布一些 QoS 0 和一些 QoS 1 的消息,并连接到安装在本地计算机中的 mosquitto 代理。 MQTT代码如下
import paho.mqtt.client as mqtt
from threading import current_thread
import datetime
import cv2 as cv2
import time
# Define Variables
MQTT_HOST = "127.0.0.1"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 60
def send_something():
try:
mqttc.publish("topic", "hello", 1, False) # QoS =1 Retain = False
except Exception as e:
print(str(e))
def send_something_else():
mqttc.publish("anothertopic", CombinedByteArr, 0, False)
def on_connect(mqttc, userdata, flags, rc):
print("[INFO] : MQTT : Connection returned result: " + mqtt.connack_string(rc))
if(rc == 0):
print("[INFO] : MQTT : Connection Successful")
else:
print(rc)
def on_disconnect(mqttc, userdata, rc):
if rc != 0:
print(" Unexpected disconnection")
while(True):
try:
print("Trying to Reconnect")
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
break
except:
print("Error in Retrying to Connect with Broker")
continue
# Initiate MQTT Client
ThreadID = str(current_thread().ident)
mqttc = mqtt.Client(client_id= ThreadID, clean_session=False)
mqttc.on_publish = on_publish
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.on_disconnect = on_disconnect
while(True):
try:
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
print("[INFO] : MQTT : MQTT Connect Complete")
break
except:
print("ERROR Occurred")
mqttc.loop_start() # Start A Thread
我 运行 多个 python 文件,这意味着 MQTT 的多个副本是 运行。在 mosquitto 日志中,它始终显示客户端不断断开连接然后重新连接。一段时间后,我在蚊子日志中不断地得到这些:-
1518788230: New client connected from 127.0.0.1 as MQTT2225 (c0, k60).
1518788230: Sending CONNACK to MQTT2225 (0, 0)
1518788230: Socket error on client MQTT2225, disconnecting.
您需要将 mqttc.loop_start()
更改为 mqttc.loop_forever()
以阻止程序在启动后台线程后退出。
编辑:在进一步思考之后,问题不在于循环,而是客户端 ID
假设您总是实例化您从主线程提供的代码中描述的对象的实例,那么线程 ID 将始终相同,这意味着代理将踢除最后一个实例之外的所有实例进行连接。
并且由于您有逻辑在一个紧密的循环中重新连接,一旦您拥有多个 MQTT 客户端,它们将始终相互启动
我有多个 python 文件。一个文件包含与 MQTT 相关的所有代码并具有一些函数,而另一个文件导入 MQTT 文件并在事件发生时调用函数。 MQTT 文件仅发布一些 QoS 0 和一些 QoS 1 的消息,并连接到安装在本地计算机中的 mosquitto 代理。 MQTT代码如下
import paho.mqtt.client as mqtt
from threading import current_thread
import datetime
import cv2 as cv2
import time
# Define Variables
MQTT_HOST = "127.0.0.1"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 60
def send_something():
try:
mqttc.publish("topic", "hello", 1, False) # QoS =1 Retain = False
except Exception as e:
print(str(e))
def send_something_else():
mqttc.publish("anothertopic", CombinedByteArr, 0, False)
def on_connect(mqttc, userdata, flags, rc):
print("[INFO] : MQTT : Connection returned result: " + mqtt.connack_string(rc))
if(rc == 0):
print("[INFO] : MQTT : Connection Successful")
else:
print(rc)
def on_disconnect(mqttc, userdata, rc):
if rc != 0:
print(" Unexpected disconnection")
while(True):
try:
print("Trying to Reconnect")
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
break
except:
print("Error in Retrying to Connect with Broker")
continue
# Initiate MQTT Client
ThreadID = str(current_thread().ident)
mqttc = mqtt.Client(client_id= ThreadID, clean_session=False)
mqttc.on_publish = on_publish
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.on_disconnect = on_disconnect
while(True):
try:
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
print("[INFO] : MQTT : MQTT Connect Complete")
break
except:
print("ERROR Occurred")
mqttc.loop_start() # Start A Thread
我 运行 多个 python 文件,这意味着 MQTT 的多个副本是 运行。在 mosquitto 日志中,它始终显示客户端不断断开连接然后重新连接。一段时间后,我在蚊子日志中不断地得到这些:-
1518788230: New client connected from 127.0.0.1 as MQTT2225 (c0, k60).
1518788230: Sending CONNACK to MQTT2225 (0, 0)
1518788230: Socket error on client MQTT2225, disconnecting.
您需要将 mqttc.loop_start()
更改为 mqttc.loop_forever()
以阻止程序在启动后台线程后退出。
编辑:在进一步思考之后,问题不在于循环,而是客户端 ID
假设您总是实例化您从主线程提供的代码中描述的对象的实例,那么线程 ID 将始终相同,这意味着代理将踢除最后一个实例之外的所有实例进行连接。
并且由于您有逻辑在一个紧密的循环中重新连接,一旦您拥有多个 MQTT 客户端,它们将始终相互启动