knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.82k stars 1.47k forks source link

Socket error on client <unknown>, disconnecting #711

Open coolcodecding opened 4 years ago

coolcodecding commented 4 years ago

I have setup mosquitto on docker using certificates to secure the connection. I have no username and password configured in mosquitto. I am using this code to connect my wemos d1 mini to mosquitto with pubsubclient.

https://github.com/debsahu/ESP_MQTT_Secure/blob/master/ESP8266_MQTT_SSL/Arduino/ESP8266_PubSubClient_SSL/ESP8266_PubSubClient_SSL.ino

I do not use the secrets.h file, but I tried it before with the same result.

After inserting my credentials, mqtt data and ca.crt i get the following serial output:

Attempting to connect to SSID: MYSSID ... connected! 
Setting time using SNTP.done! 
Current time: Wed Feb 19 13:53:17 2020 Time: Wed Feb 19 13:53:17 2020 
MQTT connecting ... failed, status code =-2. Try again in 5 seconds. 
MQTT connecting ... failed, status code =-2. Try again in 5 seconds. 
MQTT connecting ... failed, status code =-2. Try again in 5 seconds.

mosquitto gives the following log:

1582138400: New connection from 192.168.0.8 on port 8883. 
1582138405: Socket error on client <unknown>, disconnecting. 
1582138405: New connection from 192.168.0.8 on port 8883. 
1582138413: New connection from 192.168.0.8 on port 8883. 
1582138418: Socket error on client <unknown>, disconnecting. 
1582138418: New connection from 192.168.0.8 on port 8883. 
1582138424: Socket error on client <unknown>, disconnecting. 
1582138424: New connection from 192.168.0.8 on port 8883. 
1582138429: Socket error on client <unknown>, disconnecting. 
1582138429: New connection from 192.168.0.8 on port 8883. 
1582138434: Socket error on client <unknown>, disconnecting. 
1582138435: New connection from 192.168.0.8 on port 8883.
1582138440: Socket error on client <unknown>, disconnecting. 
1582138440: New connection from 192.168.0.8 on port 8883. 
1582138443: Client <unknown> has exceeded timeout, disconnecting.

At least I get the last line: disconnected because of timeout. And 192.168.0.8 is the wemos D1 mini. I used the MQTTfx to connect to mosquitto with the ca.crt file before and it worked fine.

my mosquitto.conf:

allow_anonymous true

port 8883

cafile /ca.crt
keyfile /server.key
certfile /server.crt
tls_version tlsv1.2

persistence true
persistence_location /mosquitto/data/

why does mosquitto not recognise the client name that I have specified in the code? Why can I not connect to mosquitto? And how can I solve this?

frederikheld commented 4 years ago

See here: https://github.com/knolleary/pubsubclient/issues/355

I'm still trying to figure out what exactly I need to change. So if you get it running, could you please post your solution?

coolcodecding commented 4 years ago

@frederikheld I gave up on this code, as I couldn't figure it out and am not willing to spend another 20h+ on it.

I switched to this, on the first sight a bit more complex code: https://github.com/tsi-software/Secure_ESP8266_MQTT_poc/tree/master/top-level-components/secure_esp8266_mqtt_client

It worked smoothly for me, hopefully for you too!

coolcodecding commented 4 years ago

So, i am back to the problem again. After setting up a new mqtt server, the same script does not work anymore.. Have there been update for the libraries?