Yet Another Xiaomi Miflora Script

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
madrian
Posts: 231
Joined: Saturday 27 August 2016 1:18
Target OS: -
Domoticz version:
Contact:

Yet Another Xiaomi Miflora Script

Post by madrian »

Why I "wrote" another script for Miflora sensor? There is another python plugin for Miflora, but it works unreliable if you have sensors at a far distance. If the python plugin cannot read Miflora sensor, it's stuck in a state where you can't read anymore any sensors.

So here is a more reliable method:

0: setup your bluetooth dongle, make sure you see BLE devices with "sudo hcitool lescan". Python3, PIP installations are neccessary.

Install bluepy:

https://github.com/IanHarvey/bluepy

Code: Select all

$ sudo apt-get install git build-essential libglib2.0-dev
$ git clone https://github.com/IanHarvey/bluepy.git
$ cd bluepy
$ python3 setup.py build
$ sudo python3 setup.py install

Code: Select all

$ sudo apt-get install python3-pip libglib2.0-dev
$ sudo pip3 install bluepy
If I remember correctly I did the two installation method after each other, after that bluepy backend worked correctly in the demo.py script. In a normal case you install manually (first case) or via pip3 install.

Let's move on.

On Ubuntu 16.04 bluez comes with gatttool, which works fine with this script. I red somewhere that gatttool is missing from newer installations, if that's the case, just use bluepy as the backend.

At this moment bluepy is my preferred backend, it seem to be reliable.


1:

Download latest Miflora library release from Github

https://github.com/open-homeautomation/miflora/releases

Code: Select all

madrian@ubuntu:~/scripts$ wget https://github.com/open-homeautomation/miflora/archive/v0.4.zip
madrian@ubuntu:~/scripts$ unzip v0.4.zip
madrian@ubuntu:~/scripts$ cd miflora-0.4/
The script can poll data with various backends: gatttool, bluepy.

You can verify working of both backend with:

Code: Select all

madrian@ubuntu:~/scripts/miflora-0.4$ sudo ./demo.py --backend gatttool scan
Bumm, it's not implemented for gatttool. Ok. Let's try bluepy as backend:

Code: Select all

madrian@ubuntu:~/scripts/miflora-0.4$ sudo ./demo.py --backend bluepy scan
Scanning for 10 seconds...
Found 3 devices:
  C4:7C:8D:62:xx:xx
  C4:7C:8D:63:xx:xx
  C4:7C:8D:61:xx:xx
That's just worked fine.

Let's try to get some data:

Code: Select all

madrian@ubuntu:~/scripts/miflora-0.4$ sudo ./demo.py --backend gatttool poll C4:7C:8D:61:xx:xx
Getting data from Mi Flora
FW: 3.1.8
Name: Flower care
Temperature: 34.3
Moisture: 38
Light: 68503
Conductivity: 326
Battery: 99
Gatttool worked perfectly. Let's try bluepy as backend:

Code: Select all

madrian@ubuntu:~/scripts/miflora-0.4$ sudo ./demo.py --backend bluepy poll C4:7C:8D:61:AA:A8
Getting data from Mi Flora
FW: 3.1.8
Name: Flower care
Temperature: 34.1
Moisture: 38
Light: 72156
Conductivity: 328
Battery: 99
Nice.

We have a working setup, let's write some script to connect with Domoticz.

Create dummy devices for each sensors. I have these per sensor:
Screen Shot 2018-04-21 at 12.37.52.png
Screen Shot 2018-04-21 at 12.37.52.png (174.25 KiB) Viewed 3601 times
Put these scripts in the folder above miflora-0.4.

domo.sh:

Code: Select all

#!/bin/bash
cd "$(dirname "$0")"

while read -r prefix val; do
    case "$prefix" in 
        FW:) fw=$val ;;
        Name:) name=$val ;;
        Temperature:) temp=$val ;;
        Moisture:) mt=$val ;;
        Light:) lt=$val ;;
        Conductivity:) ct=$val ;;
        Battery:) bt=$val ;;
    esac
done < <(sudo ./demo.py --backend bluepy poll $1)

#moisture
curl "http://localhost:8080/json.htm?type=command&param=udevice&idx=$2&nvalue=0&svalue=$mt&battery=$bt"

#temperature
curl "http://localhost:8080/json.htm?type=command&param=udevice&idx=$3&nvalue=0&svalue=$temp&battery=$bt"

#light
curl "http://localhost:8080/json.htm?type=command&param=udevice&idx=$4&svalue=$lt&battery=$bt"

#conductivity
curl "http://localhost:8080/json.htm?type=command&param=udevice&idx=$5&nvalue=0&svalue=$ct&battery=$bt"
You can test at this point:

Code: Select all

madrian@ubuntu:~/scripts/miflora-0.4$ ./domo.sh C4:7C:8D:63:7A:11 103 104 105 106
{
   "status" : "OK",
   "title" : "Update Device"
}
{
   "status" : "OK",
   "title" : "Update Device"
}
{
   "status" : "OK",
   "title" : "Update Device"
}
{
   "status" : "OK",
   "title" : "Update Device"
}
Note the syntax is: ./domo.sh mac_address_of_the_miflora moisture_idx temperature_idx light_idx condictivity_idx

Everything is working fine. I have multiple sensors so I wrote another script:

Code: Select all

madrian@ubuntu:~/scripts/miflora-0.4$ cat run_domo.sh 
#!/bin/bash
cd "$(dirname "$0")"

#fc1
/home/madrian/scripts/miflora-0.4/domo.sh C4:7C:8D:63:xx:xx 103 104 105 106

#fc2
/home/madrian/scripts/miflora-0.4/domo.sh C4:7C:8D:62:xx:xx 107 108 109 110

#fc3
/home/madria/scripts/miflora-0.4/domo.sh C4:7C:8D:61:xx:xx 111 112 113 114
Do one more test by running ./run_domo.sh

Finally make a new crontab to call this script every x minutes.

Code: Select all

*/30 * * * * /home/madrian/scripts/miflora-0.4/run_domo.sh  >/dev/null 2>&1
Kivi
Posts: 6
Joined: Wednesday 15 April 2015 20:54
Target OS: Linux
Domoticz version:
Contact:

Re: Yet Another Xiaomi Miflora Script

Post by Kivi »

Thank you for this guide, I just had this horror crash all the time.. trying your way :P.
totof60
Posts: 4
Joined: Saturday 06 January 2018 16:52
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Yet Another Xiaomi Miflora Script

Post by totof60 »

Thanks it work fine for me
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests