From 3e1c26f91e736fab5cda570ccb22be79a221df23 Mon Sep 17 00:00:00 2001 From: xyj <10908227994@qq.com> Date: Mon, 11 Dec 2023 14:42:49 +0800 Subject: [PATCH] update --- MQTT.py | 12 ++++++------ conf/main/config.conf | 2 +- data_upload.py | 3 ++- sensor_to_server.py | 3 ++- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/MQTT.py b/MQTT.py index eef0445..550b26a 100644 --- a/MQTT.py +++ b/MQTT.py @@ -1,3 +1,4 @@ +import datetime import time import paho.mqtt.client as mqtt @@ -7,8 +8,8 @@ from tool import * import configparser config = configparser.ConfigParser() -config.read('/home/pi/lot_manager/conf/main/config.conf') -# config.read('/home/lihai/pythonProjects/lot_manager/conf/main/config.conf') +# config.read('/home/pi/lot_manager/conf/main/config.conf') +config.read('/home/lihai/pythonProjects/lot_manager/conf/main/config.conf') def valid(msg, client): @@ -37,7 +38,7 @@ class MQTTClient: def on_connect(self, client, userdata, flags, rc): self.client.subscribe(self.topic) - self.client.publish('success', payload='成功订阅lot_mqtt,time=' + str(time.time()), qos=0) + self.client.publish('success', payload='成功订阅lot_mqtt,time=' + str(datetime.datetime.now()), qos=0) def on_message(self, client, userdata, msg): if not valid(msg, client): @@ -77,13 +78,12 @@ class MQTTClient: def start(self): self.client.username_pw_set(self.username, self.password) self.client.connect(self.broker, self.port) - print("连接成功") if __name__ == '__main__': - print(device_name) broker = config.get("broker", "host") - port = config.get("broker", "port") + # 这里必须是int类型 + port = config.getint("broker", "port") topic = config.get("topic", "name") username = config.get("security", "username") password = config.get("security", "password") diff --git a/conf/main/config.conf b/conf/main/config.conf index 9df52e9..3e7bb4d 100644 --- a/conf/main/config.conf +++ b/conf/main/config.conf @@ -1,5 +1,5 @@ [broker] -host=ceshi-mqtt.lihaink.cn +host=192.168.1.27 port=1883 [topic] name=lot_mqtt diff --git a/data_upload.py b/data_upload.py index 2aa7127..05e9d88 100644 --- a/data_upload.py +++ b/data_upload.py @@ -38,7 +38,8 @@ class DataUploadClient: if __name__ == '__main__': broker = config.get("broker", "host") - port = config.get("broker", "port") + # 这里必须是int类型 + port = config.getint("broker", "port") topic = config.get("topic", "name") username = config.get("security", "username") password = config.get("security", "password") diff --git a/sensor_to_server.py b/sensor_to_server.py index 0ff78ce..7e1c3c8 100755 --- a/sensor_to_server.py +++ b/sensor_to_server.py @@ -239,7 +239,8 @@ if __name__ == '__main__': config = configparser.ConfigParser() config.read('/home/pi/lot_manager/conf/main/config.conf') broker = config.get("broker", "host") - port = config.get("broker", "port") + # 这里必须是int类型 + port = config.getint("broker", "port") username = config.get("security", "username") password = config.get("security", "password") client = mqtt.Client(client_id=device_name)