219mze 发表于 2024-10-3 12:53:14

物联网基本:运用EMQ搭建MQTT服务器


    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">1 EMQ介绍</span></span></strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EMQ (Erlang/Enterprise/Elastic MQTT Broker) 是基于 Erlang/OTP 平台<span style="color: black;">研发</span>的开源物联网 MQTT <span style="color: black;">信息</span>服务器。Erlang/OTP 是出色的软实时(Soft-Realtime)、低延时(Low-Latency)、分布式(Distributed) 的语言平台。MQTT 是轻量的(Lightweight)、发布订阅模式(PubSub) 的物联网<span style="color: black;">信息</span>协议</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EMQ 项目设计<span style="color: black;">目的</span>是承载移动终端或物联网终端海量 MQTT 连接,并实<span style="color: black;">此刻</span>海量物联网设备间快速低延时<span style="color: black;">信息</span>路由:</p>稳定承载大规模的 MQTT 客户端连接,单服务器节点支持50万到100万连接。分布式节点集群,快速低延时的<span style="color: black;">信息</span>路由,单集群支持1000万规模的路由。<span style="color: black;">信息</span>服务器内扩展,支持定制多种认证方式、<span style="color: black;">有效</span>存储<span style="color: black;">信息</span>到后端数据库。完整物联网协议支持,MQTT、MQTT-SN、CoAP、WebSocket 或私有协议支持。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EMQ官网:https://www.emqx.io/cn</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">2 准备工作</span></span></strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">搭建MQTT服务器之前,需要先下载EMQ软件包,个人<span style="color: black;">运用</span><span style="color: black;">选取</span>EMQ X Broker,EMQ X Broker 是基于高并发的 Erlang/OTP 语言平台<span style="color: black;">研发</span>,支持百万级连接和分布式集群架构,发布订阅模式的开源 MQTT <span style="color: black;">信息</span>服务器</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">进入官网下载:https://www.emqx.io/cn/downloads#broker</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">选取</span>版本,软件包,笔者下载的是V2.3.11,软件包<span style="color: black;">选取</span>Window,Window10的压缩包</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/5f1b94e83cb0436ba36458f08d860d22~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=NgasToLVyOewgxEy29s1TAdNFbs%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下载完成解压,打开emqttd文件夹,软件包里即<span style="color: black;">包括</span>如下文件(<span style="color: black;">重视</span>:EMQ的下载存放路径别有中文):</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/a2028c2a1a4b4ba5ac52f69a80d95288~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=MVK4SmRrrSmnw9%2Bc0gqC%2FSnexgs%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EMQ 2.0用户手册:https://docs.emqx.net/broker/v2/cn/</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">3 安装EMQ</span></span></strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>管理员身份运行cmd命令窗口,<span style="color: black;">转</span>到emqttd的存放路径下/bin文件夹路径</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/bd44700ee24c4aeba04189ff22bc27f4~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=PsC9tO9fElM%2FgHFC5ijZRdDybps%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输入命令 emqttd install ,安装emq</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/1241ae525b2c461b9eb034ea559cad40~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=MPSTaEwqtGMA1I6fCgmhyQEy7TE%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输入命令 emqttd start ,<span style="color: black;">起步</span>emq,<span style="color: black;">第1</span>次安装<span style="color: black;">起步</span>emq时可能会卡住,<span style="color: black;">不碍事</span>,关闭命令窗口,再次<span style="color: black;">起步</span>emq,当<span style="color: black;">显现</span>以下界面时候即<span style="color: black;">暗示</span>已成功<span style="color: black;">起步</span>emq</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/pgc-image/15d2228950574021bb2081ee69cdc5d5~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=m0ZByooWcDlOspk%2FenrdFVt8Rqs%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EMQ <span style="color: black;">信息</span>服务器<span style="color: black;">起步</span>后,会默认加载 Dashboard 插件,<span style="color: black;">起步</span> Web 管理<span style="color: black;">掌控</span>台。可<span style="color: black;">经过</span> Web <span style="color: black;">掌控</span>台,查看服务器运行状态、统计数据、客户端(Client)、会话(Session)、主题(Topic)、订阅(Subscription)、插件(Plugin)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">掌控</span>台<span style="color: black;">位置</span>: http://127.0.0.1:18083,默认用户: admin,<span style="color: black;">秘码</span>:public</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">尝试登录<span style="color: black;">掌控</span>台,验证 EMQ <span style="color: black;">是不是</span>成功<span style="color: black;">起步</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/d14bc7e6c05c483ebe6addebf8b6e781~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=nvJbuYl7JbNE748Iq%2FxGYgTxsA0%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/863e1bdd6e06449e9fb8b3baad6f29a0~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=DTgGETawyMdac9ST7aI3JbhgX3Y%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">能进入<span style="color: black;">掌控</span>台,即<span style="color: black;">暗示</span>EMQ安装成功</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">4 添加用户</span></span></strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EMQ 2.0 支持丰富的扩展插件,<span style="color: black;">包含</span><span style="color: black;">掌控</span>台、扩展模块、多种认证方式、多种接入协议等:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/1c1092a7647d4261b0e1e30936c04c52~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=gh3I2wXBsAN8q9OjSMAGUJr5evo%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span>在 Web <span style="color: black;">掌控</span>台查看各个插件的运行<span style="color: black;">状况</span>:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/9b85f04a266e438eae49c013a2e8a220~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=DdwWxgyvkEeb38UaTraiISxg9BU%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">用户名、<span style="color: black;">秘码</span>认证插件(emq_auth_username)默认是停止运行的,<span style="color: black;">咱们</span>需要将其更改为<span style="color: black;">起步</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/9cae9cbe49444e1e964411edffc15c18~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=VyMTDx9IEbZetybmzAV6LMahtlw%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在命令行窗口<span style="color: black;">运用</span> emqttd_ctl users add 命令添加用户,添加一个用户名和<span style="color: black;">秘码</span>均为test的用户做MQTT测试,可<span style="color: black;">运用</span>命令 emqttd_ctl users list 查看用户列表验证<span style="color: black;">是不是</span>成功添加用户</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/76ffde7d03864685a6ed38e3f79ae211~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=N0PslmFBK5MTIlDMnz95zLhDKiI%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">4 数据交互测试</span></span></strong></h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">4.1 MQTTBox连接</strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>MQTTBox工具测试 EMQ MQTT服务器<span style="color: black;">是不是</span>能正常通信,自定义发布主题和订阅主题,得到MQTTBox连接信息如下:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">连接信息</span></strong> <strong style="color: blue;"><span style="color: black;">参数</span></strong> <span style="color: black;">位置</span> 127.0.0.1 <span style="color: black;">端口 1883 </span>用户名 test <span style="color: black;"><span style="color: black;">秘码</span> test </span>发布主题 publish_test<span style="color: black;">订阅主题 subscribe_test</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/1a1deb4ca6704966a0b442a83406a5a6~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=%2BPIMYRmurFEMAAFvwJF0tjiNBC0%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/5e38501a4d284ef997d050debda5d3bf~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=BV6B3V5ZxdTHTZ%2Fe9e3UdjBG7%2Fs%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MQTTBox连接上EMQ MQTT服务器,且订阅subscribe_test主题</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">4.2 Websocket 连接</strong></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EMQ 2.0 <span style="color: black;">信息</span>服务器默认占用的 TCP 端口<span style="color: black;">包含</span>:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">端口</span></strong> <strong style="color: blue;"><span style="color: black;">用途</span></strong> 1883 MQTT 协议端口 <span style="color: black;">8883 MQTT/SSL 端口 </span>8083 MQTT/WebSocket 端口<span style="color: black;">8080 HTTP API 端口 </span>18083 Dashboard 管理<span style="color: black;">掌控</span>台端口</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">故<span style="color: black;">咱们</span>用WebSocket做MQTT连接时端口应该为8083,而不是1883,故得到WebSocket连接信息如下:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">连接信息</span></strong> <strong style="color: blue;"><span style="color: black;">参数</span></strong> <span style="color: black;">位置</span> 127.0.0.1 <span style="color: black;">端口 8083 </span>用户名 test <span style="color: black;"><span style="color: black;">秘码</span> test</span>发布主题 subscribe_test <span style="color: black;">订阅主题 publish_test</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/535e0212c80c43ffaa6c5b746e5bb0b0~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728099184&amp;x-signature=xIyWIEuL30x5wHKDJi%2B1meVuzq4%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">WebSocket连接上EMQ MQTT服务器,且订阅publish_test主题</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;">4.3 信息交互测试</strong></h1>MQTTBox发布一条主题为publish_test的<span style="color: black;">信息</span>,<span style="color: black;">能够</span>在EMQ WebSocket上看到发布的<span style="color: black;">信息</span>WebSocket发布一条主题为subscribe_test的<span style="color: black;">信息</span>,<span style="color: black;">能够</span>在MQTTBox上看到发布的<span style="color: black;">信息</span> EMQ MQTT服务器能正常接收、发送数据,正常通信,<span style="color: black;">运用</span>EMQ搭建MQTT服务器成功<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">重视</span>:此处搭建的MQTT服务器只能<span style="color: black;">运用</span>局域网通信进行数据传输</strong></p>




4lqedz 发表于 2024-11-10 10:26:14

我完全赞同你的观点,思考很有深度。

nqkk58 发表于 2024-11-11 10:08:55

楼主果然英明!不得不赞美你一下!
页: [1]
查看完整版本: 物联网基本:运用EMQ搭建MQTT服务器