This commit is contained in:
xyj 2023-12-11 15:29:40 +08:00
parent f5fc061309
commit d93fb5c5c1
2 changed files with 8 additions and 4 deletions

10
MQTT.py
View File

@ -8,7 +8,8 @@ from tool import *
import configparser import configparser
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('/home/pi/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): def valid(msg, client):
@ -33,11 +34,11 @@ class MQTTClient:
self.password = password self.password = password
# 千万不要指定client_id 不然死翘翘 # 千万不要指定client_id 不然死翘翘
self.client = mqtt.Client() self.client = mqtt.Client()
self.client.on_connect = self.on_connect self.client.on_connect = self.on_connect
self.client.on_message = self.on_message self.client.on_message = self.on_message
def on_connect(self, client, userdata, flags, rc): def on_connect(self, client, userdata, flags, rc):
print(rc)
if rc == 0: if rc == 0:
self.client.subscribe(self.topic) self.client.subscribe(self.topic)
client.publish('success', payload='成功订阅lot_mqtt,time=' + str(datetime.datetime.now()), qos=0) client.publish('success', payload='成功订阅lot_mqtt,time=' + str(datetime.datetime.now()), qos=0)
@ -90,10 +91,13 @@ if __name__ == '__main__':
username = config.get("security", "username") username = config.get("security", "username")
password = config.get("security", "password") password = config.get("security", "password")
# print(broker, port, topic, username, password) # print(broker, port, topic, username, password)
MQTT = MQTTClient(broker, 1883, port, 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)
while True: while True:
try: try:
MQTT.start() MQTT.start()
MQTT.client.loop_forever() MQTT.client.loop_forever()
except Exception as e: except Exception as e:
print(e)
time.sleep(30) time.sleep(30)

View File

@ -45,7 +45,7 @@ if __name__ == '__main__':
username = config.get("security", "username") username = config.get("security", "username")
password = config.get("security", "password") password = config.get("security", "password")
# MQTT = DataUploadClient("192.168.1.27", 1883, "lot_mqtt", "demo", "123456") # MQTT = DataUploadClient("192.168.1.27", 1883, "lot_mqtt", "demo", "123456")
MQTT = DataUploadClient(broker, 1883, port, username, password) MQTT = DataUploadClient(broker, port, topic, username, password)
while True: while True:
try: try:
MQTT.start() MQTT.start()