Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Mqtt侦听断开连接和自动重新连接_Python_Python 3.x_Mqtt - Fatal编程技术网

Python Mqtt侦听断开连接和自动重新连接

Python Mqtt侦听断开连接和自动重新连接,python,python-3.x,mqtt,Python,Python 3.x,Mqtt,我编写了一个脚本,从周一至周五上午8:30至下午19:00的某个物联网传感器获取实时数据,但问题是该脚本将停止,而不会每隔50-60分钟出现任何错误。因此,我添加了一个回调来监听断开连接并尝试自动重新连接,但它不起作用 import paho.mqtt.client as mqtt import time import datetime ms_topic = '' client_id = '' def stamp_to_time(time_stamp): #convert t

我编写了一个脚本,从周一至周五上午8:30至下午19:00的某个物联网传感器获取实时数据,但问题是该脚本将停止,而不会每隔50-60分钟出现任何错误。因此,我添加了一个回调来监听断开连接并尝试自动重新连接,但它不起作用

import paho.mqtt.client as mqtt
import time
import datetime


ms_topic = ''  
client_id = ''


def stamp_to_time(time_stamp):
    #convert timestamp to datetime
    local_time = time.localtime(int(time_stamp))
    mytime = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
    return datetime.datetime.strptime(mytime, "%Y-%m-%d %H:%M:%S")


# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe(ms_topic,2)


#The callback for when the client receives a disconnect response from the server
def on_disconnect(client, userdata, rc):
    current_time = stamp_to_time(time.time())
    if current_time.strftime("%H") != '19': #jump out the loop if it's 19:00 
        client.reconnect()


# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    payload = str(msg.payload, encoding = 'utf-8')          

    current_time = stamp_to_time(time.time())
    if current_time.strftime("%H") == '19': #jump out the loop if it's 19:00 
        client.disconnect()


def test():  
    client = mqtt.Client(client_id, clean_session = False)    
    client.username_pw_set("", "")  
    client.reconnect_delay_set(min_delay = 1, max_delay = 10000)
    client.on_connect = on_connect
    client.on_message = on_message
    client.on_disconnect = on_disconnect
    client.connect("www.zeta-alliance.com", 1883, 60)
    client.loop_forever()


if __name__ == '__main__':  
    test()

我的目标是保持它工作,并从上午8:30到下午19:00接收实时数据,可以监听断开连接并自动尝试重新连接。谁能告诉我这段代码有什么问题,以及如何解决这个问题?真的很感激

连接到mqtt代理时传递keepAliveInterval和timeout属性

%M
是分钟而不是小时…您已经编辑了问题,但如果分钟与小时确实解决了问题,则未包含任何更新。根据文档以及paho mqtt的代码,一个空字符串作为
client_id
clean_session=False
应该抛出一个
ValueError
。因此,要么你过于简化了你的代码,要么你遗漏了一些东西。您是否尝试过不使用
clean_session=False
?此外,应该通过调用
loop\u forever
自动处理重新连接,因此我想这是一个更深层次的问题,可能是链接断开了?答案需要包含更多的上下文。此外,这些值是可选的,将用默认值填充,因此这不是解决方案。