How to make a plant monitor dashboard: Part II
Published on under the Plant Monitor (Series) category.Toggle Memex mode

In the last part of this series, I walked you through how to create a program that logs the moisture levels in your plants. If you haven't already read that tutorial, I'd recommend going back to it before reading on. If you have, you are ready to advance onto the next stage of your journey toward building a plant monitor dashboard: showing your plant data on pretty charts. That is the topic of this tutorial.
In my last tutorial, I said that you should take a couple of readings, ideally over multiple days. This is important because we'll need some data to plot on our chart before we create it. If you don't already have some data, run your plant sensor program a few times. You'll need to run the program on at least two days so that we can make the charts in this guide.
You can find the code for this project at on GitHub if you want to take a look at the finished project.
Preparing the libraries we need
To create our charts, we are going to use a Python library called matplotlib. We are also going to use a library called pandas (no relation to the animal!) to process our data that we collected in our csv file. If you have not already installed matplotlib and pandas, you can do so using this command:
pip3 install matplotlib pandas
After installing these libraries, create a new file called create_charts.py. Then add the following text to the start of the file:
import pandas as pd
import matplotlib.pyplot as plt
import datetime
now = datetime.datetime.now()
This text imports the libraries we need to create our charts. We also get the time when the program is run that we will use later to mark when our charts haev been created. Now that we have imported the libraries we need and collected the time, we can start creating charts.
Read the moisture data into your program
If you have data for at least two days, you are ready to go. Let's start by reading our moisture data into the program we'll use to create charts. To do this, we can use the following code:
final_data = pd.read_csv("/home/james/plant-sensor/logging.csv")
final_data = final_data.groupby(["date"], as_index=False)
final_data = final_data.tail(30)
Let's talk about this code line by line. First, we read our plant data from the file logging.csv into a variable called plant_data. You should substitute the folder /home/james/plant-sensor/ for the folder where you are saving your logging.csv file. Next, we group our readings by date. This is crucial because we might take multiple readings per day and we don't want every single one to show up on our "last 30 days" chart. Grouping by date
We then retrieve the last 30 readings using the .tail(30) code.
This code doesn't actually create a chart. It just gives us the data we need.
Configure your chart
Using matplotlib, we can make a chart that shows the data we read into our program in the last section. Before we actually plot our data onto a chart—add the lines that shows our data on the chart—we are going to tell matplotlib a bit about how the chart should look. We'll tell matplotlib the title of the chart and the labels that should appear on each axis using this code:
plt.figure(figsize=(8, 6))
plt.title("Plant status (daily, last 30 days) accurate as of {}".format(now.strftime("%d %B, %Y (%H:%M:%S)")))
plt.xlabel("Time")
plt.ylabel("Moisture level")
This code creates a new chart plot (using plt.figure()) and adds a title, x axis label, and y axis label to our chart. (8, 6) tells matplotlib that our image should be 8 inches by 6 inches. We use the .strftime function to get the date and time so that we can add that to our chart title. That will make it easy for us to see when the chart was last generated. The .format function replaces the {} text with the date and time that we calculate using .strftime().
Now we basically have a blank chart. If you want to check it out, you can use this code:
plt.plot()
plt.show()
You can create your chart by running the create_charts.py program in which you have written all of your code. Here's the command you need to use to run your program (or you can run it from the Python IDLE shell if that's where you have written your code):
python3 create_charts.py
You will only be able to run this code if you are coding with your Raspberry Pi attached to a screen. If you are using the command line to create your chart, you'll need to save the chart somewhere and copy it over to your computer so you can check it out. To save a chart to a file, you can use this code:
plt.savefig("moisture_levels_30_days.png")
This code will save your chart to the file moisture_levels_30_days.png in the folder in which you have created your Python script. You can then copy that image over to a computer with a screen using either a USB drive or the scp command. You can learn more about the scp command in this excellent tutorial by the Raspberry Pi Foundation.
Your chart should look something like this:
Create a chart that shows moisture levels over the last 30 days
Okay, so we have a blank chart. You are probably wondering how you add your data to the chart. That's a great question! To do that, we need to add some code below the "plt.xlabel()" line of code that we wrote earlier. Here's the code we need to use to plot our data onto the chart we created:
ax = plt.gca()
final_data.plot(kind="line", x="date", y="m1", color="green", ax=ax)
final_data.plot(kind="line", x="date", y="m2", color="orange", ax=ax)
final_data.plot(kind="line", x="date", y="m3", color="blue", ax=ax)
plt.savefig("plant_data.png")
We use the plt.gca() code to get information about our chart axis. The final_data.plot() code adds a new line to our chart. In the plot() function, we specify:
- kind: How our data should be represented. In this case, in a line (we are creating a line graph!).
- x: The data to appear on the x axis.
- y: The data to appear on the y axis.
- color: The color of the line.
- ax: Our axis. We need to specify this value so that all of our lines are added to the same chart.
After we create our lines, we save our chart to the file plant_data.png.
Now run your create_charts.py program. If you are using your Pi to code, you should see a new file in the directory in which your create_charts.py program is located. This file will be called plant_data.png. If you open it up, you should see all of the plant data from your readings.
Hooray! Now we have created a chart that shows our plant information.
Connecting this program to our plant recording program
Right now, we have to run the create_charts.py program every time we want to generate a chart. This is not ideal because it means there is some manual work involved in creating a chart every time we want to check in on our plant.
We can make the create_charts.py program run every time that we run the recording program.
Go back to your plant_sensor.py file and add this line to the top:
import create_charts
This code lets us access the code in our create_charts file from our recording program.
Then add this line to the very bottom of the plant_sensor.py file:
create_charts.create()
This line of code will run a function called create() in our create_charts file.
Finally, we need to wrap all of the text in our create_charts file in a function called create().
Find this line in the create_charts.py file:
final_data = pd.read_csv("/home/james/plant-sensor/logging.csv")
Above this line, add the following code:
def create():
This line of code defines the create() function that we will run from our logging program.
Indent all of the lines of code below the def create(): line so that they are all part of the function.
In sum, we have:
- Created a function called create() in our create_charts.py file.
- Imported our create_charts.py code into our plant_sensor.py file.
- Added a line of code that runs the create() function when we run the plant_sensor.py program.
Now, run your plant_sensor.py file:
python3 plant_sensor.py
After the file has run, open the plant_data.png file in the same folder as the rest of your code. This file should have been updated with the latest data and should show the time at which the chart was created in the title.
Now we don't have to run two programs to record data and generate our charts: we just need to run one program, the plant_sensor.py file.
Wrapping up
So far, we have created a program that keeps track of our plant moisture levels and a program that lets us create a chart with our plant moisture levels. We have come so far! With the code we have already written, you can monitor the moisture levels in your plant at any time. But, there is one more thing we can do to improve our project: make our plant readings accessible on a web page so we can check in on them at any time. That will be the topic of our next guide.
Do you have any feedback? Is something not clear to you? Do you love the tutorial? Let me know by sending me an email at readers@jamesg.blog. I shall see you in part three of this guide.
Other posts in this series
Check out the other posts I have written as part of this series.
Responses
Comment on this post
Respond to this post by sending a Webmention.
Have a comment? Email me at readers@jamesg.blog.