diff --git a/MQTT.py b/MQTT.py index 17fda4b..e6c6cf2 100755 --- a/MQTT.py +++ b/MQTT.py @@ -71,19 +71,18 @@ def exec_shutdown(): pass -if __name__ == '__main__': - client = mqtt.Client(client_id=device_name) - client.username_pw_set("demo", "123456") - # Specify callback function - client.on_connect = on_connect - # 尝试连接 MQTT 服务器 - while True: - try: - # ceshi-mqtt.lihaink.cn - client.connect('192.168.1.27', 1883) - client.loop_forever() - except Exception as e: - print("Connection failed:", e) - time.sleep(10) - print("正在尝试重连") +client = mqtt.Client(client_id=device_name) +client.username_pw_set("demo", "123456") +# Specify callback function +client.on_connect = on_connect +# 尝试连接 MQTT 服务器 +while True: + try: + # ceshi-mqtt.lihaink.cn + client.connect('192.168.1.27', 1883) + client.loop_forever() + except Exception as e: + print("Connection failed:", e) + time.sleep(10) + print("正在尝试重连") diff --git a/data_upload.py b/data_upload.py index 94c940e..71b421b 100755 --- a/data_upload.py +++ b/data_upload.py @@ -19,26 +19,25 @@ def on_disconnect(client, userdata, rc): print("失败,执行本地存储") # subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/stop_data_upload.sh'], shell=True) - times = 3 -if __name__ == '__main__': - client = mqtt.Client(client_id=device_name) - client.username_pw_set("demo", "123456") - # Specify callback function - client.on_connect = on_connect - client.on_disconnect = on_disconnect - # 尝试连接 MQTT 服务器 - while True: - try: - # ceshi-mqtt.lihaink.cn - client.connect('192.168.1.27', 1883) - client.loop_forever() - except Exception as e: - print("Connection failed:", e) - if times == 0: - # 执行本地存储 - on_disconnect(None, None, None) - print("正在尝试重连") - time.sleep(5) - times -= 1 +client = mqtt.Client(client_id=device_name) +client.username_pw_set("demo", "123456") +# Specify callback function +client.on_connect = on_connect +client.on_disconnect = on_disconnect + +# 尝试连接 MQTT 服务器 +while True: + try: + # ceshi-mqtt.lihaink.cn + client.connect('192.168.1.27', 1883) + client.loop_forever() + except Exception as e: + print("Connection failed:", e) + if times == 0: + # 执行本地存储 + on_disconnect(None, None, None) + print("正在尝试重连") + time.sleep(5) + times -= 1