This commit is contained in:
xyj 2023-12-11 14:42:49 +08:00
parent 857f0cea04
commit 3e1c26f91e
4 changed files with 11 additions and 9 deletions

12
MQTT.py
View File

@ -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")

View File

@ -1,5 +1,5 @@
[broker]
host=ceshi-mqtt.lihaink.cn
host=192.168.1.27
port=1883
[topic]
name=lot_mqtt

View File

@ -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")

View File

@ -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)