[TUTORIAL] Monitor your plants with Mi Flora sensors

Objective

This tutorial aims to retrieve information provided by Mi Flora sensors in Gladys. For this, we need:

  • A Mi Flora sensor
  • A Raspberry Pi Zero W
  • A Gladys instance

The Raspberry Pi Zero will serve as a gateway for the data: It will execute the Python script from Thom Dietrich as a service to retrieve the data and write it to MQTT topics accessible by Gladys.

Prerequisites

An MQTT server must be configured via Gladys (See MQTT Integration)

1- Gladys

Go to Integration > MQTT > Add a device
Name: MiFlora Sensor - Basil (example)
External ID: mqtt:miflora:basil

Note: The external ID must necessarily start with mqtt:miflora: followed by the name of the plant that you will use later to configure the Python script. External ID: mqtt:miflora:[Plant Name]

Then, add the different features of the sensor





Important: The external IDs of the features must be copied as shown in the screenshots; they correspond to the IDs of the Python script.

Gladys is now able to read the information from the Basil sensor on the MQTT server.

Raspberry Pi Zero

The rest of this tutorial is a translation with adaptations from Thom Dietrich’s wiki. Since the PR has not yet been integrated by Thom, I use my Git as a reference for the script modified for Gladys.

Connect to the Raspberry Pi via SSH

sudo apt install git python3 python3-pip bluetooth bluez
git clone https://github.com/Chouille/miflora-mqtt-daemon.git /opt/miflora-mqtt-daemon
cd /opt/miflora-mqtt-daemon
sudo pip3 install -r requirement.txt

The daemon depends on GATTTool, an external tool provided by the bluez package. To ensure that GattTool is installed

gatttool --help

Then, we create the configuration file necessary for the script.

cp /opt/miflora-mqtt-daemon/config.{ini.dist,ini}

Retrieve the MAC address of your sensor like this:

sudo hcitool lescan
4B:47:E2:DE:CE:9A (UNKNOWN)
84:C0:EF:46:B2:8A (UNKNOWN)
10:0B:F1:43:59:16 (UNKNOWN)
C4:7C:8D:62:40:29 FLOWER CARE

Edit the configuration file, here is a basic configuration:

[General]
reporting_method = gladys-mqtt
adapter = hci0
[Daemon]
enabled = true
period = 3600 #1 hour
hostname = [Gladys IP]
port = 1883
keepalive = 60
base_topic = gladys/master/device
username=gladys
password=[See Gladys setup]
Basil@LivingRoom=C4:7C:8D:62:40:29

Important: The MAC address declaration is done by the name of the plant that you indicated in Gladys. The location of the sensor (@Room) is optional.

Test

sudo python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py

From an MQTT client connected to Gladys’s MQTT server, you should have this:

After creating a section in the Gladys dashboard, you should get this:

Save the script as a service

Important: The Daemon mode must be enabled in the configuration file!

sudo cp /opt/miflora-mqtt-daemon/template.service /etc/systemd/system/miflora.service
sudo systemctl daemon-reload
sudo systemctl start miflora.service
sudo systemctl status miflora.service
sudo systemctl enable miflora.service

Now that the plant monitoring is on Gladys, you could:

  • Be notified if your plant lacks water and/or start a pump to water it.
  • Be notified if the temperature is too high/low for your plant.
  • Turn on a supplementary grow light if it lacks light.
  • etc …

Hello and a big thank you for the tutorial :+1:

I haven’t finished it, as I’m encountering an error and I’m a bit stuck… I also had some difficulties moving forward in the tutorial:

You need to execute these commands in sudo otherwise you can’t progress, so:

sudo git clone https://github.com/Chouille/miflora-mqtt-daemon.git /opt/miflora-mqtt-daemon
sudo cp /opt/miflora-mqtt-daemon/config.{ini.dist,ini}

Otherwise, I’m at the point of testing the miflora-mqtt-daemon.py file… when I run the command
sudo python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py

I get this:

[2021-04-13 14:46:20] Configuration parameter reporting_mode set to an invalid value

Yet, in the config file I have:

reporting_method = gladys-mqtt

I don’t know if I’m right, but it seems that gladys-mqtt is not declared in the miflora-mqtt-daemon.py file…

Hello Jean.
If you have cloned my repo, the variable reporting_method must be declared.
You can check in miflora-mqtt-daemon.py that you have this between line 349 and 365:

elif reporting_mode == 'gladys-mqtt':
print_line('Announcing Mi Flora devices to MQTT broker for auto-discovery ...')

for [flora_name, flora] in flores.items():
    topic_path = '{}/mqtt:miflora:{}/feature'.format(base_topic, flora_name.lower())
    data = OrderedDict()
    for param,_ in parameters.items():
        data[param] = flora['poller'].parameter_value(param)
    mqtt_client.publish('{}/mqtt:battery/state'.format(topic_path),data['battery'],1,True)
    mqtt_client.publish('{}/mqtt:moisture/state'.format(topic_path),data['moisture'],1,True)
    mqtt_client.publish('{}/mqtt:light/state'.format(topic_path),data['light'],1,True)
    mqtt_client.publish('{}/mqtt:conductivity/state'.format(topic_path),data['conductivity'],1,True)
    mqtt_client.publish('{}/mqtt:temperature/state'.format(topic_path),data['temperature'],1,True)

sleep(0.5) # some slack for the publish roundtrip and callback function
print()

And between line 440 and 443:

elif reporting_mode == 'gladys-mqtt':
        print_line('Publishing to MQTT topic \"{}/mqtt:miflora:{}/feature\"'.format(base_topic,
        flora_name.lower()))
        mqtt_client.publish('{}/mqtt:miflora:{}/feature'.format(base_topic, flora_name.lower()),
        json.dumps(data))
        sleep(0.5) # some slack for the publish roundtrip and callback function

In fact, you also needed to add gladys-mqtt in the check part of your file :slight_smile:

    # Check configuration
if reporting_mode not in ['mqtt-json', 'mqtt-homie', 'json', 'mqtt-smarthome', 'homeassistant-mqtt', 'thingsboard-json', 'wirenboard-mqtt', 'gladys-mqtt']:

Thanks a lot for the tutorial, I was just looking for a way to monitor my plants and stop killing them all :laughing:

Do you know which metrics to monitor and when to alert?

I’m asking myself the same questions ^^ I don’t have a green thumb and if this can help me, I won’t say no xD

Exactly! :slight_smile:

Generally, it’s about exposure, humidity, and ambient temperature. And it depends on the plants :sweat_smile:.

Maybe we should create a dedicated topic with the hygrometric values that work by type of plant? Strangely, I found very little information on the net. Are gardeners not geeky?

This could be very interesting!

What kind of genre is this?

https://github.com/vrachieru/plant-database

5 Likes

Awesome! Of course, integrating this kind of project would be ideal, but it’s not really the priority :stuck_out_tongue:

There is also soil fertility (presence of nutrients). This can be achieved by adding fertilizer or easily/for free with vermicomposting, but you need to be a bit patient ^^

https://illustrascience.fr/le-compost/

The advantage is that you reduce your waste. Those lucky enough to have a garden can build a composter from pallets, for those in apartments (with or without a balcony):

You can also add composter monitoring in Gladys :slight_smile:

But it does give off quite a smell, a worm composter though :wink:

@lmilcent @spenceur, have you tested it?

Personally, not having a Raspberry Pi Zero, I tried it on a Raspberry Pi 3B+ and it doesn’t work, it finds my sensor but can’t retrieve the information, yet on GitHub it says that the Raspberry 3 is compatible…

I don’t really have an error message except for a timeout, I can see that it connects to the flower care via its LED, but it cuts off after two flashes, every time.

I saw your issue @glasserre, I think I’m good to buy a little Raspberry Zero.

I’m receiving my sensors next week (yes, I cracked when I saw the tutorial :-p )

I haven’t bought a sensor yet, but that will come one day, the desire is not lacking :wink:
Especially since I’ve stopped counting the number of plant corpses I’ve had to throw away :sweat_smile:

Yes, I searched (updated btlewrap/bluepy, tested with bluegatt) but no data.
After all, I’m not an expert at all, I must have missed something.

I’m going to get some more as well, it’s soon the French Days :slight_smile:

Isn’t there a way to add the discovery of @Jean34 in a Mi Flora service?
That way, the user could, if possible, create scenes based on a reference, right?

I will see a more generic thing (not directly linked to a service) with the scenes.

We will assign sensors to a plant and it will manage the alert conditions on its own. (Based on the discovery of @Jean34)