I initially bought the ESP32 to connect to my Mi Flora sensors. So after I managed to figure out the basics for working with them, I wanted to dive right in and get the Mi Floras working.
First stop, search engine and of lucky for me, somebody already did a lot of the work. On GitHub, user sidddy has a working version of getting an ESP32 talking to a Mi Flora. This was an excellent start as it works quite nicely. The code is straight forward, the ESP wakes up, sets up wifi and bluetooth connection, connects to the Mi Flora, looks for the services it needs, posts the values on MQTT and sleeps again.
As I said, excellent starting point but I wanted a bit more. First, I found that the connection does not always succeed but since these ESPs are now hidden in my garden I wanted to know if the values of the Mi Floras were not updated in my domotica whether it was a problem of the ESP or the Mi Flora. To do that I modified the code so that it sends out an MQTT message with it’s own mac address so I know it was alive. Next, I wanted to have it query multiple Mi Floras. Actually I was hoping to have just one ESP32 for my garden but the range of the ESP32 and the Mi Flora was a bit shorter than I hoped. For me they needed to be at most about 3 meters apart.
I modified the code to sequentially poll 4 Mi Floras in one part of the garden. I managed to get this working only to find that sometimes it can ‘hang’ waiting on the right response from the Mi Flora. Instead of extending the time it stays awake, I decided I would try to poll them all but I would choose randomly from the devices that I did not yet poll. Once in operation, I also found that sometimes you get values that you do not want to have in your domotica environment, 0 degrees temperature (in Spring) or 0% humidity so I have added some checks around each value as well. If the range is off, it will just not post that particular value to MQTT. All in all I found that if I poll them every 30 minutes (a bit frequent, I know) than I have a pretty good chance of them reporting back at least once in every 2 hours which is the limit I have set in my domotica environment.
So the end result, which I have posted on GitHub, is an extension of the mentioned version and all credits go to sidddy as the only thing I did was add a bit of code around his ground work.