
本文共 3826 字,大约阅读时间需要 12 分钟。
目录
Mosquitto是一个开源的C实现的MQTT服务器和客户端。本文介绍Mosquitto在ubuntu18.04系统上的编译,安装,配置和使用。
0 MQTT协议
MQTT协议已经到了5.0版本,每个版本的协议内容可以如下获取:
MQTT 3.1.1
中文版
英文版
MQTT 5.0
中文版
英文版
v3.1.1和v5.0之间的差异
1 编译和安装
1.1 Download source code
libwebsockets: git clone https://libwebsockets.org/repo/libwebsockets
or git clone https://github.com/warmcat/libwebsockets.git
1.2 安装
1.2.1 源码安装
下载
下载解压:tar -xzvf mosquitto-1.6.9.tar.gz
直接在目录下运行: cd mosquitto-1.6.9; make即可
# Comment out to remove publishing of the $SYS topic hierarchy containing# information about the broker state.WITH_SYS_TREE:=yes# Build with systemd support. If enabled, mosquitto will notify systemd after# initialization. See README in service/systemd/ for more information.WITH_SYSTEMD:=no# Build with SRV lookup support.WITH_SRV:=no# Build with websockets support on the broker.WITH_WEBSOCKETS:=no# Use elliptic keys in brokerWITH_EC:=yes# Build man page documentation by default.WITH_DOCS:=yes
如果要修改编译选项, 例如要mosquitto支持websocket
可以修改config.mk,再make
要支持websocket, 编译时会报错:
mosquitto.c:49:12: fatal error: libwebsockets.h: No such file or directory
# include <libwebsockets.h>
这时因为缺少libwebsockets包,需要先安装libwebsockets:
git clone https://libwebsockets.org/repo/libwebsockets
1.2.2 二进制安装
源码安装太繁琐,可以直接二进制安装。
以Ubuntu18.04为例:
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients
1.3 启动运行
Binary:/usr/sbin/mosquito
Configuration file:/etc/mosquitto/mosquitto.conf
Systemd file: /lib/systemd/system/mosquitto.service
Systemd 启动
安装完成后,运行命令systemctl status mosquitto,如果有以下输出,说明安装成功。
> systemctl status mosquitto
● mosquitto.service - Mosquitto MQTT v3.1/v3.1.1 Broker
Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2020-04-20 13:25:31 CST; 4min 24s ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Main PID: 4487 (mosquitto)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/mosquitto.service
└─4487 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
4月 20 13:25:31 robin systemd[1]: Starting Mosquitto MQTT v3.1/v3.1.1 Broker...
4月 20 13:25:31 robin systemd[1]: Started Mosquitto MQTT v3.1/v3.1.1 Broker
手动启动
mosquitto -c /etc/mosquitto/mosquitto.conf -d
-c:指定配置文件
-d:后台运行
2 配置
配置文件放在/etc/mosquitto/目录下。
可以运行: man 5 mosquitto.conf 查看完整的配置说明
配置参数有很多,本人已经翻译成中文, 可以从以下获取完整的中文翻译版本:
2.1安全配置
/etc/mosquitto/conf.d$ 创建 robot.conf
# 禁止匿名访问allow_anonymous false# 认证配置password_file /etc/mosquitto/pwfile# 权限配置acl_file /etc/mosquitto/aclfile
创建密码文件
$ sudo touch /etc/mosquitto/pwfile
向密码文件添加用户
$ sudo mosquitto_passwd /etc/mosquitto/pwfile inspectRobot1
会让你输入两遍密码, 本例输入: foa123
$ sudo mosquitto_passwd -b /etc/mosquitto/pwfile usr1 usr1
$ sudo mosquitto_passwd -b /etc/mosquitto/pwfile usr2 usr2
创建权限配置文件
sudo touch /etc/mosquitto/aclfile
添加以下内容
# usr1只能发布以test为前缀的主题,订阅以$SYS开头的主题即系统主题user usr1topic write test/#topic read $SYS/## usr2只能订阅以test为前缀的主题user usr2topic read test/#
这时运行$ mosquitto_sub -h localhost -t "test/#" -i "client1" 会报以下错误
Connection error: Connection Refused: not authorised.
mosquitto_sub -h localhost -t "test/#" -u inspectRobot1 -P foa123 -i "client1"
mosquitto_pub -h localhost -t "test/abc" -u usr1 -P usr1 -i "client2" -m "How are you?"
3 测试工具
mosquitto_pub: 发布消息
mosquitto_sub:订阅消息
常用参数介绍:
参数 |
描述 |
-h |
服务器主机,默认localhost |
-t |
指定主题 |
-u |
用户名 |
-P |
密码 |
-i |
客户端id,唯一 |
-m |
发布的消息内容 |
mosquitto_sub -h localhost -t "test/#" -u usr1 -P usr1 -i "client1"
mosquitto_sub -h localhost -t "test/#" -i "client1"
mosquitto_pub -h localhost -t "test/abc" -u usr2 -P usr12 -i "client3" -m "How are you?"
mosquitto_pub -h localhost -t "test/abc" -i "client3" -m "How are you?"
发布:
mosquitto_pub -h localhost -t "test/abc" -i "client3" -m "{\"header\":123, \"data\":{\"co\":0.002, \"o2\":23.0}}"
订阅结果为:
{"header":123, "data":{"co":0.002, "o2":23.0}}
Reference:
转载地址:https://blog.csdn.net/lclfans1983/article/details/105694696 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关于作者
