With a green thumb and some patience, gardening is a lovely hobby regardless if you have a few exotic plants indoors or have a full-blown outdoor garden with potatoes and perennials.
With that said, there are also times when your patience may run out when plants begin to die for what seems to be no good reason at all.
The Raspberry Pi Pico W can help to provide a solution to ensure that plants will thrive without lifting a finger at all (well, almost).
Let’s review how a plant monitor, some code, and a tiny microcontroller will keep track of your plant’s health from anywhere in your home.
Required Hardware
Surprisingly, there’s not a lot of hardware required. A lot of the magic is contained within the Plant Monitor. You really only need a few items to get started.
Although this plant monitor supports the use of alligator clips, this project utilizes the pin connectors attached to the back side of the plant monitoring device.
Setting Up the Gardening Assistant
This project involves connecting the plant monitor to your Raspberry Pi Pico W, as well as creating and manipulating code to get everything working. A web server will be required to serve up a simple webpage accessible within your home internet connection.
There are different model versions of the Raspberry Pi Pico. For this project, you will need to use a Raspberry Pi Pico W. To learn about what the Pico W is capable of, check out our guide on what the Pico W is and what it can do.
First, let’s ensure that the plant monitor is connected and working properly. Later in the article, you will tackle setting up a simple web server used to monitor your plant with any browser-enabled device connected to your home network.
Preparing the Plant Monitor
With many sensors available to purchase through various internet sites, you will come to learn that some soil sensors will wear down easily in soil and others stand up to the elements fairly well. The Monk Makes Plant Monitor is a nice option as it is not prone to corrode in soil. Not only does this monitor measure soil wetness, but it also measures humidity and temperature too.
Only four pins will need to be connected from the plant monitor to your Raspberry Pi Pico W:
- GND goes to GND
- 3V connects to 3V3 Out
- RX_IN will find its way to GP0
- TX_OUT will meet up with GP1
Once connected to power, your Raspberry Pi Pico W will be able to provide power to itself and the plant monitor. You’ll notice some lights on the hardware that confirm the device is in working order. As well, there’s an LED light that will shine green, yellow, or red (depending on the level of moisture detected in your soil).
Although the Monk Makes Plant Monitor comes with some great python modules, you’ll still need to create some simple code in order to monitor the health of your plant’s soil. You can grab the following python files from our MUO GitHub repository.
You’ll need pmon.py and test.py for the soil sensing portion and the python files microdot.py, mm_wlan.py, and pico_w_server.py will be used to complete the simple web server later.
Now is a great time to pause and refresh yourself with subtle differences between MicroPython and Python if you have not done so already.
The python file, pmon.py, creates a MicroPython Class for the plant monitor. UART will take care of the duplex data transmission and then some work converting analog to digital is also necessary. You’ll also notice the wetness, temp, and humidity functions being defined in this file as well.
def get_wetness(self):
return int(self.request_property(“w”))
def get_temp(self):
return float(self.request_property(“t”))
def get_humidity(self):
return float(self.request_property(“h”))
def led_off(self):
self.uart.write(“l”)
def led_on(self):
self.uart.write(“L”)
Next, you’ll need the test.py file obtained from our MUO GitHub repository.
You’ll notice that modules time, pmon (from PlantMonitor), and machine are required o properly monitor your plant’s health.
As the PlantMonitor module is imported, all that’s required to monitor soil conditions is a simple while loop. Also, the print command will output the soil moisture, temperature, and humidity readouts after running test.py in Thonny.
time.sleep(2)
pm = PlantMonitor()
while True:
w = pm.get_wetness()
t = pm.get_temp()
h = pm.get_humidity()
print(“Wetness: {0} Temp: {1} Humidity: {2}”.format(w, t, h))
time.sleep(1)
Don’t feel like watering your plant when the soil is too dry? Assign your pump relay to a pin on the Raspberry Pi Pico and utilize an if statement to watch for a wetness value (out of 100) to trigger your water pump, via a relay, to turn on and dispense water again.
relay1 = Pin(15, Pin.OUT)
if w = 24
relay1.value(1)
relay1(0)
You will want to do some testing in order to find that perfect balance to ensure your plant is satisfied with the amount of water it’s receiving. You can also add in another if statement to turn on a heat lamp, via a relay, if your plant is too cold.
Simple Web Server
You’ll need three python files, from our MUO GitHub repository, in order for your Raspberry Pi Pico W to broadcast the soil statistics to your home internet connections:
- microdot.py
- mm_wlan.py
- pico_w_server.py
The microdot file handles the back-end functions to create this simple HTTP-based web server and displays the python code output as a html-based webpage that can be called using the IP address of the Raspberry Pi Pico W.
The mm_wlan.py file offers a simple way to connect to a wireless network. You’ll either receive an IP address of your Raspberry Pi Pico and a connected message. If the connection was not successful, you’ll receive a connection failed message instead.
The pico_w_server.py file is where you enter the SSID (remember that the Raspberry Pi Pico W only connects to 2.4GHz SSIDs) and your Wi-Fi password. Within the HTML section, you can customize what your web server will display on a web browser. You can also remove the comments from the refresh section and tweak the interval should you not want the web page refreshing every second or so.
At the very bottom of this file, you may also customize the port as well. This is handy if you wish to expose this information to the internet outside your home.
When you run your test.py file, the required server python files (mm_wlan and pico_w_server) are imported for you. After you run the test.py file, grab the IP address if your Pi (found in the Thonny output) and add the port you’ve used (default is 80) from any web-browser that is connected to the same 2.4GHz SSID at home. You should see something like this:
In order to reduce the dependency of your connected PC, change the test.py file to main.py and save on your Raspberry Pi Pico W. You may also wish to consider connecting an LCD to your Pico so that you program the display to output the IP address (when you remove the dependency of your connected PC).
Bring Back That Green Thumb
With a sophisticated soil sensor and a simple web server you can now monitor your plant’s health from a web-browser anywhere in your home.
Feel free to tweak the code as you see fit. If you’re up for it, consider creating a soil sensing app that adds some polish to the simple web server you’ve just set up.
To make this project feel complete, add a pump and relay, along with a heat lamp, and you’ll have yourself a fully automated garden. Now you’ll be able to forever maintain your ‘green thumb’ status.