Files
blog-press/docs/Web/Network/Mqtt.md
2026-05-27 11:26:16 +08:00

56 lines
1.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Mqtt简介与使用
date: 2026-05-27
---
# 一、简介
  MQTTMessage Queuing Telemetry Transport 是一种 轻量级物联网通信协议,基于 发布 / 订阅Pub/Sub 模式。
## 1.1 核心概念
1. Broker消息服务器
2. Client发布或订阅消息的设备 / 程序
3. Topic: 消息主题(路径形式)
4. Publish发布消息
5. Subscribe订阅消息
6. QoS服务质量等级0 / 1 / 2
## 1.2 工作方式
```text
[设备A] --发布--> TopicA --> [Broker] --> TopicA --> [设备B]
```
# 二、安装
  [EMQX](https://www.emqx.com/zh) Enterprise 是基于开源 EMQX 扩展的企业级 MQTT 物联网消息接入平台,完全兼容 MQTT v3.1.1 / v5.0。
## 2.1 docker安装
```yml
services:
emqx:
image: emqx/emqx-enterprise:5.10.0
container_name: emqx
ports:
- "1883:1883"
- "8083:8083"
- "8084:8084"
- "8883:8883"
- "18083:18083"
restart: always
```
  启动容器后输入ip:18083默认账号密码为admin/public。
# 三、消息规范
## 3.1 Topic
```text
<domain>/<product>/<device_id>/<msg_type>/<resource>
```
&emsp;&emsp;例如`iot/thermostat/dev001/status/online``iot/thermostat/dev001/cmd/set_mode`
## 通配符
1. +单层通配符例如iot/+/dev001/telemetry/+
2. #多层通配符只能放在topic末尾例如iot/thermostat/dev001/#
::: tip
使用+通配符时必须严格满足层级要求例如如果topic是5层那么iot/+将无法匹配到消息
:::