Mi Flora Bluetooth LE

From Domoticz
Jump to navigation Jump to search

Are you always killing plants? Then you should have a look at the Xiaomi Mi Flora sensor (a.k.a. Xiaomi Flower Mate), a cheap bluetooth Low Energy plant sensor that measures the moisture in the soil, the temperature, and even the light intensity. It's coin cell battery can last about a year if you only take one measurement a day.

Luckily, some clever people have reverse engineered the protocol it uses.

Be careful, this device uses Bluetooth LE (Low Energy) and therefore, it's not compatible with old bluetooth adapters. Raspberry Pi 3 Bluetooth is Ok.


You have three ways to connect to the MiFlora from Domoticz:

1. NEW! Via a plugin.
2. Directly, via a Python script.
3. Indirectly, by talking through a MQTT server via a Python script.

In all cases you'll have to start by installing and enabling Bluetooth. You'll also have to give Python to access Bluetooth trough additional libraries you will need to install.


1. Xiaomi Mi Flower Mate Plugin

The plugin makes things very easy, so this is the recommended option.

Installation instructions can be found at the plugin's own wiki page: Plugins/Mi_flower_mate


2. Using a script - Preparing Domoticz

This is the manual route, where you have to prepare sensors yourself. We'll then fill in their values via the python script, which you'll have to set up to run every day via a cronjob.

First, if you haven't already done this before, go to hardware, add new hardware, and choose 'dummy' as the type. All the other setting don't matter, you can keep their defaults.

Next, create 4 virtual sensors by clinking the "create virtual sensors" button on the hardware page, under the dummy hardware device. We'll need to make four of them:
# type percentage (moisture)
# type temperature
# type lux
# type custom (fertility)


Installing Bluetooth on Raspberry Pi 2/3 - Debian 8 Jessie

Before doing anything, make sure your server is up-to-date by issues these commands in the terminal: sudo apt-get update and sudo apt-get upgrade

On to Bluetooth. At the moment of writing this, the Raspberry Pi's Apt-get tools could install a pretty old version of Bluez (5.23-2+rpi2), but that doesn't support Bluetooth Low Energy. You need at least version 5.3 for that.

Note: Bluez version 5.44 does not compile gatttool on a Raspberry Pi by default (deprecated tool), so we must add

--enable-deprecated

.

So for now we'll have to make a new version and NOT install it 'manually' over the existing one, but only using the tool we need : gatttool. To compile Bluez 5.44, which is full of nice bugfixes for Bluetooth Low Energy, basically type in commands below and grab a cup of tea during "make all". You should check if a newer version of Bluez 5.44 is available, and replace the newer version number in all the commands below (and edit this page, too).

Dependencies :

sudo apt-get install libglib2.0-dev libdbus-1-dev libical-dev libreadline-dev libudev-dev

Setup :

cd /home/pi
wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.44.tar.gz
tar -xvf bluez-5.44.tar.gz
cd bluez-5.44
sudo ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --enable-tools --disable-test --disable-systemd --enable-deprecated
sudo make all
sudo apt-get install python-bluez python-requests

If all goes well, one last thing is to copy the Gatt tool to the directory where your linux version keeps its programs. It's location is different for different Linux flavours. Try:

sudo cp attrib/gatttool /usr/bin/ ( or sudo cp attrib/gatttool /usr/local/bin/ )

And just to be safe try export PATH=$PATH:~/bluez-5.44/attrib/

If all goes well you'll have installed Bluetooth. Time to reboot the server.

sudo shutdown -r now

Option A: Direct connection via a Python script

The direct-connection script was written in Python 3, so let's grab that.

Download the Python3 script with git command : (if it isn't installed, you can install it by sudo apt-get install -y git-core
cd ~ && git clone https://github.com/Tristan79/miflora.git

Copy the folder miflora to your Domoticz python scripts directory.
cp -R ~/miflora/ ~/domoticz/scripts/python/miflora

Python-to-bluetooth Next, Install Python3 and the required Python3 libraries that let Python script talk to Bluetooth.

sudo aptitude install python3 python3-pip
sudo pip3 install pygatt
sudo pip3 install requests

Finding the sensor's Bluetooth Address Now it's time to find your sensor's address, because we'll need to add in into the script soon.

Make sure Bluetooth LE is on. From the bluez folder type:
sudo tools/btmgmt le on
sudo tools/btmgmt connectable on
sudo tools/btmgmt power on

Then scan for your device. The toggling off-on at the start is on purpose)

sudo hciconfig hci0 down
sudo hciconfig hci0 up
hciconfig
sudo hcitool lescan

Write down the address (something like 12:34:56:78:90:AB), and press ctrl-z to exit

Alternatively, you can also try to scan this way:

bluetoothctl
scan on
exit

Edit the domoticz.py script Add the Domoticz server IP address at the top, as well as any login information (if necessary).

Next, add your device's Bluetooth address into the appropriate 'update' call at the bottom of the script. Also add the four IDX codes (the unique numbers) of the Domoticz switches that you created earlier. You can find those IDX numbers in the devices list in Domoticz. If you made them in the correct order above this should be easy:

update("C3:7C:8E:12:80:94","72","73","74","75")

Schedule the polling

To enable this script to runs, say, every 12 hours, we'll need to create a Cron task:
sudo crontab -e
And then add this line:
0 0,12 * * * /usr/bin/python3 /home/pi/domoticz/scripts/python/miflora/domoticz.py
This runs the script twice a day, at noon and at midnight.

Option B: Connecting via MQTT via Python

This version uses a MQTT server as a man in the middle. It's the long way around, but it's nice if you have multiple smart home platforms and devices that you would like to have access to the Mi Flora sensor data. MQTT is a good standard for that.

Download the script here: https://github.com/marcelrv/miflora

This script uses an older version of Python, so make sure you have it installed.

sudo apt-get install python sudo apt-get install python-pip sudo pip install gattlib sudo pip install requests

optional: sudo pip install pygatt

Place the script in the domoticz/scripts/python folder.

Someone who has tried this route will have to update this part.

More sources

There is a large thread on the forum. And you can also check out: Presence Detection via Bluetooth [1] [2]