MQTT 使用场景详解
什么是 MQTT
MQTT(Message Queuing Telemetry Transport)是一种轻量级、基于发布/订阅模式的消息传输协议,由 IBM 的 Andy Stanford-Clark 和 Arcom 的 Arlen Nipper 于 1999 年设计。该协议专为低带宽、高延迟或不可靠的网络环境优化,特别适合物联网(IoT)设备通信。
MQTT 以其简洁的设计和极低的开销著称,最小报文只有 2 字节,广泛应用于智能家居、工业物联网、车联网等领域。
MQTT 核心特性
1. 发布/订阅模式
与传统的请求/响应模式不同,MQTT 采用发布/订阅模式:
- 发布者(Publisher):将消息发送到主题(Topic)
- 订阅者(Subscriber):订阅感兴趣的主题
- Broker(代理):负责接收消息并转发给订阅者
这种松耦合的架构使得发布者和订阅者可以独立运行,无需直接连接。
2. QoS(服务质量)
MQTT 提供三种服务质量级别:
-
QoS 0:最多分发一次(At most once)
-
QoS 1:至少分发一次(At least once)
-
QoS 2:仅分发一次(Exactly once)
3. 主题(Topic)
主题是 MQTT 的核心概念,采用分层结构:
1
2
3
|
home/livingroom/temperature
home/bedroom/humidity
industrial/factory1/sensor/pressure
|
支持通配符:
4. 遗嘱消息(Last Will)
当客户端异常断开时,Broker 可以发送预设的遗嘱消息,用于通知其他客户端设备离线。
5. 保留消息(Retained Message)
Broker 会保留最后一个主题消息,新订阅者可以立即获取最新数据。
6. 持久会话(Persistent Session)
客户端可以恢复之前的订阅和未接收的消息。
MQTT 常用客户端库
1. Eclipse Paho
支持多种语言,是最流行的 MQTT 客户端库:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# Python
pip install paho-mqtt
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print(f"Connected with result code {rc}")
client.subscribe("home/temperature")
def on_message(client, userdata, msg):
print(f"{msg.topic}: {msg.payload}")
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("broker.emqx.io", 1883, 60)
client.loop_forever()
|
2. MQTT.js
JavaScript/Node.js 首选库:
1
2
3
4
5
6
7
8
9
10
11
|
const mqtt = require('mqtt')
const client = mqtt.connect('mqtt://broker.emqx.io:1883')
client.on('connect', () => {
console.log('Connected')
client.subscribe('home/temperature')
})
client.on('message', (topic, message) => {
console.log(`${topic}: ${message.toString()}`)
})
|
3. MQTT.js 浏览器端
1
2
3
4
5
6
7
8
9
10
11
12
|
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
<script>
const client = mqtt.connect('wss://broker.emqx.io:8084/mqtt')
client.on('connect', () => {
client.subscribe('home/temperature')
})
client.on('message', (topic, message) => {
console.log(`${topic}: ${message.toString()}`)
})
</script>
|
4. Go 语言
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import (
mqtt "github.com/eclipse/paho.mqtt.golang"
)
func main() {
opts := mqtt.NewClientOptions().AddBroker("tcp://broker.emqx.io:1883")
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
client.Subscribe("home/temperature", 0, func(client mqtt.Client, msg mqtt.Message) {
fmt.Printf("%s: %s\n", msg.Topic(), string(msg.Payload()))
})
select {}
}
|
主流 MQTT Broker
1. Mosquitto
最流行的开源 MQTT Broker,资源占用低:
1
2
3
4
5
6
7
8
9
10
11
|
# 安装
sudo apt install mosquitto mosquitto-clients
# 启动
sudo systemctl start mosquitto
# 订阅主题
mosquitto_sub -t "home/temperature"
# 发布消息
mosquitto_pub -t "home/temperature" -m "25.5"
|
配置文件 /etc/mosquitto/mosquitto.conf:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# 监听端口
listener 1883
# 允许匿名访问
allow_anonymous true
# 启用websockets
listener 9001
protocol websockets
# 持久化存储
persistence true
persistence_location /var/lib/mosquitto/
# 日志
log_dest file /var/log/mosquitto/mosquitto.log
log_type error
log_type warning
log_type notice
log_type information
|
2. EMQX
功能强大的企业级 MQTT Broker:
1
2
3
4
5
6
7
|
# Docker 部署
docker run -d --name emqx \
-p 1883:1883 \
-p 8083:8083 \
-p 8084:8084 \
-p 18083:18083 \
emqx/emqx:latest
|
- 支持集群
- 规则引擎
- 数据持久化
- WebSocket 支持
3. Apache ActiveMQ
企业级消息中间件,支持 MQTT:
1
2
3
4
5
6
|
<!-- activemq.xml -->
<broker>
<transportConnectors>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883"/>
</transportConnectors>
</broker>
|
4. HiveMQ
企业级 MQTT 平台:
典型使用场景
1. 智能家居
MQTT 是智能家居领域最常用的协议:
1
2
3
4
5
|
[温度传感器] --(MQTT)--> [Broker] --(MQTT)--> [手机APP]
|
--(MQTT)--> [智能音箱]
|
--(MQTT)--> [云平台]
|
典型主题设计:
1
2
3
4
5
6
7
8
9
10
|
# 设备状态上报
home/{room}/sensor/{device_id}/state
home/livingroom/sensor/temp_001/state
# 设备控制指令
home/{room}/switch/{device_id}/set
home/bedroom/switch/light_001/set
# 设备在线状态
home/{room}/device/{device_id}/online
|
实际应用示例:
1
2
3
4
5
6
7
8
9
10
|
# 温度传感器上报数据
client.publish(
"home/livingroom/sensor/temp_001/state",
payload='{"temperature": 25.5, "humidity": 60}',
qos=1,
retain=True
)
# 手机APP订阅温度数据
client.subscribe("home/+/sensor/+/state")
|
2. 工业物联网(IIoT)
工厂设备监控和远程控制:
1
2
|
[PLC] ---> [工业网关] --(MQTT)--> [MQTT Broker] ---> [SCADA系统]
[传感器] --> [工业网关] --(MQTT)--> [MQTT Broker] ---> [数据分析平台]
|
主题设计:
1
2
3
4
5
6
7
8
9
10
|
# 设备数据
industrial/{factory}/{line}/sensor/{device_id}/data
industrial/factory1/line1/sensor/pump_001/data
# 设备告警
industrial/{factory}/{line}/alarm/{level}
industrial/factory1/line1/alarm/critical
# 设备控制
industrial/{factory}/{line}/control/{device_id}/command
|
数据格式(JSON):
1
2
3
4
5
6
7
8
|
{
"timestamp": "2026-04-09T10:00:00Z",
"device_id": "pump_001",
"type": "pressure",
"value": 1.5,
"unit": "MPa",
"status": "normal"
}
|
3. 车联网(V2X)
车辆与云平台通信:
1
2
3
|
[车载终端] --(MQTT)--> [云平台] <--数据分析
|
<--[车主APP]
|
主题设计:
1
2
3
4
5
6
7
8
9
10
11
|
# 车辆状态上报
vehicle/{vin}/telemetry
vehicle/ABC123/telemetry
# 车辆位置
vehicle/{vin}/location
vehicle/ABC123/location
# 远程控制
vehicle/{vin}/command/lock
vehicle/{vin}/command/start
|
4. 智慧农业
温室大棚环境监控:
1
2
3
|
[温湿度传感器] ---> [网关] --(MQTT)--> [云平台] ---> [管理平台]
[光照传感器] --->
[土壤传感器] --->
|
主题设计:
1
2
3
4
5
6
7
8
|
# 温室数据
agri/greenhouse/{id}/sensor/temperature
agri/greenhouse/{id}/sensor/humidity
agri/greenhouse/{id}/sensor/light
agri/greenhouse/{id}/sensor/soil
# 灌溉控制
agri/greenhouse/{id}/irrigation/set
|
5. 医疗健康
可穿戴设备和医疗设备数据采集:
1
2
|
[心率监测器] --(BLE)--> [手机] --(MQTT)--> [健康平台]
[血压计] ------> [手机] --(MQTT)--> [健康平台]
|
主题设计:
1
2
3
4
5
6
7
|
# 生理数据
health/{user_id}/heartrate
health/{user_id}/blood_pressure
health/{user_id}/steps
# 设备状态
health/{user_id}/device/{device_id}/status
|
6. 物流追踪
货物运输状态监控:
1
2
|
[GPS追踪器] --(4G)--> [MQTT Broker] --> [物流平台]
[温湿度计] -----> [MQTT Broker] --> [物流平台]
|
主题设计:
1
2
3
4
5
6
7
8
9
|
# 货物位置
logistics/{shipment_id}/location
# 环境数据
logistics/{shipment_id}/environment/temperature
logistics/{shipment_id}/environment/humidity
# 状态更新
logistics/{shipment_id}/status
|
7. 电力系统
智能电网数据采集:
1
2
|
[智能电表] ---> [集中器] --(MQTT)--> [用电管理平台]
[变压器] -----> [集中器] --(MQTT)--> [用电管理平台]
|
8. 机器人控制
机器人状态监控和指令下发:
1
2
3
4
5
6
7
8
|
# 机器人状态
robot/{robot_id}/status
robot/{robot_id}/telemetry/position
robot/{robot_id}/telemetry/battery
# 指令控制
robot/{robot_id}/command/move
robot/{robot_id}/command/gripper
|
MQTT 与其他协议对比
| 特性 |
MQTT |
CoAP |
AMQP |
HTTP |
| 模式 |
发布/订阅 |
请求/响应 |
队列/发布-订阅 |
请求/响应 |
| 传输 |
TCP/UDP |
UDP |
TCP |
TCP |
| 报文大小 |
最小2字节 |
最小4字节 |
较大 |
较大 |
| QoS |
3级 |
2级 |
3级 |
无 |
| 功耗 |
低 |
极低 |
中等 |
高 |
| 适用场景 |
IoT |
传感器 |
企业消息 |
Web API |
安全性最佳实践
1. 认证和授权
1
2
3
4
5
6
7
8
9
10
|
# 用户名密码认证
client.username_pw_set("username", "password")
# TLS/SSL 加密
client.tls_set(
ca_certs="/path/to/ca.crt",
certfile="/path/to/client.crt",
keyfile="/path/to/client.key"
)
client.tls_insecure_set(False)
|
2. 访问控制
使用 ACL 限制主题访问:
1
2
3
4
5
6
7
8
9
10
11
12
|
{
"acls": [
{
"username": "device001",
"allow": ["home/device001/#"]
},
{
"username": "app001",
"allow": ["home/+/sensor/#"]
}
]
}
|
3. 主题命名规范
- 使用有意义的分层结构
- 避免特殊字符
- 统一命名风格(小写)
总结
MQTT 作为物联网通信的核心协议,以其轻量、可靠、易用的特点,成为连接设备与云端的桥梁。从智能家居到工业物联网,从车联网到医疗健康,MQTT 正在各行各业发挥重要作用。
其优势总结:
- ✅ 轻量级,低资源消耗
- ✅ 发布/订阅,松耦合
- ✅ QoS 机制,可靠传输
- ✅ 主题过滤,灵活路由
- ✅ 持续连接,实时通信
- ✅ 广泛支持,生态成熟
掌握 MQTT,将为你打开物联网世界的大门。