【问题标题】:bridge local mosquitto to cloud broker将本地 mosquitto 桥接到云代理
【发布时间】:2023-10-12 00:22:01
【问题描述】:

我正在尝试将本地 mosquitto(在树莓派上)连接到某个云代理,以便我可以发送数据并使用该数据控制某些设备。我尝试使用 Cloudmqtt 和 dioty,但没有成功。在 cloudmqtt 的情况下,我被告知 mosquitto.conf 文件一切正常,对于 dioty,我被告知它使用不支持桥接的 Mosca 代理(我被告知我指的是 cloudmqtt 和 dioty支持服务)。有谁知道一些可以桥接到本地 mosquitto 的远程代理,有人已经尝试过吗?

我考虑使用一些 IOT 平台来执行此操作,但我已经阅读了该平台使用定价,我这样做是为了学习和爱好,所以在我学习如何使用它之前,我想保持免费帐户。

编辑: 我正在使用 mosquitto 1.4.5,mosquitto.conf 文件如下:

#Place your local configuration in /etc/mosquitto/conf.d/
#
#A full description of the configuration file is at
#/usr/share/doc/mosquitto/examples/mosquitto.conf.example

connection cloudmqtt
address mnumber.cloudmqtt.com:port
topic zaESP8266 in 0
topic ESP8266 out 0
try_private true
notifications false
start_type automatic
remote_usename username_of_the_user_on_cloudmqtt
remote_password password_of the_user_on_cloudmqtt

pid_file /var/run/mosquitto.pid

persistance true
persistance_location /var/lib/mosquitto/

我之所以选择 Cloudmqtt 作为在线蚊子代理是因为我认为本地蚊子 远程蚊子桥应该可以工作。我可以使用笔记本电脑或手机等客户端,并使用它们与本地代理发布和订阅消息。这样,即使我无法访问互联网,我也可以让本地代理与本地客户进行交互。

编辑: 我配置了 mosquitto.conf 文件,以便可以与 test.mosquitto.org 桥接,我添加了以下几行:

connection test
address test.mosquitto.org
topic in_topic in 0
topic out_topic out 0
try_private false
notifications false
bridge_attempt_unsubscribe true

现在,在一个终端中我可以使用mosquitto_sub -t in_topic,而在另一个终端中我可以使用mosquitto_pub -h test.mosquitto.org -t in_topic -m message。我在使用 mosquitto_sub 命令的第一个终端中收到该消息。因此,在此之后,我认为桥梁运作良好是正确的。但我无法弄清楚桥接到 cloudmqtt 似乎有什么问题。

Cloudmqtt 使用 mosquitto mqtt 代理,dioty 从 mosquitto 交叉到 Mosca mqtt 代理,不支持桥接但显然具有更好的可扩展性(此信息已由 dioty 客户服务提供给我)

编辑: 我使用 Node-RED 解决了这个桥接问题。只需添加一个 mqqt 输入并将其配置为本地 mosquitto 代理(端口 1883)和两个 mqtt 输出,用于 ​​cloudmqtt 和 DIoTY。

在cloudmqtt上,注册账号后(萌猫免费),进入控制面板添加cloudmqtt实例,cloudmqtt会提供用户名和密码。在 Node-RED 中,通过添加服务器、用户名、密码和端口,为 cloudmqtt 实例配置一个 mqtt 输出。

在 DIoTY 上,您还需要创建帐户或使用 google 或其他帐户登录。当您登录时,您将获得用户名,这将是您的邮件帐户以及其他信息,例如主机和端口,密码将发送到电子邮件帐户。因此,要在 Node-RED 中使用它,您需要将主机(服务器)、用户名、密码和端口添加到 mqtt 输出。当您订阅或发布消息时,请记住您有根主题,例如:

/your_mail_account/主题

无论是订阅某个主题还是发布某些消息,您都需要将根主题添加为某种前缀。

完成此配置后,您需要部署更改,就是这样。所以对于这种我使用了 Node-RED。

【问题讨论】:

    标签: cloud local mqtt mosquitto bridge


    【解决方案1】:

    您可以尝试将 test.mosquitto.org 作为远程代理。

    为了改善您的问题,请详细说明您正在使用哪个版本的 mosquitto,并发布您使用的确切 mosquitto.conf 设置 - 当然要删除凭据。

    【讨论】:

    • 我认为 test.mosquitto.org 不适合这个,因为它不使用任何类型的安全认证。我在原始问题中提供了 mosquitto 版本和 mosquitto.conf 文件。
    • 我的意思是你可以尝试 test.mosquitto.org 作为测试的替代方案,而不是作为你的最终解决方案。许多人连接到 test.mosquitto.org,所以如果你不能,那么问题就是你的目的。在这种情况下,您无需进行身份验证。
    • 我编辑了原始问题,我为桥接 test.mosquitto.org 配置了 mosquitto.conf 文件,一切正常。不知道cloudmqtt应该是什么问题。
    • 我再次通过聊天与 cloudmqtt 客户服务进行了交谈,他们不知道为什么它不起作用。
    【解决方案2】:

    我正在使用 windowsc9.io linux 控制台 进行测试。


    我发现https://www.cloudmqtt.com/docs-faq.html#TLS_SSL

    cloudmqtt.com 服务器证书由 Comodo 签名,它以 AddTrust CA 作为根。大多数操作系统默认都带有它,所以你可以指向你的默认信任/CA存储。

    获取 AddTrust CA 证书的方法

    方法01

    1. 点击开始,点击开始搜索,输入mmc,然后按ENTER。

    2. 文件菜单上,点击添加/删除管理单元

    3. Available snap-ins下,点击Certificates,然后点击Add

    4. 此管理单元将始终管理证书下,单击计算机帐户,然后单击下一步

    5. 点击本地计算机,然后点击完成

    6. 如果您没有要添加到控制台的其他管理单元,请单击确定

    7. 在控制台树中,双击Certificates

    8. 点击受信任的根证书颁发机构 > 证书存储。

    9. 右键单击AddTrust External CA Root

    10. 单击导出以导出证书并按照证书导出向导中的步骤,导出文件格式选择Base-64 编码的X.509(CER) .

    11. ca.cer 保存到 **C:\Program Files\mosquitto**

    方法02

    我在这里获得证书:www.tbs-certificates.co.uk/FAQ/en/357.html

    AddTrust CA 根证书http://www.tbs-x509.com/AddTrustExternalCARoot.crt

    1. 复制AddTrustExternalCARoot.crt到C:\Program Files\mosquitto\
    2. 双击AddTrustExternalCARoot.crt,安装证书...

    将 Mosquitto 桥接到 CloudMQTT

    Windows

    我正在使用 mosquitto 1.4.11windowsmosquitto.conf 文件我使用不同的名称(cloud.conf ),它看起来像这样:

    connection cloudmqtt
    address xxx.cloudmqtt.com:<ssl-port>
    remote_username <username_of_the_user_on_cloudmqtt>
    remote_password <password_of the_user_on_cloudmqtt>
    remote_clientid cloud.mqtt
    bridge_protocol_version mqttv311
    try_private true
    notifications false
    start_type automatic
    topic # both 2
    # Method 01
    bridge_cafile ca.cer
    # Method 02
    #bridge_cafile AddTrustExternalCARoot.crt
    bridge_insecure false
    cleansession false
    local_clientid local.mosquitto
    

    运行cmd.exe

    C:\Program Files\mosquitto> mosquitto.exe -c cloud.conf -v
    
    1490627692: mosquitto version 1.4.11 (build date 20/02/2017 23:24:29.40) starting
    1490627692: Config loaded from cloud.conf.
    1490627692: Opening ipv6 listen socket on port 1883.
    1490627692: Opening ipv4 listen socket on port 1883.
    1490627692: Bridge local.mosquitto doing local SUBSCRIBE on topic #
    1490627692: Connecting bridge cloudmqtt (xxx.cloudmqtt.com:<ssl-port>)
    1490627692: Bridge cloud.mqtt sending CONNECT
    1490627693: Received CONNACK on connection local.mosquitto.
    1490627693: Bridge local.mosquitto sending SUBSCRIBE (Mid: 1, Topic: #, QoS: 2)
    1490627693: Received SUBACK from local.mosquitto
    ...
    

    现在,我的本地蚊子经纪人以 2 路通信方式桥接了 cloudmqtt.com


    Linux

    使用方法02(AddTrustExternalCARoot.crt)在c9.io linux console简单尝试,输出如下。

    $ mosquitto -c cloud.conf -v -p 8080
    1490634446: mosquitto version 1.4.11 (build date Fri, 03 Mar 2017 15:11:39 +0000) starting
    1490634446: Config loaded from cloud.conf.
    1490634446: Opening ipv4 listen socket on port 8080.
    1490634446: Opening ipv6 listen socket on port 8080.
    1490634446: Bridge c9.mosquitto doing local SUBSCRIBE on topic #
    1490634446: Connecting bridge cloudmqtt2c9 (xxx.cloudmqtt.com:<ssl-port>)
    1490634446: Connecting bridge cloudmqtt2c9 (xxx.cloudmqtt.com:<ssl-port>)
    1490634446: Bridge cloud.mqtt.c9 sending CONNECT
    1490634446: Received CONNACK on connection c9.mosquitto.
    1490634446: Bridge c9.mosquitto sending SUBSCRIBE (Mid: 1, Topic: #, QoS: 2)
    1490634447: Received SUBACK from c9.mosquitto
    ...
    

    树莓派

    AddTrustExternalCARoot.crt 复制到与 cloud.conf

    相同的目录
    connection pi2cloudmqtt
    address xxx.cloudmqtt.com:<ssl-port>
    remote_username <username_of_the_user_on_cloudmqtt>
    remote_password <password_of the_user_on_cloudmqtt>
    remote_clientid cloud.mqtt.pi
    bridge_protocol_version mqttv311
    try_private true
    notifications false
    start_type automatic
    topic # both 2
    # Method 01
    #bridge_cafile ca.cer
    # Method 02
    bridge_cafile AddTrustExternalCARoot.crt
    bridge_insecure false
    cleansession false
    local_clientid pi.mosquitto
    

    $ mosquitto -c cloud.conf -v

    【讨论】:

    • 我在打开与网桥的连接时出现证书错误,上面写着'check bridge_cafile "AddTrustExternalCARoot.crt" Exception "STATUS_ACCESS_VIOLATION"