【问题标题】:MQTT Socket error on client <unknown>客户端上的 MQTT 套接字错误 <未知>
【发布时间】:2016-02-29 16:32:08
【问题描述】:

我已在 Raspberry Pi 上设置 MQTT 并为代理配置了 Arduino Uno,但我在 /var/log/mosquitto/mosquitto.log 文件中看到以下条目:

New connection from 192.168.10.114 on port 1883.
Socket error on client <unknown>, disconnecting.

Pi 设置了 ETH0 连接到我的本地 LAN,IP 地址为 192.168.1.50

Pi 上还有一个 WiFi AP 设置。 Arduino Uno 通过 WiFi 连接以发送/接收 MQTT 消息。 WiFi AP 的 IP 地址为 192.168.10.1,并通过dnsmasq 提供 DHCP 租约。

我已尝试在本地 MQTT 代理服务器(Pi)上发布和订阅测试并得到相同的错误:

Command:
mosquitto_sub -h 192.168.10.1 -t topic

mosquitto.log:
New connection from 192.168.10.1 on port 1883.
New client connected from 192.168.10.1 as mosqsub/1837-raspberryp (cl, k60).
Socket error on client <unknown>, disconnecting.

这里是/etc/mosquitto/mosquitto.conf:

pid_file /var/run/mosquitto.pid

persistence true
log_dest file /var/log/mosquitto/mosquitto.log

allow_anonymous true

include_dir /etc/mosquitto/conf.d

sudo service mosquitto stop sudo service mosquitto start:

mosquitto version 1.4.8 terminating
mosquitto version 1.4.8 (build date Sun, 14 Feb 2016 15:06:55 +0000) starting
Config loaded from /etc/mosquitto/mosquitto.conf.
Opening ipv4 listen socket on port 1883.
Opening ipv6 listen socket on port 1883.

我的接口配置可能存在问题。这是/etc/network/interfaces:

source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
    address 192.168.10.1
    netmask 255.255.255.0

谁能指出 MQTT 上的套接字错误来自哪里?

【问题讨论】:

  • 您解决了这里的问题吗?由于未知客户端的套接字问题,我遇到了 mosquitto 崩溃

标签: linux raspberry-pi mqtt


【解决方案1】:

我来到这个帖子是因为我遇到了同样的错误。经过更多故障排除后,它与身份验证配置有关 - 客户端(Arduino)试图匿名连接(没有密码),而代理(Pi)设置为仅允许经过身份验证的连接(MQTT 配置中的 allow_anonymous false )。

将正确的密码添加到 Arduino 连接代码为我解决了这个问题。

【讨论】:

    【解决方案2】:

    我在 Arduino 连接到 mosquitto 时遇到了类似问题:

    mosquitto_1  | 1551412354: New connection from 10.0.3.9 on port 1883.
    mosquitto_1  | 1551412354: New client connected from 10.0.3.9 as weather-station (c1, k15).
    mosquitto_1  | 1551412376: Client weather-station has exceeded timeout, disconnecting.
    mosquitto_1  | 1551412376: Socket error on client weather-station, disconnecting.
    mosquitto_1  | 1551412402: New connection from 10.0.3.9 on port 1883.
    mosquitto_1  | 1551412402: New client connected from 10.0.3.9 as weather-station (c1, k15).
    mosquitto_1  | 1551412424: Client weather-station has exceeded timeout, disconnecting.
    mosquitto_1  | 1551412424: Socket error on client weather-station, disconnecting.
    

    最初的连接会工作,然后会遇到频繁的套接字错误,最终完全无法工作。

    经过一番挖掘,我发现我没有调用 PubSubClient 的 loop() 函数。没有这个,连接就不能正常服务,导致超时和套接字错误。尝试将client.loop() 添加到您的loop() 函数中。

    【讨论】:

      【解决方案3】:

      请注意,如果客户端因任何原因失败,MQTT 将引发此错误。我花了几个小时研究 MQTT,认为 pub/sub 机制被破坏了。但相反,问题只是我自己手写的包含 python 断言语句的解组过程中的一个错误。断言失败,杀死客户端,并产生 MQTT 错误,但我的控制台上没有显示断言失败的记录,导致我在花园小路上走了一会儿。原因是我当时执行 sys.exit(-1) ,终止了对控制台的通知。 (即使在原型代码中,最好尽可能做正确的事情,而不是像这样愚蠢的事情!)

      【讨论】:

        【解决方案4】:

        对于匿名连接,我遇到了问题。将“”指定为用户名和密码显然不够匿名。只是不要在连接语句中提及它效果更好。

        //mqtt_user = ""
        //mqtt_password =""
        //if (client.connect(mqtt_clientid, mqtt_user, mqtt_password)) {
        if (client.connect(mqtt_clientid)) {
        

        【讨论】:

          【解决方案5】:

          我错误地在 arduino 上设置了 WifiSSLClient 而不是 WifiClient。 改成无SSL客户端,连接错误消失了。

          我的设置:

          • 客户端:在 Arduino wifi rev 上运行的 arduino MQTT 库 (ArduinoMqttClient.h)。 2
          • MQTT 代理:mosquitto 在树莓派 3 上运行

          【讨论】:

            【解决方案6】:

            即使使用正确的 acl、.conf 和 user_password 文件,我也遇到了类似的问题。问题是在我的 acl 文件中,用户名后面有一个额外的空格字符,这似乎被认为是名称的一部分,因此是预期的。这很难发现,因为空间是看不见的。

            【讨论】:

              【解决方案7】:

              这个问题可能是由于多种原因造成的,其中之一是,

              我的 MQTT 代理在 macOS 上运行,系统防病毒防火墙阻止我连接。

              禁用防病毒防火墙帮助我毫无问题地连接到代理。

              【讨论】:

                【解决方案8】:

                您的意思是 Raspberry 是代理,而 arduino 是订阅者,不是吗?

                当我想与我的订阅者联系代理时,我遇到了类似的问题,它无法连接到端口 1883(似乎仅适用于发布者..)。 我通过以下更改解决了它,

                config.mk

                WITH_WEBSOCKETS:=yes

                mosquitto.conf

                改变:

                user mosquitto by user pi

                在文件末尾添加:

                listener 1883 listener 9001 <Raspberry IP> protocol websockets

                重新运行代理

                mosquitto -c mosquitto.conf
                

                然后我可以将我的订阅者连接到端口 9001 上的代理。

                【讨论】:

                • 这是完全错误的。发布者和订阅者都可以使用 1883 端口(连接时它们之间没有真正的区别,同一个客户端可以同时是 pub 和 sub)。
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-07-20
                • 2019-03-17
                相关资源
                最近更新 更多