Accéder au contenu principal

Mi flora integration to OpenMQttgateway

Following some users requests on OpenMQTTGateway github I made some research and code to be able to read data from Mi Flora sensor also known as xiaomi flower sensor.
The goal is to connect the mi flora to an mqtt compatible controller like openhab or home assistant with a simple arduino or esp8266 connected to an hm10/11 module.



Mi flora use Bluetooth Low Energy technology to communicate to a smartphone. 
It gather on one sensor temperature, humidity, hygro and fertility measurement. These measures are available through an app on your smartphone so as to monitor your plants.

As this device use BLE the idea was to be able to read the measures with OpenMqttgateway through the hm10 component.

Some code and library are already available in python for the pi or equivalents. But not for arduino or esp8266.
This is where it begins.

With the current gateway version we were able to receive the mac adress of ble devices including the mi flora one. But not the device measures.

I tried the different AT commands of my hm10 without sucess I hadn't any return corresponding to the measures I was getting in the android app. After some hours spent, I asked for help in Martin Currey blog, who answered me to upgrade my hm10 version and to test the new commands.
Indeed with the firmware v547 the command AT+DISA?  was introduced, if we check the release note of Jinan Huamao site we see that this command return the following data :

Add AT+DISA? command used to search devices and return full information
Possible return string format:
OK+DISS  --> Search start
<OK+DISA:><Device Address><Device Type><Device RSSI><Rest data length><Rest data>
OK+DISE  --> Search end
Note: <Rest data length> and <Rest data> is Hex format

So as to know if it is relevant for our measures I used the same method of reverse engineering than this blog post

The particular point compared to the previous commands is that we need now to manipulate hexadecimal returns instead of ascii ones. Maybe the points which asked me most of the developments...

What I did is to launch several times the command AT+DISA? In different sensor configuration and saved the returned values. Here is the detail for one AT+DISA? :

Hexadecimal dataAsciiDecimalType of data
4F 4B 2B 44 49 53 41 53OK+DISAScommand return
4F 4B 2B 44 49 53 41 3AOK+DISA:command return
63 B6 65 8D 7C C4MAC adress reverted
00device type
C0192P3: Device RSSI [1Byte]
1C28P4: Rest data length [1Byte]
02 01 06 03 02 95 FE 14 16 95 FE 71 20 98 00 68 63 B6 65 8D 7C C4 0D 09 10 02 00 00P5: Rest data
0D 0ACR LF
4F 4B 2B 44 49 53 41 3AOK+DISA:command return
63 B6 65 8D 7C C4MAC adress reverted
000device type
C0192P3: Device RSSI [1Byte]
0D13P4: Rest data length [1Byte]
0C 09 46 6C 6F 77 65 72 20 63 61 72 65Flower careP5: Rest data
0D 0ACR LF
4F 4B 2B 44 49 53 43 45OK+DISCEcommand return

There is 2 sentence of data begining each time by OK+DISA:
Each sentence contains:
  • The mac adress reverted
  • The device type = 00, seems not used
  • The device RSSI signal strength
  • The rest data length
  • The rest data , the first message contains the measure, the second message contains the name of the device 'flower care"
  • The other are commands return

We already had the mac adress and the rssi with the AT+DISI? command
But we didnt had the first rest data message.

If we put in list this data chain we can isolate certain values : 
02 01 06 03 02 95 FE 14 16 95 FE 71 20 98 00 68 63 B6 65 8D 7C C4 0D 09 10 02 00 00
02 01 06 03 02 95 FE 14 16 95 FE 71 20 98 00 71 63 B6 65 8D 7C C4 0D 04 10 02 D8 00
02 01 06 03 02 95 FE 14 16 95 FE 71 20 98 00 6D 63 B6 65 8D 7C C4 0D 04 10 02 D7 00
02 01 06 03 02 95 FE 13 16 95 FE 71 20 98 00 6B 63 B6 65 8D 7C C4 0D 08 10 01 00
02 01 06 03 02 95 FE 14 16 95 FE 71 20 98 00 1C 63 B6 65 8D 7C C4 0D 09 10 02 00 00
02 01 06 03 02 95 FE 14 16 95 FE 71 20 98 00 20 63 B6 65 8D 7C C4 0D 09 10 02 00 00
02 01 06 03 02 95 FE 15 16 95 FE 71 20 98 00 22 63 B6 65 8D 7C C4 0D 07 10 03 00 00 00
02 01 06 03 02 95 FE 14 16 95 FE 71 20 98 00 24 63 B6 65 8D 7C C4 0D 09 10 02 00 00
02 01 06 03 02 95 FE 15 16 95 FE 71 20 98 00 26 63 B6 65 8D 7C C4 0D 07 10 03 6E 00 00

The hex value number 8 doesn't seems to vary enough to be a measurement but if we convert it to decimals it gives 14->20, 13->19, 15->21 which corresponds exactly to the number of hex value following.

The last values seems more interesting, we have 4 different values 09, 04, 08, 07 as we have exactly 4 different measure type for this sensor maybe we have a first track.
The 10 is constant.
The 02, 01, 03 correspond to the number of values following.
After that we have some values that we can convert to decimals:
00 00 -> 0
D8 00 -> 55296
D7 00 -> 55040
6E 00 00 -> 7208960

Not so relevant...

If we revert these values as the mac adress we get:
00 00 -> 0
00 D8  -> 216
00 D7  -> 215
00 00 6E  -> 110
More interesting, indeed we can deduct that the values after the 10 are the measures if we revert the hexa values order.

After some trials I deducted that:
  • 9 = ferti,
  • 4 = temp, 
  • 7 = lux, 
  • 8 = hum

By comparaison with the values in the app I saw that the temperature is multiplied by a factor 10. The others seems usable like that.

Once this analyse done I had to recode the ZgatewayBT to be able to analyse hexadecimals data.

The gateway now support mi flora & presence detection!


To follow the test/integration process of the code you can subscribe to this issue.

Next step will be to make some code factoring and why not creating a library dedicated to HM10-11 for arduino and esp8266!


Xiaomi and Mi Flora are registered trademarks of BEIJING XIAOMI TECHNOLOGY CO., LTD.


Commentaires

  1. is there any practical way for understanding the hex values?

    RépondreSupprimer
    Réponses
    1. You can use this tool:
      https://www.binaryhexconverter.com/hex-to-decimal-converter

      Supprimer
    2. https://i.hizliresim.com/mMvO01.png

      I try to getting value from miflora by Arduino, hm-10. According to value of miflora, I manage somethings. I am looking for automate the hex converting. Of course the hex value has some meaning. I believe that there should be practical way understanding the data, like python project https://github.com/open-homeautomation/miflora. thank you

      Supprimer
    3. For example, why "Flower Care" is meaningful but others are not?

      Supprimer
    4. If you look at Openmqttgateway code file zgatewaybt.ino it do it out of the box for arduino with hm10

      Supprimer
    5. I am sorry for occupy you. I am new. As I learn the meaning of mqtt, I don't need any network connection for now, so I dont need any mqtt gateway. Am I right?

      I try to parse zgatewaybt.ino code, there are some points I cant understand. It is my mistake. I am sorry.

      Supprimer
    6. I cant find detailed documentation about using zgatewaybt.ino

      Supprimer
    7. how should I give meaning the line https://github.com/1technophile/OpenMQTTGateway/blob/master/ZgatewayBT.ino#L200

      i think it is main idea.

      Supprimer
  2. I download the github project OpenMqttGateway. I put the library on the proper files. I use Arduino IDE. It is last version. When I compile the openmqttgateway.ino I take a lot of errors like "OpenMQTTGateway.ino:154:48: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]". after that, I also try to use platformio but the result is the same. What should I do? What is my mistake?

    Really Thank you.

    RépondreSupprimer
    Réponses
    1. If you are only getting warnings it should not block you. My recommendation for the moment is to stay with arduino IDE (until I port the project to platform.io)

      Supprimer
  3. Hello, when I assign the hex data to a String by openmqttgateway, the String overflow. I also try char array. but the "lastindexof","substring" functions are not available for char array. How did you exceed the problem? I use arduino uno.

    RépondreSupprimer
    Réponses
    1. Hello, Did you think on working with a board with larger memory like MEGA. Or maybe you would have a simpler way by using an ESP32

      Supprimer
    2. I succeed. Thank you sir.

      Supprimer
  4. Ce commentaire a été supprimé par l'auteur.

    RépondreSupprimer
  5. Hi. I tried with AT+DISA? but I realized that most of the time the HM-10 module can't detect the Xiaomi Flora. Are you facing the similar problem?

    RépondreSupprimer

Enregistrer un commentaire

Posts les plus consultés de ce blog

433toMQTTto433 - Bidirectional ESP8266 NodeMCU gateway between RF 433Mhz signal and MQTT

The goal  is to act as a gateway between 433Mhz sensors and a MQTT broker or between the MQTT broker and 433Mhz actuators, It enables to: receive MQTT data from a topic and send RF 433Mhz signal corresponding to the received MQTT data  publish MQTT data to a different topic related to received 433Mhz signal  It can be an interesting part in an home automation system so as to interface sensors and actuators (wall sockets) with software like openhab . List of compatible sensors here The interest of putting this gateway to an ESP8266 and not on a raspberry pi is to be able to manage security actions at gateway level (power on a siren, cut power to certain devices) following RF data received by sensors without being dependent to the PI for security related actions. [EDIT] all infos are now centralized into  the github repository  take a look at it you will find up to date info about OpenMQTTGateway You need: Software: Mosquitto Arduino IDE latest version (tested ok with 1.6.10

Infrared IR, 433mhz and MQTT on ESP8266 bidirectional gateway OpenMQTTGateway

Following discussions on the home assistant forum people gave me the idea to add Infrared communication to the 433mhz gateway.  The goal is to act as a gateway between 433Mhz sensors, infrared remote controls and a MQTT broker or between the MQTT broker and 433Mhz actuators, infrared devices, It enables to: receive MQTT data from a topic and send RF 433Mhz signal corresponding to the received MQTT data  publish MQTT data to a different topic related to received 433Mhz signal  receive MQTT data from a topic and send infrared signal corresponding to the received MQTT data  publish MQTT data to a different topic related to received infrared signal  It can be an interesting part in an home automation system so as to interface sensors and actuators (wall sockets), your tv, home cinema, hifi ... with software like  openhab  or home assistant . List of compatible RF sensors here [EDIT] all infos are now centralized into the github repository  take a look at it you will find up to d

Get your BLE sensors data into Home Assistant in 5 minutes

You can now upload your board directly from the web browser!  So let's imagine you want to read data from a sensor like a Mi Flora, an LYWSD03MMC, a weight scale, or any other BLE sensor from this list  Plug an ESP32 dev board to your computer USB port Go to this website: https://docs.openmqttgateway.com/upload/web-install.html Select esp32dev-ble Click the install button Depending on your board you may have to press the BOOT button Choose the port that the ESP is connected to. Wait until the process is complete. Release the BOOT button That's it, OMG is now loaded into your ESP32 board without Arduino IDE, platformIO or a binary flasher. Here are the steps in images: Now comes the Home Assistant part: Add the MQTT integration and activate auto discovery Create a user and a password (Configuration->Users) without administrator right for the gateway Well, this is enough for Home Assistant. So let's now connect both: Check the Wifi Access points available with your smartph