I want to point out that I am by no means an electronics expert. If you ask me to make a circuit diagram or explain the details of how an electronic product works, I would be clueless. In life, I have a basic understanding of how electricity works, and I just fiddled with electronic components to complete this project. That said, when using electronic devices, especially those with external power sources, one should always exercise caution. Always be careful when attempting to use electronic products and power devices!
I am not good at taking care of plants, mainly because I often forget to water them. So I started thinking, “I’m sure someone has found a way to automate this process.” It turns out many people have. That is, using Arduino or Raspberry Pi to automatically water plants. In this article, I will describe how I use Raspberry Pi and Python to automatically water my plants. Here are the components I used in this project:
-
Raspberry Pi 3 Starter Kit
-
12V Water Pump
-
12V Power Supply
-
5V Relay
-
Various Jumper Wires
-
Alligator Clips
-
Silicone Tubing
Once I had all the components I needed, it was time to do some research. Here are the resources I referenced to understand how to connect electronic devices and their workings:
-
How Electricity Works
-
How to Use Relays
-
Powering a Relay from Raspberry Pi
-
Connecting a 12V Power Supply to the Relay
This is an image of the GPIO pins on the Raspberry Pi:
First, I connected the relay to the Raspberry Pi like this:
The red wire (female-to-female) connects from the JD_VCC pin on the relay board to the 5V pin on the Raspberry Pi (pin 2). The orange wire (female-to-female) connects from the VCC pin on the relay board to the 3.3V pin on the Raspberry Pi (pin 1).
By connecting these wires, we powered the relay board. To actually control individual relays, we need to connect two more pins from the relay board to the Raspberry Pi: the GND (ground) pin and the IN1 pin.
The black wire (female-to-female) is the ground wire, connecting from the GND pin on the relay board to any ground pin on the Raspberry Pi (I used pin 34). The white wire (female-to-female) connects from the IN1 pin on the relay board to pin 12 on the Raspberry Pi. The white wire allows us to turn the relay on and off with code.
You can connect the relay and Raspberry Pi, and if you use the code I provided, be sure to remember to change the password. Once the relay board and Raspberry Pi are connected, connect the 12V adapter to the single relay. Do not connect the 12V power adapter to the wall outlet until you have everything connected properly.
The 12V power supply has a handy adapter that you can use to connect wires. I connected the red wire (male-to-male) to the positive terminal of the adapter and the brown wire (male-to-male) to the negative terminal of the adapter. Then, I added an alligator clip (female-to-male) to the brown wire.
The relay board has four independent relays, each with three outputs where you can screw in the wires. The middle output is where you want to connect the positive lead of the external power supply, and the left output is where you want to connect the positive lead of the device you want to power. This is the same for all relays on the board.
From the 12V adapter, connect the red wire (positive/power) to the middle relay output. Then, connect a new orange wire (male-to-male) to the leftmost output of the relay and attach an alligator clip (female-to-male) to the end of the orange wire.
Now we just need to connect the alligator clips to the water pump. No matter what order you connect the alligator clips to the pump, you are just setting the water flow.
I installed the water pump in a way that I want the water to flow from left to right, so I connected the black alligator clip to the pin with a red dot next to it and the other alligator clip to the pin on the pump. If I reverse the connections of the alligator clips, the water will flow from right to left.
Water flows in from the left pipe, enters the pump, flows out from the right pipe, and then goes to the plants. Finally, let’s look at the part I am really good at, the code. To interact with the GPIO on the Raspberry Pi, I used the gpiozero library. It’s really easy to use and abstracts many low-level operations you typically need to learn to start interacting with GPIO pins. You can find the documentation link here. Before we actually start writing code, you must connect the Raspberry Pi to a monitor, mouse, and keyboard, or you can connect to the Raspberry Pi via SSH. Once you log into your Raspberry Pi, navigate to your desktop and create a folder named “run.” Inside “run,” you will have another folder called “classes.” Now, in the “classes” folder, create a file named “Hardware.py” that should look like this: In this file, I only defined a new class named Relay that inherits from the OutputDevice class. Next, in the “classes” folder, create a new file named “TimeKeeper.py.” It should look like the following:
Essentially, the purpose of this class is to keep track of the current time and the last time our plants were watered. “Hardware.py” and “TimeKeeper.py” are both in the “classes” folder. Then I created a new file named “water_plant.py.” I placed it outside the “classes” folder. The “water_plant.py” file looks like this:
You can change the values of the “WATERING_TIME” and “SECONDS_TO_WATER” variables as you wish. The first variable decides when during the day the plant should receive water, while the second variable determines how long the plant receives water. I also set up email notifications so that when your plants are watered, you will receive an email, and every Friday you will receive an email reminding you to check the water level. By default, I have disabled these notifications, so the program will not crash on startup, but if you want to enable them, you must do three things: 1) Change lines 26 and 27 to include your Gmail and password 2) Uncomment lines 63, 65, and 68 3) Go here, log in with the Gmail you want to receive notifications on, and switch it to “on,” otherwise, you will receive an error when you try to send emails with your Gmail account. It is also important to note that email notifications only work with Gmail accounts. Now that all the files are set up, you should have the following directory tree structure:
You can place this “run” folder anywhere on the Raspberry Pi, but I just decided to place it on the desktop. Finally, when we want to run the “water_plant” script, I want the script to run when we turn on the Raspberry Pi, rather than having to connect it to the monitor every time we want to run it, so we can turn off the Raspberry Pi and move it anywhere. Once we turn it on, we don’t need to SSH into the Raspberry Pi or connect it to a monitor to run the script. To do this, we need to use the “crontab” command. If you open a terminal window on the Raspberry Pi, either by connecting it to a monitor or switching to the monitor, and enter the following command:
sudo crontab -e You should see something like this:
In this file, you want to add the following code snippet:
@reboot python3 /home/pi/Desktop/run/water_plant.py
Then save it by pressing CTRL+X → Y → Enter. Essentially, we are just telling the Raspberry Pi, “Hey, whenever you reboot, run the Python script ‘water_plant.py’ located in the ‘run’ folder on the desktop.” If you didn’t place the “run” folder on your desktop, just update the path where you placed the “water_plant.py” script. You can view the complete code here, or you can clone the repository with the following command: git clone https://github.com/AlanConstantino/rpi-plant-project.git. That’s it! Now you should have a working Raspberry Pi that waters your plants every 24 hours! It will even send you email reminders when your plants are watered and a reminder every Friday to check the water level.
Original English: https://blog.alanconstantino.com/articles/water-your-plant-using-a-raspberry-pi-and-python.html Translator: Yang