From d8597294af939a686f5422fb1f9f6dea88a233af Mon Sep 17 00:00:00 2001 From: xyj <10908227994@qq.com> Date: Mon, 11 Dec 2023 14:51:41 +0800 Subject: [PATCH] update --- MQTT.py | 19 ++++++++++--------- conf/main/config.conf | 2 +- data_upload.py | 14 +++++++------- sensor_to_server.py | 18 +++++++++--------- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/MQTT.py b/MQTT.py index 4c6b778..b439058 100644 --- a/MQTT.py +++ b/MQTT.py @@ -33,6 +33,7 @@ class MQTTClient: self.username = username self.password = password self.client = mqtt.Client(client_id=device_name) + self.client.username_pw_set(self.username, self.password) self.client.on_connect = self.on_connect self.client.on_message = self.on_message @@ -76,19 +77,19 @@ class MQTTClient: pass def start(self): - self.client.username_pw_set(self.username, self.password) + self.client.connect(self.broker, self.port) if __name__ == '__main__': - broker = config.get("broker", "host") - # 这里必须是int类型 - port = config.getint("broker", "port") - topic = config.get("topic", "name") - username = config.get("security", "username") - password = config.get("security", "password") - print(broker, port, topic, username, password) - MQTT = MQTTClient(broker, port, topic, username, password) + # broker = config.get("broker", "host") + # # 这里必须是int类型 + # port = config.getint("broker", "port") + # topic = config.get("topic", "name") + # username = config.get("security", "username") + # password = config.get("security", "password") + # print(broker, port, topic, username, password) + MQTT = MQTTClient("192.168.1.27", 1883, "lot_mqtt", "demo", "123456") while True: try: MQTT.start() diff --git a/conf/main/config.conf b/conf/main/config.conf index 3e7bb4d..9df52e9 100644 --- a/conf/main/config.conf +++ b/conf/main/config.conf @@ -1,5 +1,5 @@ [broker] -host=192.168.1.27 +host=ceshi-mqtt.lihaink.cn port=1883 [topic] name=lot_mqtt diff --git a/data_upload.py b/data_upload.py index 2895826..261f54b 100644 --- a/data_upload.py +++ b/data_upload.py @@ -38,13 +38,13 @@ class DataUploadClient: if __name__ == '__main__': - broker = config.get("broker", "host") - # 这里必须是int类型 - port = config.getint("broker", "port") - topic = config.get("topic", "name") - username = config.get("security", "username") - password = config.get("security", "password") - MQTT = DataUploadClient(broker, port, topic, username, password) + # broker = config.get("broker", "host") + # # 这里必须是int类型 + # port = config.getint("broker", "port") + # topic = config.get("topic", "name") + # username = config.get("security", "username") + # password = config.get("security", "password") + MQTT = DataUploadClient("192.168.1.27", 1883, "lot_mqt", "demo", "123456") while True: try: MQTT.start() diff --git a/sensor_to_server.py b/sensor_to_server.py index 7e1c3c8..35bf889 100755 --- a/sensor_to_server.py +++ b/sensor_to_server.py @@ -236,17 +236,17 @@ def on_connect(client, userdata, flags, rc): if __name__ == '__main__': - config = configparser.ConfigParser() - config.read('/home/pi/lot_manager/conf/main/config.conf') - broker = config.get("broker", "host") - # 这里必须是int类型 - port = config.getint("broker", "port") - username = config.get("security", "username") - password = config.get("security", "password") + # config = configparser.ConfigParser() + # config.read('/home/pi/lot_manager/conf/main/config.conf') + # broker = config.get("broker", "host") + # # 这里必须是int类型 + # port = config.getint("broker", "port") + # username = config.get("security", "username") + # password = config.get("security", "password") client = mqtt.Client(client_id=device_name) - client.username_pw_set(username, password) + client.username_pw_set("demo", "123456") # Specify callback function client.on_connect = on_connect # Establish a connection - client.connect(broker, port) + client.connect("192.168.1.27", 1883) client.loop_forever()