00. 目录
01. Mosquitto概述
Eclipse Mosquitto是一个开源的(EPL/EDL 许可)消息代理,实现了MQTT的 5.0、3.1.1 和 3.1 版本。Mosquitto是轻量级的,适用于从低功耗的单板计算机到完整服务器的所有设备上使用。
MQTT协议提供了一种使用发布/订阅模型执行消息传递的轻量级方法。这使得它适合于物联网信息传递,例如使用低功率传感器或移动设备,如手机、嵌入式计算机或微控制器。
Mosquitto项目还提供了一个用于实现MQTT客户端的C库,以及非常流行的 Mosquitto_pub 和 Mosquitto_sub 命令行MQTT客户端。
02. Mosquitto下载
GitHub下载:https://github.com/eclipse/mosquitto
CSDN下载:https://download.csdn.net/download/dengjin20104042056/87893294
旧版本下载:https://mosquitto.org/files/
03. Mosquitto安装
3.1 解压
deng@local:~/sz01$ tar -xzvf mosquitto-2.0.15.tar.gz
3.2 进入mosquitto目录
deng@local:~/sz01$ cd mosquitto-2.0.15/
3.3 执行make命令
deng@local:~/sz01/mosquitto-2.0.15$ make
3.4 执行make install命令
deng@local:~/sz01/mosquitto-2.0.15$ sudo make install
3.5 查看是否安装成功
deng@local:~/sz01/mosquitto-2.0.15$ mosquitto --help
mosquitto version 2.0.15
mosquitto is an MQTT v5.0/v3.1.1/v3.1 broker.
Usage: mosquitto [-c config_file] [-d] [-h] [-p port]
-c : specify the broker config file.
-d : put the broker into the background after starting.
-h : display this help.
-p : start the broker listening on the specified port.
Not recommended in conjunction with the -c option.
-v : verbose mode - enable all logging types. This overrides
any logging options given in the config file.
See https://mosquitto.org/ for more information.
deng@local:~/sz01/mosquitto-2.0.15$
04. Mosquitto应用
4.1 mosquitto服务端
deng@local:~$ mosquitto --help
mosquitto version 2.0.15
mosquitto is an MQTT v5.0/v3.1.1/v3.1 broker.
Usage: mosquitto [-c config_file] [-d] [-h] [-p port]
-c : specify the broker config file.
-d : put the broker into the background after starting.
-h : display this help.
-p : start the broker listening on the specified port.
Not recommended in conjunction with the -c option.
-v : verbose mode - enable all logging types. This overrides
any logging options given in the config file.
4.2 mosquitto_sub
deng@local:~$ mosquitto_sub --help
mosquitto_sub is a simple mqtt client that will subscribe to a set
of topics and print all messages it receives.
mosquitto_sub version 2.0.15 running on libmosquitto 2.0.15.
Usage: mosquitto_sub {[-h host] [--unix path] [-p port] [-u username] [-P password] -t topic | -L URL [-t topic]}
[-c] [-k keepalive] [-q qos] [-x session-expiry-interval]
[-C msg_count] [-E] [-R] [--retained-only] [--remove-retained] [-T filter_out] [-U topic ...]
[-F format]
[-W timeout_secs]
[-A bind_address] [--nodelay]
[-i id] [-I id_prefix]
[-d] [-N] [--quiet] [-v]
[--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]
[{--cafile file | --capath dir} [--cert file] [--key file]
[--ciphers ciphers] [--insecure]
[--tls-alpn protocol]
[--tls-engine engine] [--keyform keyform] [--tls-engine-kpass-sha1]]
[--tls-use-os-certs]
[--psk hex-key --psk-identity identity [--ciphers ciphers]]
[--proxy socks-url]
[-D command identifier value]
mosquitto_sub --help
4.3 mosquitto_pub
deng@local:~$ mosquitto_pub --help
mosquitto_pub is a simple mqtt client that will publish a message on a single
topic and exit.
mosquitto_pub version 2.0.15 running on libmosquitto 2.0.15.
Usage: mosquitto_pub {[-h host] [--unix path] [-p port] [-u username] [-P password] -t topic | -L URL}
{-f file | -l | -n | -m message}
[-c] [-k keepalive] [-q qos] [-r] [--repeat N] [--repeat-delay time] [-x session-expiry]
[-A bind_address] [--nodelay]
[-i id] [-I id_prefix]
[-d] [--quiet]
[-M max_inflight]
[-u username [-P password]]
[--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]
[{--cafile file | --capath dir} [--cert file] [--key file]
[--ciphers ciphers] [--insecure]
[--tls-alpn protocol]
[--tls-engine engine] [--keyform keyform] [--tls-engine-kpass-sha1]]
[--tls-use-os-certs]
[--psk hex-key --psk-identity identity [--ciphers ciphers]]
[--proxy socks-url]
[--property command identifier value]
[-D command identifier value]
mosquitto_pub --help
4.5 先运行 Mosquitto 的 MQTT 代理程序
deng@local:~$ mosquitto
1686453357: mosquitto version 2.0.15 starting
1686453357: Using default config.
1686453357: Starting in local only mode. Connections will only be possible from
clients running on this machine.
1686453357: Create a configuration file which defines a listener to allow remote access.
1686453357: For more details see https://mosquitto.org/documentation/authentication-methods/
1686453357: Opening ipv4 listen socket on port 1883.
1686453357: Opening ipv6 listen socket on port 1883.
1686453357: mosquitto version 2.0.15 running
4.6 运行 Mosquitto 的 MQTT 订阅主题的客户端程序 mosquitto_sub订阅消息
deng@local:~$ mosquitto_sub -t 'deng' -v
其中-t
表示订阅主题(topic),这里的主题为deng
,-v
表示打印消息。
4.7 运行 Mosquitto 的 MQTT 发布消息的客户端程序 mosquitto_pub 发布消息
deng@local:~$ mosquitto_pub -t 'deng' -m 'hello shenzhen'
其中-t
表示指定的主题(topic),这里要发布到主题deng
,-m
表示要发布的消息内容hello shenzhen
。
05. 问题讨论
5.1 执行mosquitto_sub或者mosquitto_pub的时候出现libmosquitto.so.1找不到的问题
deng@local:~/openssl$ 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
如果出现如下问题找不到动态库:
则执行命令创建一个软链接:
sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1