From 67d610ada4ebd59887d32b24d59c0535d1762960 Mon Sep 17 00:00:00 2001 From: xyj <10908227994@qq.com> Date: Mon, 11 Dec 2023 16:46:03 +0800 Subject: [PATCH] update --- MQTT.py | 33 ++++++++------------------ conf/device/device.conf | 9 +++++++ conf/main/{config.conf => common.conf} | 9 ++++--- config.py | 21 ++++++++++++++++ data_upload.py | 19 ++++----------- sensor_to_server.py | 23 ++++++------------ 6 files changed, 56 insertions(+), 58 deletions(-) create mode 100644 conf/device/device.conf rename conf/main/{config.conf => common.conf} (73%) create mode 100644 config.py diff --git a/MQTT.py b/MQTT.py index 58b8902..3330159 100644 --- a/MQTT.py +++ b/MQTT.py @@ -3,13 +3,9 @@ import time import paho.mqtt.client as mqtt -from device import device_name 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') +from config import broker, port, subscribe_topic, publish_topic, username, password def valid(msg, client): @@ -17,11 +13,6 @@ def valid(msg, client): if 'msg' not in origin_data: client.publish('error', payload='msg must be supplied', qos=0) return False - if 'device_name' not in origin_data: - client.publish('error', payload='device_name must be supplied', qos=0) - return False - if device_name != origin_data['device_name']: - return False return True @@ -34,14 +25,15 @@ class MQTTClient: self.password = password # 千万不要指定client_id 不然死翘翘 self.client = mqtt.Client() + self.client.username_pw_set(self.username, self.password) self.client.on_connect = self.on_connect self.client.on_message = self.on_message def on_connect(self, client, userdata, flags, rc): - print(rc) if rc == 0: self.client.subscribe(self.topic) - client.publish('success', payload='成功订阅lot_mqtt,time=' + str(datetime.datetime.now()), qos=0) + client.publish('success', payload='成功订阅' + 'device_name' + ',time=' + str(datetime.datetime.now()), + qos=0) def on_message(self, client, userdata, msg): if not valid(msg, client): @@ -79,25 +71,20 @@ 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) - # print(type(broker), type(port), type(topic), type(username), type(password)) - # MQTT = MQTTClient("ceshi-mqtt.lihaink.cn", 1883, "lot_mqtt", "demo", "123456") - MQTT = MQTTClient(broker, port, topic, username, password) + print(broker, port, subscribe_topic, publish_topic, username, password) + # MQTT客户端 + MQTT = MQTTClient(broker, port, subscribe_topic, username, password) + # 循环连接 while True: try: MQTT.start() + # 阻塞监听 MQTT.client.loop_forever() except Exception as e: print(e) + # 异常等待时间在进行连接 time.sleep(30) diff --git a/conf/device/device.conf b/conf/device/device.conf new file mode 100644 index 0000000..e75d2fa --- /dev/null +++ b/conf/device/device.conf @@ -0,0 +1,9 @@ +# 设备1 +[lot_mqtt_device1] +# 订阅的控制主题,必须和系统设置的相同 +subscribe_topic=lot_mqtt_device1 +# 发布消息的主题 +publish_topic=device1 +username=demo +password=123456 +# ... diff --git a/conf/main/config.conf b/conf/main/common.conf similarity index 73% rename from conf/main/config.conf rename to conf/main/common.conf index 9df52e9..52945c0 100644 --- a/conf/main/config.conf +++ b/conf/main/common.conf @@ -1,11 +1,10 @@ +# 域名地址 [broker] host=ceshi-mqtt.lihaink.cn port=1883 -[topic] -name=lot_mqtt -[security] -username=demo -password=123456 [record] post_record_list_url=https://shop.lihaink.cn/api/index/file_list post_record_url=https://shop.lihaink.cn/api/index/upload +[message_type] + + diff --git a/config.py b/config.py new file mode 100644 index 0000000..18c7235 --- /dev/null +++ b/config.py @@ -0,0 +1,21 @@ +import configparser + +from device import device_name + +config = configparser.ConfigParser() +# 读取公共配置 +config.read('conf/main/common.conf') +# 域名 +broker = config.get("broker", "host") +# 端口,这里必须是int类型 +port = config.getint("broker", "port") +# 读取设备配置 +config.read('conf/device/device.conf') +# 订阅的主题 +subscribe_topic = config.get(device_name, "subscribe_topic") +# 发布的主题 +publish_topic = config.get(device_name, "publish_topic") +# 用户 +username = config.get(device_name, "username") +# 密码 +password = config.get(device_name, "password") diff --git a/data_upload.py b/data_upload.py index 6eaabba..30eb97c 100644 --- a/data_upload.py +++ b/data_upload.py @@ -1,13 +1,10 @@ -import configparser import time import paho.mqtt.client as mqtt -from device import device_name from tool import * +from config import broker, port, subscribe_topic, username, password -config = configparser.ConfigParser() -config.read('/home/pi/lot_manager/conf/main/config.conf') times = 6 @@ -20,11 +17,13 @@ class DataUploadClient: self.password = password # 千万不要指定client_id 不然死翘翘 self.client = mqtt.Client() - + self.client.username_pw_set(self.username, self.password) self.client.on_connect = self.on_connect self.client.on_disconnect = self.on_disconnect def on_connect(self, client, userdata, flags, rc): + global times + times = 6 self.client.publish('success', payload='连接成功,执行数据推送和本地存储' + str(time.time()), qos=0) subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/start_data_upload.sh'], shell=True) @@ -33,19 +32,11 @@ class DataUploadClient: subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/stop_data_upload.sh'], shell=True) 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") - # MQTT = DataUploadClient("192.168.1.27", 1883, "lot_mqtt", "demo", "123456") - MQTT = DataUploadClient(broker, port, topic, username, password) + MQTT = DataUploadClient(broker, port, subscribe_topic, username, password) while True: try: MQTT.start() diff --git a/sensor_to_server.py b/sensor_to_server.py index d0f6b58..7da900b 100755 --- a/sensor_to_server.py +++ b/sensor_to_server.py @@ -1,6 +1,4 @@ -import configparser import json -import os import threading import time @@ -9,7 +7,8 @@ import paho.mqtt.client as mqtt from api import add from db.models.lot_data_model import LOT_DATA -from device import device_name + +from config import broker, port, subscribe_topic, publish_topic, username, password def get_data(): @@ -186,11 +185,10 @@ def get_data(): 'rainfall': rainfall_data, 'wind_speed': speedwind_data, 'wind_direction': winddirection_data, - 'create_time': int(time.time()), - 'device_name': device_name + 'create_time': int(time.time()) } t2 = LOT_DATA(**data) - client.publish('demo', payload=json.dumps(data, ensure_ascii=False), qos=0) + client.publish(publish_topic, payload=json.dumps(data, ensure_ascii=False), qos=0) add(t2) except Exception as e: print(e) @@ -216,11 +214,10 @@ def p(): 'rainfall': 1, 'wind_speed': 1, 'wind_direction': 1, - 'create_time': int(time.time()), - 'device_name': device_name + 'create_time': int(time.time()) } t2 = LOT_DATA(**data) - client.publish('demo', payload=json.dumps(data, ensure_ascii=False), qos=0) + client.publish(publish_topic, payload=json.dumps(data, ensure_ascii=False), qos=0) add(t2) except Exception as e: print(e) @@ -236,13 +233,7 @@ 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") + print(broker, port, subscribe_topic, publish_topic, username, password) # 千万不要指定client_id 不然死翘翘!!! client = mqtt.Client() client.username_pw_set(username, password)