开始学习
Mosquitto
简介
Mosquitto 是一款开源的 MQTT 消息代理(服务器)软件,由 Eclipse 基金会主导开发,采用 EPL(Eclipse Public License 2.0)和 EDL(Eclipse Distribution License 1.0)双重许可。
Mosquitto 提供轻量级的、支持可发布/可订阅的的消息推送模式,使设备对设备之间的短消息通信变得简单,比如现在应用广泛的低功耗传感器,手机、嵌入式计算机、微型控制器等移动设备。
Mosquitto 安装之后默认提供了 mosquitto_pub 和 mosquitto_sub 两个命令行 MQTT 客户端工具。Mosquitto CLI 有多个配置选项,支持 TLS 证书连接、通过代理服务器连接,支持 debug 模式,在 debug 模式下可以获取更详细的消息信息。
- 项目官网:https://mosquitto.org
- GitHub 仓库:https://github.com/eclipse/mosquitto
使用
安装
通过 SSH 登录 Linux 系统(比如树莓派),执行下面命令安装 mosquitto。
安装 mosquitto 服务器(broker)
sudo apt install mosquitto
安装完后会有 mosquitto 和 mosquitto_passwd 两个命令行工具。
安装 mosquitto 客户端
sudo apt install mosquitto-clients
安装完后会有 mosquitto_pub、mosquitto_rr 和 mosquitto_sub 三个命令行工具。
测试
接着再打开另一个 SSH 连接,这里把它称为 SSH2(前面那个终端称为 SSH1),其中 SSH1 作为订阅者(subscriber),SSH2 作为发布者(publisher)。
[SSH1]:订阅 test/topic1 主题
mosquitto_sub -d -t test/topic1
[SSH2]:发布 test/topic1 主题的消息
mosquitto_pub -d -t test/topic1 -m "hello, getiot.tech"
看看 [SSH1] 是否已经收到消息?