- A python written plant irrigation system.
- Uses both a 5V pump and a moisture sensor.
- Incorporates pushover notifications.
My sister is into gardening and planting and I wanted to make something that could automatically water the plants if needed. I also wanted to combine what I learnt in my previous project to incorporate a notification system when the plants have been watered or not.
After the initial setup of installing Raspbian and configuring it I first started to play around with the moisture sensor to see if I could get a reading of whether or not it was activating.
After studying the GPIO diagram I hooked up the sensor and wrote some code to detect if it was in water.
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(20, GPIO.IN)
if GPIO.input(20):
# off
print("Soil is not wet")
else:
# on
print("Soil is wet!")
Now that the moisture sensor was correctly reading I could focus on powering the pump and understanding how the relay works.
This was a bit harder than my previous projects but after watching this video I understood how the wiring worked.
I then wrote more code to power on the pump from my raspberry pi.
import RPi.GPIO as GPIO
import time
channel = 21
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)
def pump_off(pin):
GPIO.output(pin, GPIO.HIGH) # Turn pump off
def pump_on(pin):
GPIO.output(pin, GPIO.LOW) # Turn pump on
pump_on(channel)
time.sleep(5)
pump_off(channel)
GPIO.cleanup()
This code allowed me to pump the water out for 5s and then power down my pump.
After I figured out how to power the pump and get a reading for the soil sensor it wasn't too hard to get them to work in tangent.
import RPi.GPIO as gpio
import time
import sys
from pushover import init,Client
# pushover
init("<api key>")
pump_pin = 21
soil = 20
sec_to_water = 30
gpio.setmode(gpio.BCM)
gpio.setup(pump_pin, gpio.OUT)
gpio.setup(soil, gpio.IN)
def pump_off():
gpio.output(pump_pin, gpio.HIGH)
def pump_on():
gpio.output(pump_pin, gpio.LOW)
def soil_check(seconds):
if gpio.input(20):
print("Watering the plant for:" + str(seconds) + "s")
# off
pump_on()
time.sleep(seconds)
pump_off()
print("Finished Watering! Now sending push notification...")
Client("<user key>").send_message(
"Pumped water to the plants", title="Plants have been watered!"
)
gpio.cleanup()
else:
pump_off()
gpio.cleanup()
print("Sending notification plants already watered!")
Client("<user key>").send_message(
"No water pumped to plants", title="Plants have already been watered!"
)
sys.exit()
soil_check(sec_to_water)
This code allowed me to do the following:
- Check if the soil is wet or not and store it in a variable called wet.
- If !wet pump water.
- If wet then don't pump water.
I also incorporated what I learnt from my pi-mail project to add notifications to when the plant gets watered and when it is not.
In the video you can see the setup of the water tank and raspberry pi in a container with the needed components. More pics can be found in the pics folder
I also setup the linux crontab
allowing me to schedule this python command everyday at 7PM
00 19 * * * python /home/pi/plant-pi/main.py &
This project was a lot more fun than my other one as it was cool to see a water pump be powered on when I told it to.
To improve this project I could maybe:
- Add a camera to my pi and take a photo every time it was getting watered and send it to pushover.
- Maybe add a web interface with a camera to watch the plant and pump it directly from the web