找回密码
 立即注册
搜索
查看: 299|回复: 0

MQTT 学习笔记

[复制链接]

266

主题

0

回帖

1119

积分

管理员

积分
1119
发表于 2024-1-12 00:18:34 | 显示全部楼层 |阅读模式

MQTT(Message Queuing Telemetry Transport,消息队列遥测传输) 是 IBM 开发的一个即时通信协议。它是一种使用发布/订阅(publish/subscribe)模型的轻量级二进制通信协议,这使得它很适合 M2M(machine to machine)的消息传递,比如低功耗传感器或移动通信设备。

参与角色

由于是基于 publish/subscribe 模型的,所以必定存在消息的发布者和消息的订阅者两个角色,此外还应具有消息代理者(message broker),也就是服务器这一角色。

动作

发布者需要将消息推送至服务器,订阅者要有订阅的动作,然后服务器需要将消息推送给订阅者。

话题

消息以话题为归集(分类),话题将发布者与订阅者的兴趣点牵连了起来。话题就是一个 UTF-8 的字符串,可以使用斜杠(/)表示多个层级关系。话题不需要创建,直接使用就可以。

QoS

QoS(Quality of Service,服务质量)

共有 3 级:

0: The broker/client will deliver the message once, with no confirmation.

1: The broker/client will deliver the message at least once, with confirmation required.

2: The broker/client will deliver the message exactly once by using a four step handshake.

通配符

+ 与 #

以下带通配符的字符串可以匹配主题 a/b/c/d

+/b/c/d

a/+/c/d

a/+/+/d

+/+/+/+

#

a/#

a/b/#

a/b/c/#

+/b/c/#

只允许使用通配符订阅话题,不允许使用通配符发布消息。

消息类型

MQTT 拥有 14 种不同的消息类型:

CONNECT:客户端连接到 MQTT 代理

CONNACK:连接确认

PUBLISH:新发布消息

PUBACK:新发布消息确认,是 QoS 1 给 PUBLISH 消息的回复

PUBREC:QoS 2 消息流的第一部分,表示消息发布已记录

PUBREL:QoS 2 消息流的第二部分,表示消息发布已释放

PUBCOMP:QoS 2 消息流的第三部分,表示消息发布完成

SUBSCRIBE:客户端订阅某个主题

SUBACK:对于 SUBSCRIBE 消息的确认

UNSUBSCRIBE:客户端终止订阅的消息

UNSUBACK:对于 UNSUBSCRIBE 消息的确认

PINGREQ:心跳

PINGRESP:确认心跳

DISCONNECT:客户端终止连接前优雅地通知 MQTT 代理

相关软件

当前流行的开源代理服务器(MQTT Broker)的实现有:mosquitto, HiveMQ, Apache ActiveMQ, RabbitMQ, mosca 等,MQTT 客户端也有不同操作系统和编程语言下的实现,流行的客户端库(MQTT ClientLibraries)有:Eclipse Paho(支持 C,C++, Java, JavaScript, Python, Go, C#), M2MQTT(C#), Fusesource MQTTClient(Java), MQTT.js(javascript), libmosquitto(c/c++)等等,通过以上库而开发出的 MQTT 客户端应用程序有:mosquitto_pub/mosquitto_sub(可运行于 Linux, Windows, MacOSX 操作系统),HiveMQWebsocket Client(Web browser),MyMQTT(Android),MQTTLens(Google Chrome)等等。

Mosquitto

Eclipse Mosquitto™ 是一款实现了 MQTT 3.1 和 3.1.1 版本的开源消息代理软件(C 语言)。

官方网站:https://mosquitto.org/

测试:

wget http://mosquitto.org/files/source/mosquitto-1.4.13.tar.gz

tar -zxvf mosquitto-1.4.13.tar.gz

cd mosquitto-1.4.13

make(编译过程需要以下三个库:libssl-dev、libc-ares-dev、uuid-dev)

sudo make install

编译生成的文件主要是:

/usr/local/sbin/mosquitto 代理服务器主程序

/usr/local/bin/mosquitto_pub 用于发布消息的命令行客户端测试程序

/usr/local/bin/mosquitto_sub 用于订阅消息的命令行客户端测试程序

编译时可能出现的错误及解决办法:

(1)./mosquitto_internal.h:27:27: fatal error: openssl/ssl.h: 没有那个文件或目录

sudo apt-get install libssl-dev

libssl-dev 是将 OpenSSL 用于开发时使用的库文件、头文件等(Debian、Ubuntu)

(2)./mosquitto_internal.h:40:20: fatal error: ares.h: 没有那个文件或目录

安装 libc-ares-dev

sudo apt-get install libc-ares-dev

libc-ares-dev 是 c-ares 的开发库,用于异步域名解析。

(3)read_handle_server.c:31:25: fatal error: uuid/uuid.h: 没有那个文件或目录

sudo apt-get install uuid-dev

它是用于生成 UUID(xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx (8-4-4-4-12))的库

测试:

终端 1:

mosquitto -v

1499668521: mosquitto version 1.4.13 (build date 2017-07-10 11:52:05+0800) starting
1499668521: Using default config.
1499668521: Opening ipv4 listen socket on port 1883.
1499668521: Opening ipv6 listen socket on port 1883.
1499668525: New connection from ::1 on port 1883.
1499668525: New client connected from ::1 as mosqsub|1739-debian (c1, k60).
1499668525: Sending CONNACK to mosqsub|1739-debian (0, 0)
1499668525: Received SUBSCRIBE from mosqsub|1739-debian
1499668525:     sensors/devicename/temperature (QoS 0)
1499668525: mosqsub|1739-debian 0 sensors/devicename/temperature
1499668525: Sending SUBACK to mosqsub|1739-debian
1499668529: New connection from ::1 on port 1883.
1499668529: New client connected from ::1 as mosqpub|1740-debian (c1, k60).
1499668529: Sending CONNACK to mosqpub|1740-debian (0, 0)
1499668529: Received PUBLISH from mosqpub|1740-debian (d0, q0, r0, m0, 'sensors/devicename/temperature', ... (5 bytes))
1499668529: Sending PUBLISH to mosqsub|1739-debian (d0, q0, r0, m0, 'sensors/devicename/temperature', ... (5 bytes))
1499668529: Received DISCONNECT from mosqpub|1740-debian
1499668529: Client mosqpub|1740-debian disconnected.

终端 2:

mosquitto_sub -d -t sensors/devicename/temperature

Client mosqsub|1739-debian sending CONNECT
Client mosqsub|1739-debian received CONNACK
Client mosqsub|1739-debian sending SUBSCRIBE (Mid: 1, Topic: sensors/devicename/temperature, QoS: 0)
Client mosqsub|1739-debian received SUBACK
Subscribed (mid: 1): 0
Client mosqsub|1739-debian received PUBLISH (d0, q0, r0, m0, 'sensors/devicename/temperature', ... (5 bytes))
31deg

终端 3:

mosquitto_pub -d -t sensors/devicename/temperature -m 31deg

Client mosqpub|1740-debian sending CONNECT
Client mosqpub|1740-debian received CONNACK
Client mosqpub|1740-debian sending PUBLISH (d0, q0, r0, m1, 'sensors/devicename/temperature', ... (5 bytes))
Client mosqpub|1740-debian sending DISCONNECT

运行时可能遇到的问题及解决办法:

(1)mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

解决办法:

sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1

Eclipse Paho

The Eclipse Paho project provides open-source client implementations of MQTT and MQTT-SN messaging protocols aimed at new, existing, and emerging applications for the Internet of Things (IoT).

项目包含了多种语言的 MQTT 客户端库。

EMQ

EMQ 杭州小莉科技 EMQ 2.0 完整支持 MQTT V3.1/V3.1.1 版本协议规范,并扩展支持 WebSocket、Stomp、CoAP、MQTT-SN 或私有 TCP 协议。EMQ 2.0 消息服务器支持单节点 100 万连接与多节点分布式集群。

VerneMQ

VerneMQ is a high-performance, distributed MQTT broker. It scales horizontally and vertically on commodity hardware to support a high number of concurrent publishers and consumers while maintaining low latency and fault tolerance. VerneMQ is the reliable message hub for your IoT platform or smart products.VerneMQ is based on Erlang OTP, the best technology currently available to build highly scalable messaging systems. This enables VerneMQ to scale horizontally and vertically by fully utilizing multicore architectures.


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|棱讯科技 ( 粤ICP备2024228160号-2|粤公网安备44030002003510号 )

GMT+8, 2024-7-27 13:04 , Processed in 0.017764 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表