<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.domoticz.com/index.php?action=history&amp;feed=atom&amp;title=Mi_Flora_Bluetooth_LE</id>
	<title>Mi Flora Bluetooth LE - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.domoticz.com/index.php?action=history&amp;feed=atom&amp;title=Mi_Flora_Bluetooth_LE"/>
	<link rel="alternate" type="text/html" href="https://wiki.domoticz.com/index.php?title=Mi_Flora_Bluetooth_LE&amp;action=history"/>
	<updated>2026-09-18T22:42:51Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.0</generator>
	<entry>
		<id>https://wiki.domoticz.com/index.php?title=Mi_Flora_Bluetooth_LE&amp;diff=17362&amp;oldid=prev</id>
		<title>Remb0 at 08:03, 18 April 2022</title>
		<link rel="alternate" type="text/html" href="https://wiki.domoticz.com/index.php?title=Mi_Flora_Bluetooth_LE&amp;diff=17362&amp;oldid=prev"/>
		<updated>2022-04-18T08:03:23Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Are you always killing plants? Then you should have a look at the [https://www.banggood.com/Global-Version-Xiaomi-4-In-1-Flower-Plant-Light-Temperature-Tester-Garden-Soil-Moisture-Nutrient-Monitor-p-1068369.html?imageAb=1&amp;amp;p=OD1302964369201412GS&amp;amp;akmClientCountry=NL&amp;amp;cur_warehouse=CN 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&amp;#039;s coin cell battery can last about a year if you only take one measurement a day.&lt;br /&gt;
&lt;br /&gt;
Luckily, some clever people have [https://wiki.hackerspace.pl/projects:xiaomi-flora reverse engineered] the protocol it uses.&lt;br /&gt;
&lt;br /&gt;
Be careful, this device uses Bluetooth LE (Low Energy) and therefore, it&amp;#039;s not compatible with old bluetooth adapters. Raspberry Pi 3 Bluetooth is Ok.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You have three ways to connect to the MiFlora from Domoticz:&lt;br /&gt;
&lt;br /&gt;
1. NEW! Via a plugin.&amp;lt;br /&amp;gt;&lt;br /&gt;
2. Directly, via a Python script.&amp;lt;br&amp;gt;&lt;br /&gt;
3. Indirectly, by talking through a MQTT server via a Python script.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In all cases you&amp;#039;ll have to start by installing and enabling Bluetooth. You&amp;#039;ll also have to give Python to access Bluetooth trough additional libraries you will need to install.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==1. Xiaomi Mi Flower Mate Plugin==&lt;br /&gt;
&lt;br /&gt;
The plugin makes things very easy, so this is the recommended option.&lt;br /&gt;
&lt;br /&gt;
Installation instructions can be found at the plugin&amp;#039;s own wiki page:&lt;br /&gt;
[[Plugins/Mi_flower_mate]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==2. Using a script - Preparing Domoticz==&lt;br /&gt;
&lt;br /&gt;
This is the manual route, where you have to prepare sensors yourself. We&amp;#039;ll then fill in their values via the python script, which you&amp;#039;ll have to set up to run every day via a cronjob.&lt;br /&gt;
&lt;br /&gt;
First, if you haven&amp;#039;t already done this before, go to hardware, add new hardware, and choose &amp;#039;dummy&amp;#039; as the type. All the other setting don&amp;#039;t matter, you can keep their defaults. &lt;br /&gt;
&lt;br /&gt;
Next, create 4 virtual sensors by clinking the &amp;quot;create virtual sensors&amp;quot; button on the hardware page, under the dummy hardware device. We&amp;#039;ll need to make four of them:&amp;lt;br&amp;gt;&lt;br /&gt;
	# type percentage (moisture)&amp;lt;br&amp;gt;&lt;br /&gt;
	# type temperature&amp;lt;br&amp;gt;&lt;br /&gt;
	# type lux&amp;lt;br&amp;gt;&lt;br /&gt;
	# type custom (fertility)&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Installing Bluetooth on Raspberry Pi 2/3 - Debian 8 Jessie===&lt;br /&gt;
&lt;br /&gt;
Before doing anything, make sure your server is up-to-date by issues these commands in the terminal: &amp;lt;code&amp;gt;sudo apt-get update&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;sudo apt-get upgrade&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On to Bluetooth. At the moment of writing this, the Raspberry Pi&amp;#039;s Apt-get tools could install a pretty old version of Bluez (5.23-2+rpi2), but that doesn&amp;#039;t support Bluetooth Low Energy. You need at least version 5.3 for that.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Note: Bluez version 5.44 does not compile gatttool on a Raspberry Pi by default (deprecated tool), so we must add &amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;--enable-deprecated&amp;lt;/syntaxhighlight&amp;gt;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
So for now we&amp;#039;ll have to make a new version and NOT install it &amp;#039;manually&amp;#039; over the existing one, but only using the tool we need&amp;lt;span&amp;gt; &amp;lt;/span&amp;gt;: gatttool.&lt;br /&gt;
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 &amp;quot;make all&amp;quot;. 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).&lt;br /&gt;
&lt;br /&gt;
Dependencies&amp;lt;span&amp;gt; &amp;lt;/span&amp;gt;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo apt-get install libglib2.0-dev libdbus-1-dev libical-dev libreadline-dev libudev-dev&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Setup&amp;lt;span&amp;gt; &amp;lt;/span&amp;gt;: &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
cd /home/pi&lt;br /&gt;
wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.44.tar.gz&lt;br /&gt;
tar -xvf bluez-5.44.tar.gz&lt;br /&gt;
cd bluez-5.44&lt;br /&gt;
sudo ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --enable-tools --disable-test --disable-systemd --enable-deprecated&lt;br /&gt;
sudo make all&lt;br /&gt;
sudo apt-get install python-bluez python-requests&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If all goes well, one last thing is to copy the Gatt tool to the directory where your linux version keeps its programs. It&amp;#039;s location is different for different Linux flavours. Try:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;sudo cp attrib/gatttool /usr/bin/&amp;lt;/code&amp;gt; ( or &amp;lt;code&amp;gt;sudo cp attrib/gatttool /usr/local/bin/&amp;lt;/code&amp;gt; )&lt;br /&gt;
&lt;br /&gt;
And just to be safe try &amp;lt;code&amp;gt;export PATH=$PATH:~/bluez-5.44/attrib/&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If all goes well you&amp;#039;ll have installed Bluetooth. Time to reboot the server.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;sudo shutdown -r now&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Option A: Direct connection via a Python script====&lt;br /&gt;
&lt;br /&gt;
The direct-connection script was written in Python 3, so let&amp;#039;s grab that.&lt;br /&gt;
&lt;br /&gt;
Download the Python3 script with &amp;lt;code&amp;gt;git&amp;lt;/code&amp;gt; command&amp;lt;span&amp;gt; &amp;lt;/span&amp;gt;: (if it isn&amp;#039;t installed, you can install it by &amp;lt;code&amp;gt;sudo apt-get install -y git-core&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;cd ~ &amp;amp;&amp;amp; git clone https://github.com/Tristan79/miflora.git&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy the folder miflora to your Domoticz python scripts directory.&amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;cp -R ~/miflora/ ~/domoticz/scripts/python/miflora&amp;lt;/code&amp;gt;&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python-to-bluetooth&lt;br /&gt;
Next, Install Python3 and the required Python3 libraries that let Python script talk to Bluetooth.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;sudo aptitude install python3 python3-pip&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;sudo pip3 install pygatt&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;sudo pip3 install requests&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finding the sensor&amp;#039;s Bluetooth Address&lt;br /&gt;
Now it&amp;#039;s time to find your sensor&amp;#039;s address, because we&amp;#039;ll need to add in into the script soon.&lt;br /&gt;
&lt;br /&gt;
Make sure Bluetooth LE is on. From the bluez folder type:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;sudo tools/btmgmt le on&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;sudo tools/btmgmt connectable on&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;sudo tools/btmgmt power on&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then scan for your device. The toggling off-on at the start is on purpose)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;sudo hciconfig hci0 down &amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;sudo hciconfig hci0 up&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;hciconfig&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;sudo hcitool lescan&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Write down the address (something like 12:34:56:78:90:AB), and press ctrl-z to exit&lt;br /&gt;
&lt;br /&gt;
Alternatively, you can also try to scan this way:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;bluetoothctl&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;scan on&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;exit&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Edit the domoticz.py script&lt;br /&gt;
Add the Domoticz server IP address at the top, as well as any login information (if necessary).&lt;br /&gt;
&lt;br /&gt;
Next, add your device&amp;#039;s Bluetooth address into the appropriate &amp;#039;update&amp;#039; 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:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;update(&amp;quot;C3:7C:8E:12:80:94&amp;quot;,&amp;quot;72&amp;quot;,&amp;quot;73&amp;quot;,&amp;quot;74&amp;quot;,&amp;quot;75&amp;quot;)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Schedule the polling&lt;br /&gt;
&lt;br /&gt;
To enable this script to runs, say, every 12 hours, we&amp;#039;ll need to create a Cron task:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;sudo crontab -e&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
And then add this line:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;0 0,12 * * * /usr/bin/python3 /home/pi/domoticz/scripts/python/miflora/domoticz.py&amp;lt;/code&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
This runs the script twice a day, at noon and at midnight.&lt;br /&gt;
&lt;br /&gt;
====Option B: Connecting via MQTT via Python====&lt;br /&gt;
&lt;br /&gt;
This version uses a MQTT server as a man in the middle. It&amp;#039;s the long way around, but it&amp;#039;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.&lt;br /&gt;
&lt;br /&gt;
Download the script here:&lt;br /&gt;
https://github.com/marcelrv/miflora&lt;br /&gt;
&lt;br /&gt;
This script uses an older version of Python, so make sure you have it installed.&lt;br /&gt;
&lt;br /&gt;
sudo apt-get install python&lt;br /&gt;
sudo apt-get install python-pip&lt;br /&gt;
sudo pip install gattlib&lt;br /&gt;
sudo pip install requests&lt;br /&gt;
&lt;br /&gt;
optional: sudo pip install pygatt&lt;br /&gt;
&lt;br /&gt;
Place the script in the domoticz/scripts/python folder.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Someone who has tried this route will have to update this part.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=More sources=&lt;br /&gt;
&lt;br /&gt;
There is a large thread on the forum. And you can also check out:&lt;br /&gt;
[[Presence detection (Bluetooth 4.0 Low energy Beacon)|Presence Detection via Bluetooth]]&lt;br /&gt;
[http://www.elinux.org/RPi_Bluetooth_LE]&lt;br /&gt;
[https://urbanjack.wordpress.com/?s=Bluez+into+BLE]&lt;/div&gt;</summary>
		<author><name>Remb0</name></author>
	</entry>
</feed>