# MQTT-SN 协议

MQTT-SN 协议是 MQTT 的直系亲属，它使用 UDP 进行通信，标准的端口是1884。MQTT-SN 的主要目的是为了适应受限的设备和网络，比如一些传感器，只有很小的内存和 CPU，TCP 对于这些设备来说非常奢侈。还有一些网络，比如 ZIGBEE，报文的长度在300字节以下，无法承载太大的数据包。所以 MQTT-SN 的数据包更小巧。

MQTT-SN 的官方标准下载地址: <http://mqtt.org/new/wp-content/uploads/2009/06/MQTT-SN_spec_v1.2.pdf>

### MQTT-SN 和 MQTT 的区别

MQTT-SN 的信令和 MQTT 大部分都相同，比如都有 Will, 都有 Connect/Subscribe/Publish 命令.

MQTT-SN 最大的不同是，Topic 使用 TopicId 来代替，而 TopicId 是一个16比特的数字。每一个数字对应一个 Topic, 设备和云端需要使用 REGISTER 命令映射 TopicId 和 Topic 的对应关系。

MQTT-SN 可以随时更改 Will 的内容, 甚至可以取消. 而 MQTT 只允许在 CONNECT 时设定 Will 的内容, 而且不允许更改.

MQTT-SN 的网络中有网关这种设备，它负责把 MQTT-SN 转换成 MQTT，和云端的 MQTT Broker 通信. MQTT-SN 的协议支持自动发现网关的功能。

MQTT-SN 还支持设备的睡眠功能，如果设备进入睡眠状态，无法接收 UDP 数据，网关将把下行的 PUBLISH 消息缓存起来，直到设备苏醒后再传送。

### CP7 MQTT-SN 网关

CP7 Mqtt-sn Gateway 是一个独立运行的网关应用程序，实现了 MQTT-SN 的大部分功能，它相当于一个在云端的 MQTT-SN 网关，直接和 Coolpy7 Core Broker 相连。

{% hint style="info" %}
开源地址：<https://github.com/Coolpy7/mqttsn-gateway>
{% endhint %}

#### 配置参数

File:  bin/data/mqttsn.yml:

```
IsAggregate: true

Host: 0.0.0.0
Port: 1884

BrokerHost: "192.168.200.201"
BrokerPort: 1883
BrokerUser:
BrokerPassword:

LogFilePath: true
LogFilePath: mqttsn.log

MessageQueueSize: 1000
ReadBuffSize: 212992
WriteBuffSize: 212992
```

&#x20;bin/data/mqttsn\_topics.yml

```
---
test1: 1 
test2: 2
```

### 启动 CP7 MQTT-SN 网关

```
# 下载测试工具
git clone https://github.com/Coolpy7/mqttsn-gateway.git
# 进入bin文件夹修改上文的两个配置文件
​
# 启动CP7 MQTT-SN网关
# 参数c: 配置文件mqttsn.yml指定文件位置（默认为程序所有位置下的bin/data文件夹下)
# 参数t: 配置文件mqttsn_topics.yml指定文件位置（默认为程序所有位置下的bin/data文件夹下)
# 以下例示建立连接到192.168.200.201所在的Coolpy7 Core Broker
./go_build_Coolpy7_sngw_go_linux_linux
​
# 工具执行后会以千为单位打印提示信息
2018/11/06 09:28:20 aggregating_gw_impl.go:33: Connect to broker
2018/11/06 09:28:20 gateway.go:59: set read buff size :  212992
2018/11/06 09:28:20 gateway.go:65: set write buff size :  212992
2018/11/06 09:28:21 statistics_reporter.go:30: [STATISTICS-REPORT] SESSION-COUNT :  0 , PUBLISH-RECV-COUNT :  0 , PUBLISH-SEND-COUNT :  0 , PUBLISH-COMP-COUNT :  0
2018/11/06 09:28:21 statistics_reporter.go:44: [STATISTICS-REPORT] PUBLISH-TAT-AVG :  0 , PUBLISH-TAT-MIN :  65535 , PUBLISH-TAT-MAX :  0
```

### MQTT-SN 客户端库

1. <https://github.com/eclipse/paho.mqtt-sn.embedded-c/>
2. <https://github.com/ty4tw/MQTT-SN>
3. <https://github.com/njh/mqtt-sn-tools>
4. <https://github.com/arobenko/mqtt-sn>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coolpy7.gitbook.io/coolpy7book/mqtt-xie-yi-ji-chu-zhi-shi/mqttsn-wang-guan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
