ThomDietrich / miflora-mqtt-daemon

Linux service to collect and transfer Xiaomi Mi Flora plant sensor data via MQTT to your smart home system, with cluster support 🌱🌼πŸ₯€πŸ‘🌳
MIT License
610 stars 140 forks source link

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

Closed chiefymuc closed 5 years ago

chiefymuc commented 6 years ago

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

chiefymuc commented 6 years ago

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

ThomDietrich commented 6 years ago

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:

https://github.com/ThomDietrich/miflora-mqtt-daemon/blob/52e4658b40bacbe0809754ed58effc7759c3de48/miflora-mqtt-daemon.py#L155

to

mqtt_client = mqtt.Client(client_id="my_test_id")
chiefymuc commented 6 years ago

That works!

ThomDietrich commented 6 years ago

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

chiefymuc commented 6 years ago

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

ThomDietrich commented 6 years ago

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

NicoFilips commented 5 years ago

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

Markkuuss commented 5 years ago

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: https://github.com/ThomDietrich/miflora-mqtt-daemon/blob/52e4658b40bacbe0809754ed58effc7759c3de48/miflora-mqtt-daemon.py#L155

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")
ThomDietrich commented 5 years ago

@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.

Markkuuss commented 5 years ago

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. https://github.com/eclipse/paho.mqtt.python/issues/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: https://github.com/ThomDietrich/miflora-mqtt-daemon/blob/58aeb016fc8766b5953aa97134121046cadca17f/requirements.txt#L3

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. https://github.com/eclipse/paho.mqtt.python/commit/6b6198ec3057bb81142d2f1ba4644502cc85f821

Markkuuss commented 5 years ago

I changed the version in the file to 1.4.0 and run pip3 again. https://github.com/ThomDietrich/miflora-mqtt-daemon/blob/58aeb016fc8766b5953aa97134121046cadca17f/requirements.txt#L3

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