How to make a plant monitor dashboard: Part I
Written by James. Published on under the Plant Monitor (Series) category.
Do you sometimes forget to water your plants when they are in need of some water? I am the sort of person that writes down "water plants" but forgets about it for a day or two before finally watering my plant. My plant is alive and well but I could do better. That's one reason why I decided to research if there were ways in which I could monitor the moisture levels in my plant soil. Moisture levels are directly correlated with when a plant needs watered: the drier the soil, the more the plant needs watered.
After some research, I came across the Pimoroni Grow HAT, an electronic device that you can attach to a Raspberry Pi Zero to keep tabs on the moisture levels in a plant. The Grow HAT kit comes with three moisture sensors, enough to monitor three plants. Or, you can be like me and put the sensors all in one plant so you can monitor moisture levels at different parts of the plant soil.
I have been building a web dashboard that lets me monitor moisture levels in my plant. In this guide, I'm going to teach you how to build a plant monitor dashboard using the Grow HAT.
You can find the code for this project at https://github.com/capjamesg/plant-sensor-project. The code is exactly what I'll teach you to write.
What you'll need
To follow along with this tutorial, you will need:
- A Pimoroni Grow HAT kit
- A plant
- A Raspberry Pi
- A power source for the Raspberry Pi
- An micro SD card configured with the Raspberry Pi operating system
- A mini HDMI cable
- A mouse and keyboard
- A micro USB hub so that you can connect your keyboard and mouse to the Pi
I'd recommend having a basic understanding of Python to follow along with this tutorial. If you have all of these things ready, you are ready to go. I would also recommend following along with this tutorial on your Raspberry Pi rather than using SSH to connect to it and write your code. This is because we're going to create charts in Part 2 which you will need to see. If you have some Pi knowhow, you can still use SSH to connect to your Pi.
Before I begin with the programming tutorial, I ask that you follow the Pimoroni Grow HAT installation instructions provided on their website. These instructions will tell you how to set up the Grow HAT on your Pi and provide an easy guide to install the Grow HAT software. The Pimoroni tutorial is detailed and covers everything you'll need to build the plant monitor dashboard.
Build your plant monitoring program
Before we can talk about building a web page or charts that show the moisture levels in your plants, we have to actually gather some data. The data we'll gather is the level of moisture according to each of the three sensors in the Grow HAT kit. To gather data, we're going to use a basic Python script.
We'll start by using the Pimoroni Grow HAT software (which you should have installed if you folllowed the Pimoroni tutorial inked above) to read the moisture levels in our soil. Before we do this, there's something you should know about the sensors that you will need to keep in mind. The sensors sometimes return a 0.0 reading. This is part of how the moisture sensors work. As a result, we need to write a program that monitors our moisture sensors until they return a reading greater than 0. Create a file called plant_sensor.py and add this code in:
from grow.moisture import Moisture import datetime import csv m1 = 0 m2 = 0 m3 = 0 m1_interface = Moisture(1) m2_interface = Moisture(2) m3_interface = Moisture(3) while m1 == 0: m1 = m1_interface.moisture while m2 == 0: m2 = m2_interface.moisture while m3 == 0: m3 = m3_interface.moisture desired_saturation = 2.0 if m1 > desired_saturation or m2 > desired_saturation or m3 > desired_saturation: print("Your plant need watered") print(m1, m2, m3)
We start by importing the Pimoroni Grow library that lets us measure moisture levels using our sensors. We import datetime and csv for later use. We then declare three variables—m1, m2, and m3—which correspond to each of our sensors. Next, we use the Moisture() function to check the moisture level on each sensor. We then use three while loops to keep reading the sensor moisture levels until they return a value greater than 0.
After this code has run, we should have three moisture levels stored in the m1, m2, and m3 variables. We also declare a variable called desired_saturation which is the level at which we will need to water our plants. 2.0 seems to be what works for me but you'll want to adjust this depending on how thirsty your plants get. If you have a plant that needs watered every two days or so, you might want to change this number. To find the number to add here, you should take a reading when you think your plant needs watered. You can then use that as your baseline, the point at which you know you'll need to water your plants.
Our program evaluates whether any sensor reports a reading higher than the desired_saturation variablt. If any sensor does report a reading higher than the desired_saturation, we print a message to the console indicating that we need to water our plant.
At the end of our program, we print the readings from our moisture sensors to the console.
To run the program, use this command:
Next, we have to save our data to a file. If we don't do this, we'll not be able to keep track of how our plants change over time.
To do this, we first need to create a file called logging.csv in which we will save our plant moisture readings. Create the logging.csv file and add this text:
This text will be our "header row." We will use these words later in the tutorial series as a way to read the information from our logging.csv file.
Add the following code to your plant_sensor.py file:
now = datetime.datetime.now() with open("/home/james/plant-sensor/logging.csv", "a", newline="") as file: writer = csv.writer(file) row = [ now.strftime("%Y-%m-%d"), now.strftime("%H:%M"), m1, m2, m3 ] writer.writerow(row)
The csv.writer() function lets us prepare an object that we can use to write to the logging file. We create the row we want to write to our csv file as a list in the row variable. Then we write that row to the file we have opened (in this case, logging.csv). logging.csv is where we'll keep track of all of our plant moisture data and, later, read past moisture levels so that we can create charts. The "a" text in the code above means we'll "append" a row to our file rather than write over the existing file or change existing data in our file.
Now you are ready to run your program:
Your program should print the moisture levels returned by the sensors to the console. Once your program has finished running, you can open logging.csv. You should see an entry something like this:
You can see the date, time, and the moisture readings from our three sensors.
Monitor your plants daily
If you're like me and forget to water your plants every so often, you are probably not going to remember to run the program we have just made every day. That's why I recommend setting up a cron task that runs your program for you at a certain interval. cron is a built-in utility in UNIX-based operating systems like Pi OS that runs programs at certain times. I have a cron task set up to take a moisture reading every hour.
To set up a cron task, you need to first open your cron file. You can do this from your terminal using the following command:
This command will open up a file that keeps track of all of your cron tasks.
Add the following text to the end of the file:
0 * * * * python3 /home/james/plant-sensor/monitor_plants.py >> /home/james/log.log 2>&1
The "0 * * " means run a command every hour. You should change the /home/james/plant-sensor/ folder to point to wherever you have saved your monitor_plants.py file. The "/home/james/log.log 2>&1" saves any errors our program encounters into a file called log.log. You should substitute /home/james/ with wherever you want to save this log file. While we don't need this logging, it is helpful to have just in case anything goes wrong.
Wait until the end of the hour and then wait a few minutes for your monitor_plants.py program to run. After about two minutes of waiting, check your logging.csv file. If you don't see a reading, something is wrong and you should consult your log.log file. If you do see a reading, you'll know that everything is working.
Now your Pi will take moisture readings without your having to do anything (as long as the Pi is turned on, of course!).
You'll want to get readings on at least two days before you move to the next part of this tutorial.
In this tutorial, we have discussed how to create a Python program that logs the moisture levels in your plant. You can now track the moisture levels in your plant by running a python program. We are not finished yet. If you like reading numbers in a file, you could stop here. But, would you not prefer to have a pretty chart to look at that shows the moisture levels in your plant? That's going to be the topic of part two of this series. Then I'll walk you through how to create a web page on which to display your charts.
Do you have any feedback on this tutorial? Are you stuck somewhere? Does a code snippet not work like you think it should? Email me at email@example.com and I would be happy to help.
Read more content like this
Check out the other posts I have written related to this article.
- Coffee Chat with Jamie from Luckie Beans
- The Coffee Dictionary Book Review
- My two-cup Chemex recipe
- The Chemex: A week or two on
- More testing with the Hario V60
Powered by Novacast
Other posts in this series
Check out the other posts I have written as part of this series.
Comment on this post
Respond to this post by sending a Webmention.