【学习笔记】Mosquitto 基础安装使用

【学习笔记】Mosquitto 基础安装与使用

简介

记录学习 Mosquitto 基础安装与使用的一些内容。

Mosquitto 是一个开源 MQTT 代理,官网的简介如下:

Eclipse Mosquitto是一个开源的(EPL/EDL 许可)消息代理,实现了MQTT的 5.0、3.1.1 和 3.1 版本。Mosquitto是轻量级的,适用于从低功耗的单板计算机到完整服务器的所有设备上使用。

MQTT协议提供了一种使用发布/订阅模型执行消息传递的轻量级方法。这使得它适合于物联网信息传递,例如使用低功率传感器或移动设备,如手机、嵌入式计算机或微控制器。

Mosquitto项目还提供了一个用于实现MQTT客户端的C库,以及非常流行的 Mosquitto_pub 和 Mosquitto_sub 命令行MQTT客户端。

下载

官网地址:https://mosquitto.org/

下载地址:https://mosquitto.org/download/

github 地址:https://github.com/eclipse/mosquitto

安装

使用树莓派平台下载源码mosquitto-2.0.15.tar.gz

【学习笔记】Mosquitto 基础安装使用_第1张图片

mosquitto-2.0.15.tar.gz文件解压:

pi@raspberrypi:~/Desktop/mqtt $ tar -zxvf mosquitto-2.0.15.tar.gz 

直接make编译:

pi@raspberrypi:~/Desktop/mqtt/mosquitto-2.0.15 $ make

如果出现错误,缺少 cjson 库:

cc db_dump.o print.o memory_mosq.o memory_public.o packet_datatypes.o packet_mosq.o persist_read.o persist_read_v234.o persist_read_v5.o property_mosq.o send_disconnect.o stubs.o time_mosq.o topic_tok.o utf8_mosq.o -o mosquitto_db_dump  
make[2]: 离开目录“/home/pi/Desktop/mqtt/mosquitto-2.0.15/apps/db_dump”
make[2]: 进入目录“/home/pi/Desktop/mqtt/mosquitto-2.0.15/apps/mosquitto_ctrl”
cc -I../mosquitto_passwd -DWITH_CJSON  -I. -I../../ -I../../include -I../../src -I../../lib -DWITH_TLS -Wall -ggdb -O2 -Wconversion -Wextra -DVERSION=\""2.0.15\"" -c mosquitto_ctrl.c -o mosquitto_ctrl.o
In file included from mosquitto_ctrl.c:19:
../../config.h:86:12: fatal error: cjson/cJSON.h: 没有那个文件或目录
 #  include 
            ^~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [Makefile:51:mosquitto_ctrl.o] 错误 1
make[2]: 离开目录“/home/pi/Desktop/mqtt/mosquitto-2.0.15/apps/mosquitto_ctrl”
make[1]: *** [Makefile:9:all] 错误 2
make[1]: 离开目录“/home/pi/Desktop/mqtt/mosquitto-2.0.15/apps”
make: *** [Makefile:66:mosquitto] 错误 2

则需先安装下 cjson,github 地址:https://github.com/DaveGamble/cJSON

pi@raspberrypi:~/Desktop/mqtt $ git clone https://github.com/DaveGamble/cJSON.git
正克隆到 'cJSON'...
remote: Enumerating objects: 4545, done.
remote: Total 4545 (delta 0), reused 0 (delta 0), pack-reused 4545
接收对象中: 100% (4545/4545), 2.45 MiB | 40.00 KiB/s, 完成.
处理 delta 中: 100% (3026/3026), 完成.
pi@raspberrypi:~/Desktop/mqtt $ cd cJSON/
pi@raspberrypi:~/Desktop/mqtt/cJSON $ make
pi@raspberrypi:~/Desktop/mqtt/cJSON $ sudo make install

编译好之后,执行make install命令安装编译后的软件到系统中。

pi@raspberrypi:~/Desktop/mqtt/mosquitto-2.0.15 $ sudo make install 

测试

先运行 Mosquitto 的 MQTT 代理程序:

pi@raspberrypi:~/Desktop/mqtt/mosquitto-2.0.15 $ mosquitto
1679985796: mosquitto version 2.0.15 starting
1679985796: Using default config.
1679985796: Starting in local only mode. Connections will only be possible from clients running on this machine.
1679985796: Create a configuration file which defines a listener to allow remote access.
1679985796: For more details see https://mosquitto.org/documentation/authentication-methods/
1679985796: Opening ipv4 listen socket on port 1883.
1679985796: Opening ipv6 listen socket on port 1883.
1679985796: mosquitto version 2.0.15 running

然后运行 Mosquitto 的 MQTT 订阅主题的客户端程序 mosquitto_sub:

pi@raspberrypi:~ $ mosquitto_sub -t 'test' -v

其中-t表示订阅主题(topic),这里的主题为test-v表示打印消息。

如果出现如下问题找不到动态库:

pi@raspberrypi:~ $ mosquitto_sub -t 'test' -v
mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

则执行命令创建一个软链接:

pi@raspberrypi:~ $ sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1

接着再运行 Mosquitto 的 MQTT 发布消息的客户端程序 mosquitto_pub 发布消息:

pi@raspberrypi:~ $ mosquitto_pub -t 'test' -m 'hello'

其中-t表示指定的主题(topic),这里要发布到主题test-m表示要发布的消息内容hello

然后在订阅主题的客户端就能看到如下消息:

test hello

[参考资料]

Eclipse Mosquitto

Download


本文链接:https://blog.csdn.net/u012028275/article/details/129818526

你可能感兴趣的:(学习笔记,学习,eclipse,mqtt,网络,c语言)