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

Test Your Internet Speed Using a Raspberry Pi + Ubidots
Easy
  • 182
  • 4

Protip

Use your Raspberry Pi to measure any internet connection speed with Ubidots.

Chicken Coop Livestream
Easy
  • 1,821
  • 6

Full instructions

We have two chickens, and their coop is located at the back of the garden. I decided to build a Pi-based streaming device to check on them.

Raspberry Pi Shutdown / Restart Button
Easy
  • 5,047
  • 20

Full instructions

Building an installation project? Shut down or reboot your project safely, without a keyboard or SSH!

3d printed case for a RaspPi 3, an Arduino and a 5" display
Easy
  • 263
  • 4

Full instructions

A simple and compact frame to mount a 5" monitor, a Raspberry Pi, an Arduino, and a battery, which is extensible for other sensors.

DIY Raspberry Pi Indoor Outdoor Webcam
Easy
  • 1,280
  • 6

Full instructions

Ever want to monitor your home but don't want to buy a $100 1080p webcam? Well I did, and you can too!

MPU6050 accelerometer on Windows 10 Raspberry 2
Easy
  • 64
  • 1

Protip

Using MPU6050 accelerometer data on Windows 10 Raspberry 2 to control model's roll and pitch with DirectX12XTK and ImGui

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaFree StoreBlog