Mqtt
来自Jack's Lab
(版本间的差异)
(→Quickstart) |
(→CMD) |
||
第57行: | 第57行: | ||
<br> | <br> | ||
− | == | + | == Python Client == |
− | <source lang= | + | <source lang=python> |
− | + | import json | |
+ | import paho.mqtt.client as mqtt | ||
+ | import sys | ||
− | + | # replace it to your id | |
+ | ########################################### | ||
+ | mqtt_uname = 'test' | ||
+ | mqtt_pass = 'test' | ||
+ | devid = '12020030001' | ||
+ | ########################################### | ||
− | + | def help_msg(): | |
− | + | print("Supported cmd:") | |
− | + | print("\tmqtt.py message") | |
− | + | ||
− | + | if len(sys.argv) >= 2: | |
+ | set_state = sys.argv[1] | ||
+ | else: | ||
+ | help_msg() | ||
+ | sys.exit(-2) | ||
+ | |||
+ | mqtt_tx_topic = 'dev/gw' | ||
+ | |||
+ | mqtt_rx_topic = 'app2dev/' + devid | ||
+ | |||
+ | mqtt_host = '192.168.1.97' | ||
+ | |||
+ | mqtt_port = 1883 | ||
+ | mqtt_msg = set_state | ||
+ | |||
+ | def on_connect(client, u_dat, rc): | ||
+ | if rc == 0: | ||
+ | print("Connected successfully") | ||
+ | else: | ||
+ | print("Connection failed. rc = "+str(rc)) | ||
+ | |||
+ | def on_publish(client, u_dat, rc): | ||
+ | print("Message "+str(rc)+" published.") | ||
+ | |||
+ | def on_subscribe(client, u_dat, mid, qos): | ||
+ | print("Subscribe with "+str(mid)+" received") | ||
+ | |||
+ | def on_message(client, udat, msg): | ||
+ | print("Message received on topic "+msg.topic+" and payload "+str(msg.payload)) | ||
+ | |||
+ | mqttc = mqtt.Client() | ||
+ | |||
+ | mqttc.on_connect = on_connect | ||
+ | mqttc.on_publish = on_publish | ||
+ | mqttc.on_subscribe = on_subscribe | ||
+ | mqttc.on_message = on_message | ||
+ | |||
+ | mqttc.username_pw_set(mqtt_uname, mqtt_pass) | ||
+ | mqttc.connect(mqtt_host, mqtt_port) | ||
+ | |||
+ | mqttc.publish(mqtt_tx_topic, mqtt_msg) | ||
</source> | </source> | ||
<br><br><br><br> | <br><br><br><br> |
2020年3月21日 (六) 19:29的版本
1 Overview
MQTT是一个轻量的发布订阅模式消息传输协议,专门针对低带宽和不稳定网络环境的物联网应用设计。
MQTT官网: http://mqtt.org
MQTT V3.1.1协议规范: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html
特点:
- 开放消息协议,简单易实现
- 发布订阅模式,一对多消息发布
- 基于TCP/IP网络连接
- 1字节固定报头,2字节心跳报文,报文结构紧凑
- 消息QoS支持,可靠传输保证
应用:
MQTT协议广泛应用于物联网、移动互联网、智能硬件、车联网、电力能源等领域。
- 物联网M2M通信,物联网大数据采集
- Android消息推送,WEB消息推送
- 移动即时消息,例如Facebook Messenger
- 智能硬件、智能家具、智能电器
- 车联网通信,电动车站桩采集
- 智慧城市、远程医疗、远程教育
- 电力、石油与能源等行业市场
2 Quickstart
MQTT 协议基于主题 (Topic) 进行消息路由
主题 (Topic) 类似 URL 路径,例如:
dev/gateway dev2srv/DEV1202 srv2dev/DEV1203
- 发布者通过主题 (Topic) 发送消息
- 所有订阅 (Subscribe) 该主题的订阅者,都会收到消息
主题 (Topic) 通过 '/' 分割层级,支持 '+', '#' 通配符:
'+': 表示通配一个层级,例如 a/+,匹配 a/x, a/y '#': 表示通配多个层级,例如 a/#,匹配 a/x, a/b/c
订阅者可以订阅含通配符主题,但发布者不允许向含通配符主题发布消息
3 Python Client
import json import paho.mqtt.client as mqtt import sys # replace it to your id ########################################### mqtt_uname = 'test' mqtt_pass = 'test' devid = '12020030001' ########################################### def help_msg(): print("Supported cmd:") print("\tmqtt.py message") if len(sys.argv) >= 2: set_state = sys.argv[1] else: help_msg() sys.exit(-2) mqtt_tx_topic = 'dev/gw' mqtt_rx_topic = 'app2dev/' + devid mqtt_host = '192.168.1.97' mqtt_port = 1883 mqtt_msg = set_state def on_connect(client, u_dat, rc): if rc == 0: print("Connected successfully") else: print("Connection failed. rc = "+str(rc)) def on_publish(client, u_dat, rc): print("Message "+str(rc)+" published.") def on_subscribe(client, u_dat, mid, qos): print("Subscribe with "+str(mid)+" received") def on_message(client, udat, msg): print("Message received on topic "+msg.topic+" and payload "+str(msg.payload)) mqttc = mqtt.Client() mqttc.on_connect = on_connect mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe mqttc.on_message = on_message mqttc.username_pw_set(mqtt_uname, mqtt_pass) mqttc.connect(mqtt_host, mqtt_port) mqttc.publish(mqtt_tx_topic, mqtt_msg)