update
This commit is contained in:
parent
f85258431e
commit
d8597294af
19
MQTT.py
19
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()
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
[broker]
|
||||
host=192.168.1.27
|
||||
host=ceshi-mqtt.lihaink.cn
|
||||
port=1883
|
||||
[topic]
|
||||
name=lot_mqtt
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue