This commit is contained in:
xyj 2023-12-11 14:51:41 +08:00
parent f85258431e
commit d8597294af
4 changed files with 27 additions and 26 deletions

19
MQTT.py
View File

@ -33,6 +33,7 @@ class MQTTClient:
self.username = username self.username = username
self.password = password self.password = password
self.client = mqtt.Client(client_id=device_name) 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_connect = self.on_connect
self.client.on_message = self.on_message self.client.on_message = self.on_message
@ -76,19 +77,19 @@ class MQTTClient:
pass pass
def start(self): def start(self):
self.client.username_pw_set(self.username, self.password)
self.client.connect(self.broker, self.port) self.client.connect(self.broker, self.port)
if __name__ == '__main__': if __name__ == '__main__':
broker = config.get("broker", "host") # broker = config.get("broker", "host")
# 这里必须是int类型 # # 这里必须是int类型
port = config.getint("broker", "port") # port = config.getint("broker", "port")
topic = config.get("topic", "name") # topic = config.get("topic", "name")
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, port, topic, username, password) MQTT = MQTTClient("192.168.1.27", 1883, "lot_mqtt", "demo", "123456")
while True: while True:
try: try:
MQTT.start() MQTT.start()

View File

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

View File

@ -38,13 +38,13 @@ class DataUploadClient:
if __name__ == '__main__': if __name__ == '__main__':
broker = config.get("broker", "host") # broker = config.get("broker", "host")
# 这里必须是int类型 # # 这里必须是int类型
port = config.getint("broker", "port") # port = config.getint("broker", "port")
topic = config.get("topic", "name") # topic = config.get("topic", "name")
username = config.get("security", "username") # username = config.get("security", "username")
password = config.get("security", "password") # password = config.get("security", "password")
MQTT = DataUploadClient(broker, port, topic, username, password) MQTT = DataUploadClient("192.168.1.27", 1883, "lot_mqt", "demo", "123456")
while True: while True:
try: try:
MQTT.start() MQTT.start()

View File

@ -236,17 +236,17 @@ def on_connect(client, userdata, flags, rc):
if __name__ == '__main__': if __name__ == '__main__':
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')
broker = config.get("broker", "host") # broker = config.get("broker", "host")
# 这里必须是int类型 # # 这里必须是int类型
port = config.getint("broker", "port") # port = config.getint("broker", "port")
username = config.get("security", "username") # username = config.get("security", "username")
password = config.get("security", "password") # password = config.get("security", "password")
client = mqtt.Client(client_id=device_name) client = mqtt.Client(client_id=device_name)
client.username_pw_set(username, password) client.username_pw_set("demo", "123456")
# Specify callback function # Specify callback function
client.on_connect = on_connect client.on_connect = on_connect
# Establish a connection # Establish a connection
client.connect(broker, port) client.connect("192.168.1.27", 1883)
client.loop_forever() client.loop_forever()