智能家居

MQTT之客户端工具

2018-06-13  本文已影响856人  梁睿坤

MQTT Spy

mqtt-spy_v0.5.3

这是一个用java开发的开源MQTT客户端,可以到他们的GitHub上下载,用起来感觉得行但是如果机器上没有安装好java的话就会非常坑,经常会运行不了。如果想省事的朋友就不太推荐此工具。

HiveMQ

HiveMQ是一个实时的MQTT服务的一个网站,他们既提供MQTT信息中转服务也提供一个客户端工具,http://www.mqtt-dashboard.com/ 是实时MQTT中转服务的面板,里面有MQTT服务的基本配置,如下图所示:

HiveMQ服务端

如果要使用HiveMQ的客户端工具的话,可以访问 http://www.hivemq.com/demos/websocket-client/ 。这是一个在线的MQTT客户端,它是采用WebSocket与MQTT进行通信的,所以他使用的端是8000。

HiveMQ

命令行工具 mosquitto

这是一个非常有效简单的实用工具,在macOS下可以按以下方式安装

$ brew install mosquitto

这个工具主要提供以下两个命令:

订阅命令 - mosquitto_sub

命令格式

Usage: mosquitto_sub [-c] [-h host] [-k keepalive] [-p port] [-q qos] [-R] -t topic ...
                     [-C msg_count] [-T filter_out]
                     [-A bind_address] [-S]
                     [-i id] [-I id_prefix]
                     [-d] [-N] [--quiet] [-v]
                     [-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]]
                     [--psk hex-key --psk-identity identity [--ciphers ciphers]]
                     [--proxy socks-url]
$ mosquitto_sub -h broker.mqttdashboard.com -t growthai/ray/mqtt/#

键入以上命令之后终端会进入一个等待状态,所有来自growthai/ray/mqtt/#主题的信息都会打印到终端上。

在HiveMQ的客户端上发一个中文消息到growthai/ray/mqtt/

以下是终端收到信息的效果

发布命令 - mosquitto_pub

Usage: mosquitto_pub [-h host] [-k keepalive] [-p port] [-q qos] [-r] {-f file | -l | -n | -m message} -t topic
                     [-A bind_address] [-S]
                     [-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]]
                     [--psk hex-key --psk-identity identity [--ciphers ciphers]]
                     [--proxy socks-url]

选项:

我们可以先在HiveMQ上订阅growthai/raymqtt/#这个主题,质量为Qos0,然后在终端向这个主题发送一条信息:

$ mosquitto_pub -h broker.mqttdashboard.com -t growthai/ray/mqtt/ -m 'hi i am ray'

然后我们可以看到在HiveMQ上收到的主题信息:

HiveMQ和mosquitto 命令行工具对于做IoT开来说都是非常轻量型的,而且使用也非常方便。

上一篇 下一篇

猜你喜欢

热点阅读