21

I have setup MQTT on a Raspberry Pi and configured an Arduino Uno to the broker, but I am seeing the following entry in the /var/log/mosquitto/mosquitto.log file:

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

The Pi is setup with ETH0 wired to my local LAN and has an IP address of 192.168.1.50

There is also a WiFi AP setup on the Pi. The Arduino Uno connects via WiFi to send/receive MQTT messages. The WiFi AP has an IP address of 192.168.10.1 and provides DHCP leases via dnsmasq.

I have tried publishing and subscribing test on the local MQTT broker server (the Pi) and get the same error:

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.

Here is /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.

There might be an issue in my interfaces configuration. Here is /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

Can anyone point me on where the socket error is coming from on MQTT?

1
  • Have you worked out the issue here? I'm getting mosquitto crashes due to socket issues with unknown client
    – dibs
    Oct 10, 2018 at 5:38

9 Answers 9

8

I came to this thread because I was facing the same error. After some more troubleshooting, it was related to the authentication configuration - the client (Arduino) was trying to connect anonymously (with no password), while the broker (Pi) was set up to only allow authenticated connections (allow_anonymous false in the MQTT configuration).

Adding the correct password to the Arduino connection code solved the issue for me.

6

I was encountering similar issues from an Arduino connecting to 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.

The initial connection would work, then it would encounter frequent socket errors, eventually totally failing to work.

After some digging, I discovered I wasn't calling the loop() function of PubSubClient. Without this the connection isn't properly serviced, resulting in timeouts and socket errors. Try adding client.loop() to your loop() function.

6

Note that this error will be raised by MQTT if the client fails for any reason. I spent several hours digging through MQTT thinking that the pub/sub mechanism was busted. But instead the problem was simply an error in my own hand-written unmarshalling procedure that contained a python assert statement. The assert failed, killing the client, and generating the MQTT error, but no record of the assert failing showed up on my console, leading me down the garden path for awhile. The cause was my executing sys.exit(-1) at that point, killing the notification to the console. (Even in prototyping code, its best to do the right thing when you can, and not silly stuff like this!)

2

I erroneously set up WifiSSLClient on arduino instead of WifiClient. Changed to no-SSL client and the connection error was gone.

My Setup:

  • client: arduino MQTT library (ArduinoMqttClient.h) running on an Arduino wifi rev. 2
  • MQTT broker: mosquitto runing on raspberry Pi 3
2

I had the same problem, This may happen for 2 reasons.

1. Username and password is empty.
2. While allowing anonymous user.

Pls remove if your username and password is empty or provide right username and passwords.

If you are allowing anonymous user then please set the below property in mosquitto.conf file and restart your broker.

allow_anonymous true

Note:- Allowing anonymous user is not a good idea.

1

For connecting anonymously I experienced issues. Specifying "" as username and password is apearantly not anonymously enough. Just don't mention it in the connecting statement works better.

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

I had a similar problem even with the correct acl, .conf and user_password files. The problem was that in my acl file I had an extra space character after the username, which seemed to be considered as a part of the name, and thus expected. That was difficult to spot since the space was invisible.

1

This issue may be due to wide range of reasons, one of them is,

My MQTT broker was running on macOS, systems antivirus firewall blocked me from connecting.

Disabling antivirus firewall helped me to connect to the broker without any issue.

-3

You mean the Raspberry is the broker and the arduino is the subscriber, don't you?

I had a similar issue when i wanted to reach the broker with my subscriber, it would not connect on port 1883 (seems to be only for publishers..). I solved it with the folowing changes,

config.mk :

WITH_WEBSOCKETS:=yes

mosquitto.conf:

Change :

user mosquitto by user pi

Add at the end of the file:

listener 1883 listener 9001 <Raspberry IP> protocol websockets

Re-run the broker

mosquitto -c mosquitto.conf

Then i could connect my subscriber to the broker on port 9001.

1
  • 3
    This is plain wrong. Port 1883 can be used by both publishers and subscribers (there is no real difference between them when connecting, and the same client can be a pub and a sub at the same time).
    – Bogd
    Feb 13, 2018 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.