Things used in this project

Hardware components:
R8326274 01
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 431650 bulb single lg. v370602166
Philips hue
×1

Schematics

IMG_0188.JPG
Img 0188

Code

Watering systemPython
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

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Sorting Hat: Circuit Playground Express Starter Project
Easy
  • 136
  • 3

Protip

Which Hogwarts House do you belong in? A quick first project for this beginner-friendly board from Adafruit.

Winforms+Lattepanda=Control Everything With Your Mouse!
Easy
  • 1,151
  • 7

Protip

If you use other things instead of the LED, then you can use your mouse to control everything!!

Housing for Raspberry Pi 3
Easy
  • 203
  • 3

Full instructions

The housing I used for my Raspberry Pi I bought broke during assembly due te bad design. Ik can make a better version of it.

Secure Simple Remote Access for Camera Viewing
Easy
  • 2,796
  • 12

Full instructions

Turn your RPi into a full featured camera with remote access, end2end encryption, and easy user enrollment.

One Small Step For a Yedi, One Giant Leap for Mankind
Easy
  • 168
  • 4

Work in progress

Counting your steps with the Calliope Mini. Hearing the Star Wars theme is always a huge motivator.

Smartphone Connected Home Door Lock
Easy
  • 434
  • 4

Full instructions

An internet-connected, servo-driven deadbolt actuator that can be operated remotely using a smartphone.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login