ThomDietrich/miflora-mqtt-daemon

Connection error with result code 2 - Connection Refused: identifier rejected.

Closed this issue · 11 comments

Hello,

I'm trying the new openhab internal MQTT broker and so far all my devices are working great.

I can't get the MQTT Daemon to connect though (triple-checked correct credentials):

[2018-11-05 18:51:24] Connecting to MQTT broker ... [2018-11-05 18:51:24] Connection error with result code 2 - Connection Refused: identifier rejected.

Looks like the broker is not happy with the client ID, see here: https://jaimyn.com.au/mqtt-connection-failed-status-codes-connack-return-codes/

But the config.ini does not allow to change the client ID. how is it chosen?

Cheers

It seems there is no Client ID set. Can it be added?

Hey @chiefymuc, this should not happen. Let's try to resolve the issue.

The current implementation should generate a random client ID: https://pypi.org/project/paho-mqtt/#constructor-reinitialise

client_id - the unique client id string used when connecting to the broker. If client_id is zero length or None, then one will be randomly generated.

Could you please rule out the setting by changing this line:

mqtt_client = mqtt.Client()

to

mqtt_client = mqtt.Client(client_id="my_test_id")

That works!

Weird! Could you please try to get log messages from moquette (the included broker) and see which client id is sent without the change?

Hi, what log message are you talking about? I need some directions to get it - sorry, not very familiar with it.

I do not have a testing system at hand but surely Moquette writes log lines to the file `openhab.log. Those lines for a working and non-working client would be great

Had the same problem somehow, but the modified line worked out perfectly!

I do not have a testing system at hand but surely Moquette writes log lines to the file `openhab.log. Those lines for a working and non-working client would be great

I tested it with the original script:

mqtt_client = mqtt.Client()

Afterwards I looked into the openhab.log file. It says that the client ID can not be empty:

2019-01-05 22:55:43.995 [ERROR] [.moquette.spi.impl.ProtocolProcessor] - The MQTT client ID cannot be empty. Username=null

If I insert the client ID, as described, the connection will work fine.

mqtt_client = mqtt.Client(client_id="my_test_id")

@Markkuuss that's really strange as the documentation for the Client constructor is supposed to generate a random ID. I wonder if we should file an issue with paho-mqtt in addition to your PR.

I searched a bit and found what I was looking for. This issue also concerns the java moquette mqtt broker as it is integrated in OpenHab.
eclipse/paho.mqtt.python#209

If the paho-mqtt constructor remains empty, mqtt 3.1.1 is used and paho will try an empty client_id by default:
https://github.com/eclipse/paho.mqtt.python/blob/v1.3.0/src/paho/mqtt/client.py#L457-L458

This is exactly the case here.

You have integrated the following version of paho-mqtt:

paho-mqtt==1.3.0

In version 1.3.1 this is fixed. If an mqtt 3.1.1 broker cannot handle an empty client id, a random id will be generated.
eclipse/paho.mqtt.python@6b6198e

I changed the version in the file to 1.4.0 and run pip3 again.

paho-mqtt==1.3.0

With this it also works with the mqtt client constructor without parameters as it currently is.