Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Alexander Zalburg
Published

Automatic Plant Watering system

Watering plants based on temperature, soil and air humidity. Automated lightning based on light sensor. Time-lapse photo included.

BeginnerFull instructions provided26,732
Automatic Plant Watering system

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Soil Humidity sesnor
×3
Temperature and Humidity sensor
×1
Ambient light sensor
×1
Water pump
×1
Webcam
×1
Philips hue
Philips hue
×1

Story

Read more

Schematics

IMG_0188.JPG

Code

Watering system

Python
This is a code that run as a service on Raspberry Pi 2
#! /usr/bin/env python

# Python Code to communicate with MCP3008

#Import SpiDev wrapper and Sleep function from time
import sys
import spidev
import Adafruit_DHT as dht
from datetime import datetime
from time import sleep
from phue import Bridge
import logging
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
logging.basicConfig()
# Taken from phue Basic Usage guide
b = Bridge('192.168.1.103') # IP of my Hue Bridge
# If the app is not registered and the button is not pressed,
# press the button and call connect() (this only needs to be run a single time)
b.connect()
#Establish SPI Connection on Bus 0, Device 0
spi = spidev.SpiDev()
spi.open(0,0)
filename = "results.txt"
def check_plant(channel):
    #Check valid channel
    if((channel > 7) or (channel < 0)):
        return -1

    #Perform SPI transaction and store returned bits in 'r'
    r = spi.xfer([1, (8+channel)<<4, 0])
    
    #Filter data bits from returned bits
    adcout = ((r[1]&3) << 8) + r[2]
    percent = 100-int(round(adcout/10.23))
    #Print 0-1023 and percentage
    #print("ADC Output: {0:4d}   Percentage: {1:3d}%".format(adcout,percent))
    #print # of flower & soil humidity %. % is reversed.
    print("Flower ", channel, ". Soil humidity is: {1:3d}%".format(adcout,percent))
    print >> f, 'Flower ', channel, ". Soil hunidity is: {1:3d}%".format(adcout,percent)
    #checking level of soil humidity
    if(percent < 55):
        print("Soil humidity is low. Activating water pump.")
        print >> f, "Soil humidity is low. Activating water pump."
        GPIO.output(23, 1)
        GPIO.output(24, 0)
        #waiting for 5 seconds to complete watering & switching pump off
        sleep(5)
        GPIO.output(23, 0)
        GPIO.output(24, 0)
        print("Plant watering is finished")
        print >> f, "Plant watering is finished"
    else:
        print("Soil humidity is ok.")
        print >> f, "Soil humidity is ok."
def check_temperature(channel):
    #Check valid channel
    if((channel > 7) or (channel < 0)):
        return -1
    #Reading parametrs from temperature/hunidity sensor
    h,t = dht.read_retry(dht.DHT11, 4)
    print ("Temp={0:0.1f}*C Humidity={1:0.1f}%".format(t,h))
    print >>f, "Temp={0:0.1f}*C Humidity={1:0.1f}%".format(t,h)
def check_light(channel):
    #Check valid channel
    if((channel > 7) or (channel < 0)):
        return -1

    #Perform SPI transaction and store returned bits in 'r'
    r = spi.xfer([1, (8+channel)<<4, 0])
    
    #Filter data bits from returned bits
    adcout = ((r[1]&3) << 8) + r[2]
    percent = 100-int(round(adcout/10.23))
    #checking current time
    current_hour = datetime.now().hour
    #if it is night - printing a message that light are switched off
    if((current_hour < 10) or (current_hour > 21)):
        print("Light is swithced off during night")
        print >> f, "Light is swithced off during night"
        b.set_light(6,'on', False)
    else:
        #If amount of light is low - switching on the light bulb to max brightness
        if(percent < 70):
                b.set_light(6,'on', True)
                b.set_light(6,'bri', 254)
                print("Lights are switched on")
                print >> f, "Lights are switched on"
        #else - switchng light bulb off
        else:
                b.set_light(6,'on', False)
                print("Lights are switched off")
                print >> f, "Lights are switched off"
while True:
    f = open(filename, 'w')
    print >> f, datetime.now()
    check_temperature(0)
    check_plant(1)
    check_plant(2)
    check_plant(3)
    check_light(0)
    f.close()
    sys.exit()

Credits

Alexander Zalburg
1 project • 8 followers
Contact

Comments

Please log in or sign up to comment.