陈拓 2021/11/07-2021/11/08
1. 概述
本文我们下载最新的mosquitto 2.x源码进行编译安装。并且用源码编译安装后系统中就有了用C语言进行MQTT开发相关的头文件和库文件。
首先登录树莓派。见《在树莓派计算模块CM4上安装Ubuntu Server 20.04》https://blog.youkuaiyun.com/chentuo2000/article/details/121419415
查看操作系统版本:
2. 下载源码
2.1 官方网址
https://mosquitto.org/download/
2.2 下载源码
右击mosquitto-2.0.14.tar.gz,复制链接地址:
https://mosquitto.org/files/source/mosquitto-2.0.14.tar.gz
- 创建工作目录
mkdir ~/ct
cd ~/ct
- 下载
wget https://mosquitto.org/files/source/mosquitto-2.0.14.tar.gz --no-check-certificate
3. 解压
tar -zxvf mosquitto-2.0.14.tar.gz
4. 编译
- 安装编译工具
安装全家桶build-essential(C/C++ toolchain包括make、gcc等)
sudo apt install build-essential
- 进入源码目录
cd mosquitto-2.0.14
- 编译
make
出错,找不到openssl/opensslconf.h
安装opensslconf.h
sudo apt-get install libssl-dev
- 再编译
make
出错,找不到cjson/cJSON.h
安装libcjson libcjson-dev
sudo apt-get install libcjson1 libcjson-dev
- 再编译
make
成功。
- 安装
sudo make install
- 重新启动树莓派
sudo reboot
- 查看帮助
mosquitto -h
mosquitto是一个MQTT v5.0/v3.1.1/v3.1代理。
用法:
Usage: mosquitto [-c config_file] [-d] [-h] [-p port]
参数说明:
-c : 指定代理配置文件
-d : 代理启动之后进入后台运行
-h : 显示帮助
-p : 以指定的端口启动代理监听。不建议与-c选项一起使用
-v : 详细模式 - 启用所有日志类型,并在终端打印调试信息。此参数覆盖在配置文件中给出的任何日志记录选项。
更多信息见https://mosquitto.org/
5. 本地测试
下面开三个终端窗口进行测试。
5.1 启动mosquitto服务
- 启动mosquitto服务
先打开一个终端窗口,输入mosquitto -v
使用默认配置,以本地模式启动,只能从运行在本机上的客户端连接。如果要从远程访问,需要创建一个配置文件,我们将在后面创建这个配置文件并测试远程连接。
5.2 打开第二个终端窗口,订阅主题
- 订阅主题
mosquitto_sub -d -v -t test_topic
出错,原因是libmosquitto.so.1安装在/usr/local/lib/目录下,默认的库文件在目录/usr/lib/下,创建一个软链接:
sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
- 重新订阅主题
mosquitto_sub -d -v -t test_topic
订阅主题test_topic,等待接收消息。
参数说明查看帮助:mosquitto_sub --help
-d : enable debug messages.
-v : print published messages verbosely.
-t : mqtt topic to subscribe to. May be repeated multiple times.
5.3 打开第三个终端窗口,发布内容
- 发布内容
mosquitto_pub -t test_topic -m "hello!"
发布主题为test_topic的消息Hello!
参数说明查看帮助:mosquitto_pub --help
-m : message payload to send.
- 订阅窗口显示接收到的消息
6. 设置配置文件
使用默认配置,如需修改服务器的配置信息需要修改:
上面的测试使用默认配置,只能从运行在本机上的客户端连接。如果要从远程访问,需要创建一个配置文件。
- 重新启动树莓派
sudo reboot
或者杀掉在后台运行的进程mosquitto服务和订阅进程:
- 复制mosquitto.conf
sudo cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
- 编辑/etc/mosquitto/mosquitto.conf
sudo nano /etc/mosquitto/mosquitto.conf
我们修改下面几处,去掉注释符号,修改默认值(注意和1.x旧版本不完全相同):
user mosquitto
max_queued_messages 1000
max_packet_size 1024
allow_zero_length_clientid true
listener 1883
autosave_interval 1800
autosave_on_changes false
persistence false
persistence_file mosquitto.db
allow_anonymous false
Password_file /etc/mosquitto/passwd.conf
-
设置密码文件passwd.conf
为mosquitto_passwd命令创建软链接:
sudo ln -s /usr/local/bin/mosquitto_passwd /usr/lib/mosquitto_passwd
创建密码文件
sudo mosquitto_passwd -c /etc/mosquitto/passwd.conf ct
创建一个名为ct用户。
输入两遍密码
7. 指定配置文件启动mosquitto
- 指定配置文件启动mosquitto
需要调试时执行命令:
mosquitto -c /etc/mosquitto/mosquitto.conf -v
可以在窗口查看mosquitto运行时的信息。
正式运行时执行命令:
mosquitto -d -c /etc/mosquitto/mosquitto.conf
-d后台运行。
- 查看进程
ps -ef | grep mosquitto
8. 带账号密码认证的本地测试
打开两个终端,测试publish 和subscribe。
- 一个终端订阅subscribe主题Topic:test,主题可以加双引号,也可以不加。
mosquitto_sub -p 1883 -u ct -P 12345678 -t "test"
等待接收消息。
- 另一个终端发布主题为test的消息Hello!
mosquitto_pub -p 1883 -u ct -P 12345678 -t test -m "Hello!"
- 订阅窗口显示接收到的消息
9. 远程测试
详细说明见《树莓派MQTT服务远程测试MQTT.fx》
https://zhuanlan.zhihu.com/p/363373024
https://blog.youkuaiyun.com/chentuo2000/article/details/115539377
10. 头文件和库文件
用C语言开发MQTT应用程序所需的头文件和库文件现在都有了。
C/C++ libraries for creating MQTT clients.
- lib/libmosquitto.so.1
- lib/cpp/libmosquittopp.so.1
- include/mosquitto.h
- include/mqtt_protocol.h
10.1 头文件
find ~ -name mosquitto.h
find ~ -name mqtt_protocol.h
查看默认目录:
10.2 库文件
find ~ -name libmosquitto.so.1
find ~ -name libmosquittopp.so.1
在默认目录下查看:
11. 开机启动mosquitto
- 修改rc-local.service文件
ls -l /lib/systemd/system/rc-local.service
sudo nano /lib/systemd/system/rc-local.service
将下列内容复制进 rc-local.service文件的最后面加上:
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
- 创建文件rc.local文件
sudo nano /etc/rc.local
- 向文件rc.local写入
#!/bin/sh
mosquitto -d -c /etc/mosquitto/mosquitto.conf
exit 0
- 给rc.local加上可执行权限
sudo chmod +x /etc/rc.local
- 重启
sudo reboot
- 检查服务运行状态
systemctl status rc-local.service
服务已启动。
- 查看mosquitto进程
ps -ef | grep mosquitto
进程已启动。