当前位置:网站首页>【MQTT从入门到提高系列 | 01】从0到1快速搭建MQTT测试环境
【MQTT从入门到提高系列 | 01】从0到1快速搭建MQTT测试环境
2022-07-06 06:18:00 【i机器未来】
这是机器未来的第24篇文章
原文首发地址:https://blog.csdn.net/RobotFutures/article/details/125532208

1. mosquitto概述
Eclipse mosquitto是一个开源(EPL/EDL许可的)消息代理,它实现了MQTT协议版本5.0、3.1.1和3.1。mosquito是轻量级的,适用于所有设备,从低功耗的单板计算机到完整的服务器。
MQTT协议提供了使用发布/订阅模型执行消息传递的轻量级方法。这使得它适用于物联网消息传递,如低功耗传感器或移动设备,如手机、嵌入式计算机或微控制器。
mosquito to项目还提供了一个用于实现MQTT客户机的C库,以及非常流行的mosquito to_pub和mosquito to_sub命令行MQTT客户机。
2. 下载MQTT部署软件
下载地址:传送门
3. 快速上手mosquitto
3.1 启动mqtt broker
启动命令行工具,切换到mosquitto安装目录,执行如下命令,启动broker
mosquitto.exe -v
默认开启的端口为1883。
命令详情如下:
mosquitto [-c config file] [ -d | --daemon ] [-p port number] [-v]
-c 指定配置文件路径,默认配置路径为安装目录下的mosquitto.conf
-d 启动时进入后台运行
-p 指定端口,默认为1883
-v 开启日志输出
注意事项:如果不指定配置文件,默认仅支持127.0.0.1的本地回环网卡连接,如果要连接局域网或外网客户端,那么一定要指定配置文件mosquitto.conf:
listener 1883
allow_anonymous true # 允许匿名访问
启动命令如下:
mosquitto.exe -c mosquitto.conf -v
因电脑配置问题,这里使用自定义端口测试
#mosquitto.exe -v -p 6969
1647942133: mosquitto version 2.0.14 starting
1647942133: Using default config.
1647942133: Starting in local only mode. Connections will only be possible from clients running on this machine.
1647942133: Create a configuration file which defines a listener to allow remote access.
1647942133: For more details see https://mosquitto.org/documentation/authentication-methods/
1647942133: Opening ipv4 listen socket on port 6969.
1647942133: Opening ipv6 listen socket on port 6969.
1647942133: mosquitto version 2.0.14 running
3.2 订阅主题
mosquitto_sub.exe -t sensors/temperature -q 1 -p 6969
-t 指定topic,这里为传感器温度值 sensors/temperature
-q 指定qos质量,这里为1
-p 指定端口,这里为6969
命令执行后,broker的日志描述如下:
1647942590: New connection from 127.0.0.1:1746 on port 6969.
1647942590: New client connected from 127.0.0.1:1746 as auto-651BD76A-09A1-67FD-1DF9-AF36BEADB2D0 (p2, c1, k60).
1647942590: No will message specified.
1647942590: Sending CONNACK to auto-651BD76A-09A1-67FD-1DF9-AF36BEADB2D0 (0, 0)
1647942591: Received SUBSCRIBE from auto-651BD76A-09A1-67FD-1DF9-AF36BEADB2D0
1647942591: sensors/temperature (QoS 1)
1647942591: auto-651BD76A-09A1-67FD-1DF9-AF36BEADB2D0 1 sensors/temperature
1647942591: Sending SUBACK to auto-651BD76A-09A1-67FD-1DF9-AF36BEADB2D0
auto-651BD76A-09A1-67FD-1DF9-AF36BEADB2D0 - 连接成功后,broker分配的会话ID
CONNACK:对Connect命令的响应
SUBSCRIBE :客户端发起的订阅请求
SUBACK :broker对客户端auto-651BD76A-09A1-67FD-1DF9-AF36BEADB2D0的SUBSCRIBE命令的反馈
3.3 发布主题
mosquitto_pub.exe -t sensors/temperature -m 32 -q 1 -p 6969
-t 指定topic,这里为传感器温度值 sensors/temperature
-q 指定qos质量,这里为1
-p 指定端口,这里为6969
-m指定消息,这里消息为32
命令执行后:
- broker的日志输出如下
1647942676: New connection from 127.0.0.1:1914 on port 6969.
1647942676: New client connected from 127.0.0.1:1914 as auto-BADB5BE4-8FF4-C9F8-DD05-955CEC58CA3D (p2, c1, k60).
1647942676: No will message specified.
1647942676: Sending CONNACK to auto-BADB5BE4-8FF4-C9F8-DD05-955CEC58CA3D (0, 0)
1647942676: Received PUBLISH from auto-BADB5BE4-8FF4-C9F8-DD05-955CEC58CA3D (d0, q1, r0, m1, 'sensors/temperature', ... (2 bytes))
1647942676: Sending PUBLISH to auto-651BD76A-09A1-67FD-1DF9-AF36BEADB2D0 (d0, q1, r0, m1, 'sensors/temperature', ... (2 bytes))
1647942676: Sending PUBACK to auto-BADB5BE4-8FF4-C9F8-DD05-955CEC58CA3D (m1, rc0)
1647942676: Received PUBACK from auto-651BD76A-09A1-67FD-1DF9-AF36BEADB2D0 (Mid: 1, RC:0)
1647942676: Received DISCONNECT from auto-BADB5BE4-8FF4-C9F8-DD05-955CEC58CA3D
1647942676: Client auto-BADB5BE4-8FF4-C9F8-DD05-955CEC58CA3D disconnected.
- 订阅的客户端日志输出如下:
PS E:\Tools\mosquitto> mosquitto_sub.exe -t sensors/temperature -q 1 -p 6969
32
可以看到,订阅客户端已经收到了发布者发布的消息:温度值32
3.4 简单的订阅/发布工作流

4. 测试过程中遇到的问题
4.1 记录mosquitto端口访问失败调试记录
- 以命令
mosquitto -v启动MQTT broker的端口开放情况如下:
PS X:> netstat -an |findstr "1883"
TCP 127.0.0.1:1883 0.0.0.0:0 LISTENING
TCP [::1]:1883 [::]:0 LISTENING
- 以命令
mosquitto -c .\mosquitto.conf -v启动MQTT broker的端口开放情况如下:
PS C:\Users\25267> netstat -an |findstr "1883"
TCP 0.0.0.0:1883 0.0.0.0:0 LISTENING
TCP 192.168.149.108:1883 192.168.149.162:59826 ESTABLISHED
TCP [::]:1883 [::]:0 LISTENING
可以看到未加载配置文件时,默认仅支持回环网卡。
写在末尾:
- 博客简介:专注AIoT领域,追逐未来时代的脉搏,记录路途中的技术成长!
- 专栏简介:从0到1掌握分布式消息中间件MQTT的使用。
- 面向人群:具备嵌入式开发基础的初级以上程序员
- 专栏计划:接下来会逐步发布跨入人工智能的系列博文,敬请期待
- Python零基础快速入门系列
- 快速入门Python数据科学系列
- 人工智能开发环境搭建系列
- 机器学习系列
- 物体检测快速入门系列
- 自动驾驶模拟器AirSim入门系列
- 自动驾驶物体检测系列
- …

边栏推荐
- Understanding of processes and threads
- 【Postman】Collections配置运行过程
- Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
- leetcode 24. 两两交换链表中的节点
- [C language] string left rotation
- GTSAM中李群的運用
- 测试周期被压缩?教你9个方法去应对
- Idea new UI usage
- Linux regularly backs up MySQL database
- Database isolation level
猜你喜欢

异常检测方法总结

Hypothesis testing learning notes

Full link voltage measurement: building three models

Significance of unit testing

B - The Suspects

【C语言】字符串左旋

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

MFC关于长字符串unsigned char与CString转换及显示问题
![[postman] collections configuration running process](/img/09/bcd9fd6379fa724671ffd09278442e.png)
[postman] collections configuration running process

把el-tree选中的数组转换为数组对象
随机推荐
联合索引的左匹配原则
properties文件
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
全程实现单点登录功能和请求被取消报错“cancelToken“ of undefined的解决方法
调用链监控Zipkin、sleuth搭建与整合
Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)
测试周期被压缩?教你9个方法去应对
在线问题与离线问题
(中)苹果有开源,但又怎样呢?
php使用redis实现分布式锁
E - food chain
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
技术分享 | 常见接口协议解析
Simulation volume leetcode [general] 1109 Flight reservation statistics
[no app push general test plan
Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
Eigen sparse matrix operation
JMeter做接口测试,如何提取登录Cookie
What are the test sites for tunnel engineering?
LeetCode 731. 我的日程安排表 II