Internet Of Things with Raspberry Pi – IoT Home Automation Irrigation System

One morning I didn’t remember to water the peace lilies outside my window. By evening they had keeled over and looked terribly dehydrated… to say the least. Since there happened to be an unconnected solenoid valve connected to the pipeline anyway, it was the perfect opportunity to throw in a little home automation to fix this.

HOW IT WORKS

The irrigation system is triggered at certain fixed local times, namely, 06:00, 14:00, 14:30 and 18:00. Although these timings are far from perfect, they can and will be adjusted as the experiment progresses, depending on the plant’s observable requirements.

Externally, this signal triggers board-pin 12 of the GPIOs, which is in turn connected to an L298N motor driver. The motor driver bumps up the voltage from signal level to 12 volts. This is sufficient to turn on the relay which in turn, controls the mains power supply to the solenoid valve outside. When the relay is switched on the valve opens, releasing water into the outlet pipes.

This system was programmed in Python, on a Raspberry Pi Model 2 B over a VNC (Virtual Network Computer) cloud connection. The software in question, is called VNC Connect and it’s server-side application comes pre-installed in practically all new versions of NOOBS and Raspbian.

VNC

A VNC Server running on the Pi, allows it to be controlled from any computer with the appropriate access credentials, on the same VNC Connect (by RealVNC) account. Although it isn’t absolutely essential, it’s useful to monitor the plant’s growth and keep an eye on the outlet pipe to make sure everything is working.

Activating the Pi’s VNC Server is rather simple, as shown on this page:

https://www.raspberrypi.org/documentation/remote-access/vnc/

Following which, you can download the VNC Viewer application on any other device (for a smartphone, tablet or PC):

https://www.realvnc.com/en/connect/download/viewer/

Both softwares are part of the VNC Connect package which is free for non-commercial usage, and cross-platform compatible.

PYTHON CODE
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(12, GPIO.OUT)

while True:
        current = time.strftime("%H%M", time.localtime())
        if current=="0600" or current=="1400" or current=="1430" or current=="1800":
                GPIO.output(12, 1)
                time.sleep(60)
                GPIO.output(12, 0)
                time.sleep(1)
                print("Plants watered")
        else:
                GPIO.output(12, 0)
                time.sleep(1)
GPIO.cleanup()
OPENCV CODE
from time import sleep
import cv2
import numpy as np

cap = cv2.VideoCapture(0)

if not (cap.isOpened()):
        print("Could not open video device")

while(True):
        ret, frame = cap.read()
        cv2.imshow('image',frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
                break

cap.release()
cv2.destroyAllWindows()

SCOPE FOR EXPANSION

The irrigation system is presently capable of being manually controlled from a remote location, but more importantly, the Pi’s new cooling fan allows it to be overclocked. Needless to say, any further development on an image processing algorithm to monitor the plant’s health, would immensely benefit from it.

However, it could also use weather data APIs available online, to decide at what time and for how long the irrigation system should be switched on.

While that’s currently under development, the experiment with the peace lilies will continue.

Leave a Reply