mqtt can be said to show a very widely used protocol on the Internet of Things, its light weight, simple, open and easy to implement these characteristics. Paho also implemented the python version of mqtt.
1. Connect
Connect and call the connect interface, then call loop_forever and start running
The third parameter of connect is the heartbeat time of mqtt
on_connect
And on_disconnect
a callback function for successful connection and disconnection,
import paho.mqtt.client as mqtt
mqttClient = mqtt.Client("python_test")
def on_disconnect(client, userdata, rc):
print("mqtt broker lost "+str(rc))
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
if __name__ == '__main__':
mqttClient.on_connect = on_connect
mqttClient.on_disconnect = on_disconnect
mqttClient.connect("192.168.18.1", 1883, 10)
mqttClient.loop_forever()
2. SSL login
To add a certificate, just add the following two before calling the connect function.
mqttClient.tls_set(ca_certs="cacert.pem", certfile=None, keyfile=None, cert_reqs=mqtt.ssl.CERT_REQUIRED,
tls_version=mqtt.ssl.PROTOCOL_TLSv1, ciphers=None)
mqttClient.tls_insecure_set(True)
3. Account password
To set the account password, add the following item before calling the connect function.
mqttClient.username_pw_set("python", passwd)
4.WILL theme settings
Set the WILL theme, before calling the connect function, use the will_set interface to set, add will theme and will theme payload
will_payload = {
"ver": "v1",
"module": "offline",
"api": "disconnect",
"param": [{
"k": "mac",
"v": "DC4BDD1DFA48"
}, {
"k": "protypeid",
"v": "PYTHON"
}]
}
mqttClient.will_set("local/offline", payload=json.dumps(will_payload), qos=CLINET_TOPIC_QOS)
5. Subscribe
The subscription of the topic uses the subscribe interface, which is generally set in the on_connect callback function
After subscribing to the topic, our purpose is to receive the subscribed data, so we need to set the callback function on_message to receive data
, as long as there is data sent over on_message will receive.
#!/usr/bin/python3
# -*- coding:utf-8 -*-
# Author: ye.lin
# Time: 2020/02/24
# Describe:
import paho.mqtt.client as mqtt
mqttClient = mqtt.Client("python_test")
def on_disconnect(client, userdata, rc):
print("mqtt broker lost "+str(rc))
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
mqttClient.subscribe(local/broadcast, qos=1)
def on_message_come(lient, userdata, msg):
print(msg.topic + " " + ":" + str(msg.payload))
if __name__ == '__main__':
mqttClient.on_connect = on_connect
mqttClient.on_disconnect = on_disconnect
mqttClient.on_message = on_message_come
mqttClient.tls_set(ca_certs="zgateway_cacert.pem", certfile=None, keyfile=None, cert_reqs=mqtt.ssl.CERT_REQUIRED,
tls_version=mqtt.ssl.PROTOCOL_TLSv1, ciphers=None)
mqttClient.tls_insecure_set(True)
mqttClient.connect("192.168.18.1", 6885, 10)
mqttClient.loop_forever()
6. Release
Publish using the publish interface, as follows
info_payload = {
"msg": "test"
}
mqttClient.publish("local/notify", payload=json.dumps(info_payload), qos=CLINET_TOPIC_QOS)