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

MQTT connection error. Please check your settings in the configuration file "config.ini" #125

Closed rb090 closed 4 years ago

rb090 commented 4 years ago

Hi,

I bought this Mija-plantsensor and I clone this repository an try to query this sensor and to read data from it.

I followed all the steps from the README.md but I always get this output:

Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon
Source: https://github.com/ThomDietrich/miflora-mqtt-daemon

[2020-08-06 17:59:17] Connecting to MQTT broker ...
[2020-08-06 17:59:17] MQTT connection error. Please check your settings in the configuration file "config.ini"

I really do not understand what went wrong - or what's wrong with my config.ini:

[General]
reporting_method = mqtt-json
adapter = hci0

[Daemon]
enabled = true
period = 300

[MQTT]
hostname = localhost
port = 1883
keepalive = 60
base_topic = miflora
tls = false

[Sensors]
MyPlant = C4:7C:8D:6B:43:76

This file is in the root folder also where also the python script (miflora-mqtt-daemon.py) is located:

screenshot

I can see the plant sensor if I do sudo hcitool lescan:

Bildschirmfoto 2020-08-06 um 20 55 42

Can you please tell me what I am doing wrong?

I am thankful for every help and advice.

rb090 commented 4 years ago

Okay it is always good to talk about issues to get ideas how to solve them 😁. The reporting_method in the section [General] was wrong. This should be set to json so that it looks like this: reporting_method = json.

So now when executing the python script (sudo python3 miflora-mqtt-daemon.py) I see valid output which looks like this:

[2020-08-06 20:39:07] Retrieving data from sensor "MyPlant" ...
[2020-08-06 20:39:07] Result: {"light": 0, "temperature": 26.3, "moisture": 6, "conductivity": 0, "battery": 100}
Data for "Basilikum": {"light": 0, "temperature": 26.3, "moisture": 6, "conductivity": 0, "battery": 100, "timestamp": "2020-08-06 20:39:07", "name": "MyPlant", "name_pretty": "MyPlant", "mac": "C4:7C:8D:6B:43:76", "firmware": "3.2.2"}
rb090 commented 4 years ago

But what I really cannot understand and it seems that sth. went wrong, when I change reporting_method and also add the base_topic described in the documentation I always get the error

Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon
Source: https://github.com/ThomDietrich/miflora-mqtt-daemon

[2020-08-06 17:59:17] Connecting to MQTT broker ...
[2020-08-06 17:59:17] MQTT connection error. Please check your settings in the configuration file "config.ini"

I want to use this sensor in Home Assistant so I change my configuration, it looks now like this:

[General]
# as described needed to publish to an MQTT broker following the HomeAssistant discovery format 
reporting_method = homeassistant-mqtt

[Daemon]
# nothing defined

[MQTT]
# Default depends on the configured reporting_method
# Default for: mqtt-json, mqtt-smarthome, homeassistant-mqtt
base_topic = miflora

[Sensors]
MyPlant = C4:7C:8D:6B:43:76

That is why I reopened this issue, when changing the reporting_method to sth else than json the error is thrown πŸ€•. What I am doing exactly wrong in my config file? Can you please help me with this?

rb090 commented 4 years ago

Okay now this issue should be solved. I added some logging to miflora-mqtt-daemon.py from line 164 to 169. The logging output of the exception shows [Errno 111] Connection refused. When seeing this error I checked if my MQTT Broker was installed correctly. That was the problem. I reinstall Mosquitto and everything works fine now.

It was necessary to add a timer service like described here: https://github.com/ThomDietrich/miflora-mqtt-daemon/issues/52#issuecomment-590190810

After that everything works also fine after reboot