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
609 stars 140 forks source link

Two instances with mqtt #121

Open Fragggy opened 4 years ago

Fragggy commented 4 years ago

Hi everyone,

first thank you for this awesome piece of software.

I started with one daemon an a pi zero and newer missed an update at the mqtt broker. Now with a second instance setup, I lose a lot of updates on the mqtt broker. Interval is set to 300s and it sometime takes 20min till something arrives.

If I disable one instance it is back to normal. Is there something special to set, when using more than one daemon on the same mqtt broker?

Thank you for any advice.

Regards

Dennis

kwesolowski commented 4 years ago

Second instance is on the same hardware? I would worry more about sharing bluetooth than sharing a broker?

Fragggy commented 4 years ago

Second instance is on different Hardware.

Range was a problem, so I set up a second one.

kwesolowski commented 4 years ago

Are both working fine with json backend when observed over SSH?

Fragggy commented 4 years ago

You mean the output when running them not as a daemon? Or is there some kind of verbose logging I have to look for?

kwesolowski commented 4 years ago

In config you can select local std out printing and run not as a service, so you can see if measurements are coming.

Also when running not as a deamon stdout is already very verbose. Do you see measurements happening but just mqtt misses them?

On Fri, 29 May 2020, 17:14 Fragggy, notifications@github.com wrote:

You mean the output when running them not as a daemon? Or is there some kind of verbose logging I have to look for?

β€” You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ThomDietrich/miflora-mqtt-daemon/issues/121#issuecomment-636028630, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABNPW344SHK2YZTOWUKDOMTRT7GMBANCNFSM4NNJOGSA .

Fragggy commented 3 years ago

Hi, I had time to dig into this a little more. Sorry it took so long.

The Problem seems to be, that both clients use the same (empty) name, when they connect to the server: image If I turn one Instance off, the reconnecting stops.

The Server now thinks it is one Client and switches between both. Is there a way to change the Name?

Regards

Dennis

OnnoH commented 3 years ago

It's easy to provide a name in mqtt_client = mqtt.Client(), but that shouldn't be neccessary, because the broker (Mosquitto in my case) assigns a clientID automatically.

1604067158: mosquitto version 1.6.12 running
1604067164: New connection from 172.17.0.1 on port 1883.
1604067164: New client connected from 172.17.0.1 as Frederik (p2, c1, k60).
1604067176: Client Frederik disconnected.
1604067205: New connection from 172.17.0.1 on port 1883.
1604067205: New client connected from 172.17.0.1 as auto-3778535E-D001-177E-BF04-C0E98312E552 (p2, c1, k60).
1604067217: Client auto-3778535E-D001-177E-BF04-C0E98312E552 disconnected.

Which broker are you using?

Fragggy commented 3 years ago

I am using the mqtt Adapter in iobroker.

Do i have to set it in the Code or is there also a parameter in config.ini for this?

Fragggy commented 3 years ago

Ok i tested it.

Changed the Code to: mqtt_client = mqtt.Client(client_id="FlowerPi2")

Now I hava a Name in the Log: image

But the client crashed with the error below:

Adding sensor to device list and testing connection ...
Name:          "Garden_right"
Internal name: "Garden_right"
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/btlewrap/bluepy.py", line 27, in _func_wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/btlewrap/bluepy.py", line 56, in connect
    self._peripheral = Peripheral(mac, iface=iface, addrType=self.address_type)
  File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 391, in __init__
    self._connect(deviceAddr, addrType, iface)
  File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 439, in _connect
    raise BTLEDisconnectError("Failed to connect to peripheral %s, addr type: %s" % (addr, addrType), rsp)
bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral C4:7C:8D:67:B9:4F, addr type: public

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py", line 216, in <module>
    print('Device name:   "{}"'.format(flora_poller.name()))
  File "/usr/local/lib/python3.7/dist-packages/miflora/miflora_poller.py", line 74, in name
    with self._bt_interface.connect(self._mac) as connection:
  File "/usr/local/lib/python3.7/dist-packages/btlewrap/base.py", line 45, in __enter__
    self._backend.connect(self._mac)
  File "/usr/local/lib/python3.7/dist-packages/btlewrap/bluepy.py", line 33, in _func_wrapper
    raise BluetoothBackendException() from last_error
btlewrap.base.BluetoothBackendException
OnnoH commented 3 years ago

That seems like another problem to me and is Bluetooth related. Your Pi can't reach the MiFlora C4:7C:8D:67:B9:4F. Battery low or a sensor reset needed?