Tag: ESP32

Mi Flora and the ESP32

Mi Flora and the ESP32

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-mark-32pxGitHub, 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.




I have been meaning to try the new ESP32 for quite a while and I finally got around to ordering them and trying them. As you may know, the ESP32 is the successor of the ESP8266 and the biggest addition, at least for me, is BLE, bluetooth low energy, support. So before I go into how I managed to get it to talk to several MiFloras, let’s first set it up to work with Arduino.

Of course it does not make sense to rewrite what others have done. I followed this article to set up Arduino to work with the ESP32.

When you’re done, read on.

What I could not find in the article is how to configure some of the settings in Arduino, so here is what I have and what works for me:

ESP32 settings

What also gave me some issues is getting the code to upload. As you can see in the picture above, I bought 2 different versions, a small one for which you have to solder the pins yourself and which has 2 buttons on the back and the ESP32 DEV module. Initially I could not get the code to upload to the ESP32 DEV module. When I tried the same for the samller module, it seemd to work. After some trial and error I figured out how to upload to the ESP32 DEV module as well. Tell Arduino to upload and after it is done compiling, it will show you:

esptool.py v2.3.1

This will continue and eventually you will get a timeout unless…. you press the button marked boot and hold it until Arduino starts uploading. Than you can let go. I don’t know why this is not needed for the smaller one and maybe it is a setting in Arduino which puts it in boot mode, similar to what could be done for the ESP8266. If I find it, I will update this article.

Now, you are all done and you want to start uploading some BLE sketches only to find it does not allow you to do so. Something about memory.  Luckily somebody already figured out what needs to be done. You need to change the partition size as described in this article. I choose method 3 but instead of creating a new type, I just updated the existing type (lazy, I know). I changed the following 2 files (Windows platform):

C:\Program Files (x86)\Arduino\hardware\espressif\esp32\boards.txt

and changed the following (marked in bold underline):


esp32.name=ESP32 Dev Module



and changed the file (path clipped to prevent scrolling):


to (full file):

# Name,   Type, SubType, Offset,  Size, Flags
nvs,      data, nvs,     0x9000,  0x5000,
otadata,  data, ota,     0xe000,  0x2000,
app0,     app,  ota_0,   0x10000, 0x1E0000,
app1,     app,  ota_1,   0x1F0000,0x1E0000,
eeprom,   data, 0x99,    0x3F0000,0x1000,
spiffs,   data, spiffs,  0x3F1000,0xF000,

So now you can upload your sketch and you might run into the next problem; either the ESP keeps rebooting and you get a message about brown out. There are people that stop brownout detection (see Google) but what worked for me, at least during development on the computer, change the cable to a better one. The cheap Chinese cables gave me brownouts while the more sturdy cables I had worked.

Next step…. using the ESP32 to talk to the Xiaomi MiFlora.