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
606 stars 142 forks source link

Initial connection to Mi Flora sensor [...] failed (was working previously on a pi zero) #80

Open septeven opened 5 years ago

septeven commented 5 years ago

Hi, I had miflora-mqtt-daemon correctly setup on a pi zero - everything was working perfectly well. Today, I decided to replace my pi zero with a raspberry pi 3b+ so I installed everything again on the new pi, including miflora-mqtt-daemon.

I got the same exact configuration than on the pizero but I can't get my miflora sensor detected though - I have the feeling like if it still binded to the pizero and can't be binded again with another setup ? Any ideas ? I've been completely stucked for hours....

Here is the error showing up when launching python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py (FYI config.ini is correctly setup, mac-adresses are correctly defined, again - same configuration than the working pizero)

Initial connection to Mi Flora sensor "miflora_plant" (xx:xx:xx:xx:xx:xx) failed.

joshmcarthur commented 5 years ago

I'm running into the same problem.

The Dockerfile I am using:

FROM resin/rpi-raspbian

RUN apt-get update &&\
    apt-get install -y git python3 python3-pip bluetooth bluez &&\
    git clone https://github.com/ThomDietrich/miflora-mqtt-daemon.git /opt/miflora-mqtt-daemon &&\
    mkdir /config

WORKDIR /opt/miflora-mqtt-daemon
RUN pip3 install -r requirements.txt
CMD [ "python3", "./miflora-mqtt-daemon.py", "--config_dir", "/config" ]

My config.ini:

# Configuration file for Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon
# Source: https://github.com/ThomDietrich/miflora-mqtt-daemon
#
# Uncomment and adapt all settings as needed.

[General]

# The operation mode of the program. Determines wether retrieved sensor data is published via MQTT or stdout/file.
# Currently supported:
#
#           mqtt-json - Publish to an MQTT broker in a proprietary json format (Default)
#          mqtt-homie - Publish to an MQTT broker following the Homie MQTT convention
#                       (https://github.com/marvinroger/homie)
#      mqtt-smarthome - Publish to an MQTT broker following the mqtt-smarthome proposal
#                       (https://github.com/mqtt-smarthome/mqtt-smarthome)
#  homeassistant-mqtt - Publish to an MQTT broker following the HomeAssistant discovery format
#                       (https://www.home-assistant.io/docs/mqtt/discovery/)
#    thingsboard-json - Publish to the ThingsBoard MQTT broker
#                       (https://thingsboard.io)
#    wirenboard-mqtt -  Publish to the Wiren Board MQTT broker
#                       (https://wirenboard.com)
#                json - Print to stdout as json encoded strings
#
#reporting_method = mqtt-json
reporting_method = json

# The bluetooth adapter that should be used to connect to Mi Flora devices (Default: hci0)
#adapter = hci0

[Daemon]

# Enable or Disable an endless execution loop (Default: true)
#enabled = true

# The period between two measurements in seconds (Default: 300)
#period = 300

[MQTT]

# The hostname or IP address of the MQTT broker to connect to (Default: localhost)
#hostname = localhost

# The TCP port the MQTT broker is listening on (Default: 1883)
#port = 1883

# Maximum period in seconds between ping messages to the broker. (Default: 60)
#keepalive = 60

# The MQTT base topic to publish all Mi Flora sensor data topics under.
# Default depends on the configured reporting_method
#base_topic = miflora                   # Default for: mqtt-json, mqtt-smarthome
#base_topic = homie                     # Default for: mqtt-homie
#base_topic = homeassistant             # Default for: homeassistant-mqtt
#base_topic = v1/devices/me/telemetry   # Default for: thingsboard-json
#base_topic =                           # Default for: wirenboard-mqtt

# Homie specific: The device ID for this daemon instance (Default: miflora-mqtt-daemon)
#homie_device_id = miflora-mqtt-daemon

# The MQTT broker authentification credentials (Default: no authentication)
#username = user
#password = pwd123

# Enable TLS/SSL on the connection
#tls = false

# Path to CA Certificate file to verify host
#tls_ca_cert =

# Path to TLS client auth key file
#tls_keyfile =

# Path to TLS client auth certificate file
#tls_certfile =

[Sensors]

# Add your Mi Flora sensors here. Each sensor consists of a name and a Ethernet MAC address.
# Additional location information can be added to the name, delimited by an '@'.
# Scan for sensors from the command line with:
#    $ sudo hcitool lescan
#
# Examples:
#
#Schefflera@Living = C4:7C:8D:11:22:33
#JapaneseBonsai    = C4:7C:8D:44:55:66
#Petunia@Balcony   = C4:7C:8D:77:88:99
Veggie@Backgarden = C4:7C:8D:xx:xx:xx

(i.e. my modifications are to enable the STDOUT JSON logging while I verify it's working OK, and to add my own Flora MAC)

Output:

docker run -v $PWD:/config --privileged --rm -it --net=host miflora-gateway

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

Adding sensor to device list and testing connection ...
Name:          "Veggie"
[2019-04-06 03:06:32] Initial connection to Mi Flora sensor "Veggie" (C4:7C:8D:xx:xx:xx) failed.

[2019-04-06 03:06:32] Retrieving data from sensor "Veggie" ...
[2019-04-06 03:07:15] Retrying ...
[2019-04-06 03:07:59] Failed to retrieve data from Mi Flora sensor "Veggie" (C4:7C:8D:xx:xx:xx), success rate: 0%

[2019-04-06 03:07:59] Sleeping (300 seconds) ...

@septeven, do you still have the pi zero available to you? Is there a way you could find out what versions of the dependencies you have available? I'm wondering if a dependency has been updated and this is what might be causing this issue.

I have only connected to my Mi Flora once since it arrived yesterday using a Node.js script that was able to connect and read the sensor values correctly.

septeven commented 5 years ago

@joshmcarthur yes I still have it and if you explain me how to find out the versions details of the dependencies, I will let you know.

joshmcarthur commented 5 years ago

Hey @septeven - I've been doing a bit more on this today and seem to have things working for me. Here's what I tried:

  1. I updated the firmware of the hardware module using the Flower Care app from 2.7.0 to 3-something.
  2. I made sure the app was force-closed when I was done with this. Some BLE periphials are fussy about having more than one client connected in my experience.
  3. I restarted my RaspberryPi. I SHOULD have done this last night, but I guess it never occurred to me. I (mis)use my Pi to run a bunch of Docker containers so restarting can be a bit of a pain, but after restarting, I could connect and read sensor data:
Initial connection to Mi Flora sensor "Veggie" (C4:7C:8D:xx:xx:xx) successful

[2019-04-07 01:25:34] Retrieving data from sensor "Veggie" ...
[2019-04-07 01:25:34] Result: {"temperature": 17.3, "moisture": 0, "conductivity": 0, "light": 373, "battery": 100}
Data for "Veggie": {"temperature": 17.3, "name": "Veggie", "timestamp": "2019-04-07 01:25:34", "light": 373, "name_pretty": "Veggie", "mac": "C4:7C:8D:xx:xx:xx", "moisture": 0, "firmware": "3.1.9", "conductivity": 0, "battery": 100}

[2019-04-07 01:25:34] Sleeping (300 seconds) ...

I first connected to the Flower Care module using a Node.js script on this same Raspberry Pi. My current theory is that possibly it was something in the Pi's bluetooth stack causing the connection to fail, rather than the module. No idea what could have gone wrong, but there's plenty of scope for that in the Pi/Docker containers/Bluetooth sharing setup I have going on here.

For your problem, perhaps it's worth a restart as well, if you haven't already. Also might be worth making sure your Pi Zero isn't on and attempting to connect as well, in case the Mi Flora module is trying to have two clients connect at the same time.

aymeric106 commented 5 years ago

Same issue for me. Was working fine for month, and stopped receiving data for a few days now. I tried to restart the service and my raspberry, but it doesn't fix anything.

● miflora.service - Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon Loaded: loaded (/etc/systemd/system/miflora.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2019-05-14 21:06:47 CEST; 5min ago Docs: https://github.com/ThomDietrich/miflora-mqtt-daemon Main PID: 1634 (python3) Status: "May 14 21:10:28 - Initialization complete, starting MQTT publish loop." CGroup: /system.slice/miflora.service β”œβ”€1634 python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py β”œβ”€1794 /bin/sh -c gatttool --device=C4:7C:8D:66:31:66 --char-read -a 0x38 --adapter=hci0 └─1795 gatttool --device=C4:7C:8D:66:31:66 --char-read -a 0x38 --adapter=hci0 mai 14 21:06:46 openHABianPi systemd[1]: Starting Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon... mai 14 21:06:47 openHABianPi systemd[1]: Started Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon. mai 14 21:08:37 openHABianPi miflora-mqtt-daemon.py[1634]: [2019-05-14 21:08:37] Initial connection to Mi Flora sensor "Dracaena" (C4:7C:8D:66:31:66) failed. mai 14 21:10:28 openHABianPi miflora-mqtt-daemon.py[1634]: [2019-05-14 21:10:28] Initial connection to Mi Flora sensor "Menthe" (C4:7C:8D:66:2F:47) failed. [21:11:53] openhabian@openHABianPi:/opt/miflora-mqtt-daemon$

I don't think I made any change on my PI. Perhaps i retreived some data from the sensor with the android miflora app.

aymeric106 commented 5 years ago

well don't know why exactly but I managed to fix this. Because i bought a Xiaomi webcam, i had to change my account to 'Chinese' servers last week. Don't why and how, but it seemed to have broke something.

I changed it back to Europeean server, resync my sensors with the flower care android app, and now data are back in my openhab pi ...

Markkuuss commented 5 years ago

Hi, I had miflora-mqtt-daemon correctly setup on a pi zero - everything was working perfectly well. Today, I decided to replace my pi zero with a raspberry pi 3b+ so I installed everything again on the new pi, including miflora-mqtt-daemon.

This could be a similar problem to mine: #83

If the Raspberry Pi 3b+ is newer, it probably also has a newer kernel version. Which kernel do you have and which one was on the Zero?

ManuelAbril commented 3 years ago

Hi,

I am having the same error. I give you some details:

[General]
reporting_method = homeassistant-mqtt

[Daemon]

[MQTT]
hostname = <myip>
port = <myport>
base_topic = miflora 
username = <myusername>
password = <mypassword>

[Sensors]
espatifilo = <espatifilo_mac>

My docker-compose is:

# https://github.com/RaymondMouthaan/miflora-mqtt-daemon
  miflora:
    image: raymondmm/miflora2mqtt:v1.1.1-arm32v7
    container_name: miflora
    restart: always
    volumes:
      - ${USERDIR}/docker_files/homeassistant/config:/config
    environment:
      - TZ=${TZ}

# https://github.com/eclipse/mosquitto
  mosquitto:
    image: eclipse-mosquitto:1.6.12
    container_name: mosquitto
    restart: unless-stopped
    network_mode: host
    ports:
      - 1883:1883
      - 9001:9001
    volumes:
      - ${USERDIR}/docker_files/mosquitto/config:/mosquitto/config
      - ${USERDIR}/docker_files/mosquitto/log:/mosquitto/log
      - ${USERDIR}/docker_files/mosquitto/data:/mosquitto/data
      - /etc/localtime:/etc/localtime:ro

# https://github.com/home-assistant/home-assistant
  home-assistant:
    image: homeassistant/raspberrypi2-homeassistant:0.115.6
    container_name: homeassistant
    restart: unless-stopped
    network_mode: host
    ports:
      - 8123:8123
    volumes:
      - ${USERDIR}/docker_files/homeassistant/config:/config
      - ${USERDIR}/docker_files/homeassistant/config/.ssh:/root/.ssh
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock
    privileged: true
    environment:
      - PUID=${PUID}
      - PGID=${PGID}
      - TZ=${TZ}
    depends_on:
      - mosquitto

Raspbian version: Linux raspberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l GNU/Linux

Logs

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

[2020-10-16 09:30:11] Connecting to MQTT broker ...
[2020-10-16 09:30:11] MQTT connection established
Adding sensor to device list and testing connection ...
Name:          "espatifilo"
[2020-10-16 09:30:42] Initial connection to Mi Flora sensor "espatifilo" (XX:XX:XX:XX:XX:XX) failed.
[2020-10-16 09:30:42] Announcing Mi Flora devices to MQTT broker for auto-discovery ...
[2020-10-16 09:30:42] Retrieving data from sensor "espatifilo" ...
[2020-10-16 09:31:12] Retrying ...
[2020-10-16 09:31:42] Failed to retrieve data from Mi Flora sensor "espatifilo" (XX:XX:XX:XX:XX:XX), success rate: 0%
[2020-10-16 09:31:42] Sleeping (300 seconds) ...
cjoha commented 3 years ago

I'm having the same issue from the looks of it.

Hardware is a Raspberry Pi 3b running Ubuntu Server (20.04.2 LTS) 64-Bit. As with @ManuelAbril I have miflora-mqtt-daemon running in a Docker container, but I have this as the only docker container on this Pi. Home Assistant lives elsewhere. I can connect to the mi-flora device using their "Flower Care" IOS app, which also confirms the latest FW version (connecting into EU servers). I've adjusted the polling rate to watch for any reconnections, but I'm getting the below

[2021-03-19 13:42:03] Retrieving data from sensor "Basil" ... [2021-03-19 13:42:04] Retrying ... [2021-03-19 13:42:04] Failed to retrieve data from Mi Flora sensor "Basil" ([mac-add-removed]), success rate: 0% [2021-03-19 13:42:04] Sleeping (10 seconds) ...

[2021-03-19 13:42:14] Retrieving data from sensor "Basil" ... [2021-03-19 13:42:14] Retrying ... [2021-03-19 13:42:15] Failed to retrieve data from Mi Flora sensor "Basil" ([mac-add-removed]), success rate: 0% [2021-03-19 13:42:15] Sleeping (10 seconds) ...

coccobill1 commented 3 years ago

Same behavior here, but on an Ubuntu VBox VM with a passed through USB stick. The only thing that helps is rebooting the VM, which also hangs at reboot more than 50% of the time. Restarting the docker container fails 100% of the time at that point.

cjoha commented 3 years ago

I thought it might be bluepy, it was failing during the container build. I've resolved that by building bluepy from source. All other dependencies are installed. But still no connection.

absolutelyNothing9 commented 3 years ago

Same issue.

analogue commented 3 years ago

New user to miflora here and had same issue trying to get miflora working in a docker container on a rpi. Turns out the --net host is necessary (at least in my case) for it to work. The empty error message is not too useful, so poking at the exception in a debug session revealed a bit more info:

[2021-04-17 12:11:08] Connecting to MQTT broker ...
[2021-04-17 12:11:08] MQTT connection established

Adding sensor to device list and testing connection ...
Name:          "DatePalms"
> /usr/src/app/miflora-mqtt-daemon.py(214)<module>()
-> print_line('Initial connection to Mi Flora sensor "{}" ({}) failed due to exception: {}'.format(name_pretty, mac, e), error=True, sd_notify=True)
(Pdb) e
BluetoothBackendException()
(Pdb) dir(e)
['__cause__', '__class__', '__context__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__suppress_context__', '__traceback__', '__weakref__', 'args', 'with_traceback']
(Pdb) e.__cause__
BTLEDisconnectError('Failed to connect to peripheral [redacted], addr type: public', {'rsp': ['stat'], 'state': ['disc'], 'mtu': [0], 'sec': ['low']})
(Pdb) dir(e.__cause__)
['__cause__', '__class__', '__context__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__suppress_context__', '__traceback__', '__weakref__', 'args', 'emsg', 'estat', 'message', 'with_traceback']
(Pdb) e.__cause__.__cause__
(Pdb) e.__cause__.args
('Failed to connect to peripheral [redacted], addr type: public', {'rsp': ['stat'], 'state': ['disc'], 'mtu': [0], 'sec': ['low']})
(Pdb) e.__cause__.emsg
(Pdb) e.__cause__.estat
(Pdb) e.__cause__.message
'Failed to connect to peripheral [redacted], addr type: public'
(Pdb)
'Failed to connect to peripheral [redacted], addr type: public'

Then I used gatttool both inside and outside the container and it failed in the container without --network=host.

Hope that helps someone :-)

brononius commented 3 years ago

Same here. I'm new to the story, and installed it on a Raspberry 4. At first, everything worked fine. But suddenly, after about 2 hours, I noticed that the data wasn't coming in anymore. After digging a bit deeper, I'm getting the same error:

root@winniedepoeh:~# sudo systemctl status miflora.service
* miflora.service - Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon
   Loaded: loaded (/etc/systemd/system/miflora.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2021-04-27 21:23:35 CEST; 3min 10s ago
     Docs: https://github.com/ThomDietrich/miflora-mqtt-daemon
 Main PID: 735 (python3)
   Status: "Apr 27 21:25:21 - Status messages published."
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/miflora.service
           `-735 /usr/bin/python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py

apr 27 21:23:32 winniedepoeh systemd[1]: Starting Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon...
apr 27 21:23:35 winniedepoeh systemd[1]: Started Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon.
apr 27 21:23:36 winniedepoeh python3[735]: [2021-04-27 21:23:36] Initial connection to Mi Flora sensor "Bonsai" (C4:7C:8D:6C:7B:BA) failed due to exception:
apr 27 21:25:21 winniedepoeh python3[735]: [2021-04-27 21:25:21] Failed to retrieve data from Mi Flora sensor "Bonsai" (C4:7C:8D:6C:7B:BA), success rate: 0%

At the end, I think it has something to do with the distance. When my sensor is about 3 meters from the RP4 in a straight line, it works. When I move to about 4m, just in line with a door jamb (?), I'm getting the error above?

phenix1990 commented 2 months ago

New user to miflora here and had same issue trying to get miflora working in a docker container on a rpi. Turns out the --net host is necessary (at least in my case) for it to work. The empty error message is not too useful, so poking at the exception in a debug session revealed a bit more info:

[2021-04-17 12:11:08] Connecting to MQTT broker ...
[2021-04-17 12:11:08] MQTT connection established

Adding sensor to device list and testing connection ...
Name:          "DatePalms"
> /usr/src/app/miflora-mqtt-daemon.py(214)<module>()
-> print_line('Initial connection to Mi Flora sensor "{}" ({}) failed due to exception: {}'.format(name_pretty, mac, e), error=True, sd_notify=True)
(Pdb) e
BluetoothBackendException()
(Pdb) dir(e)
['__cause__', '__class__', '__context__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__suppress_context__', '__traceback__', '__weakref__', 'args', 'with_traceback']
(Pdb) e.__cause__
BTLEDisconnectError('Failed to connect to peripheral [redacted], addr type: public', {'rsp': ['stat'], 'state': ['disc'], 'mtu': [0], 'sec': ['low']})
(Pdb) dir(e.__cause__)
['__cause__', '__class__', '__context__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__suppress_context__', '__traceback__', '__weakref__', 'args', 'emsg', 'estat', 'message', 'with_traceback']
(Pdb) e.__cause__.__cause__
(Pdb) e.__cause__.args
('Failed to connect to peripheral [redacted], addr type: public', {'rsp': ['stat'], 'state': ['disc'], 'mtu': [0], 'sec': ['low']})
(Pdb) e.__cause__.emsg
(Pdb) e.__cause__.estat
(Pdb) e.__cause__.message
'Failed to connect to peripheral [redacted], addr type: public'
(Pdb)
'Failed to connect to peripheral [redacted], addr type: public'

Then I used gatttool both inside and outside the container and it failed in the container without --network=host.

Hope that helps someone :-)

Had same problems with the connection. Setting network mode to host solved the problem.

My sensor has firmware version 3.3.6 - seems to work fine πŸ‘

Thanks for sharing the solution!