Coop Automation

jthornton

Free Ranging
6 Years
Aug 30, 2017
5,037
10,305
682
Poplar Bluff, MO
My Coop
My Coop
For the tinkerers here I've decided to move away from the Ardunio Nano and ESP8266 to Single Board Computer (SBC) to control the chicken coop automation. Please spare me your tales of whipping out your CC and ordering a door. I went with the Raspberry pi 3 and I'm quite happy with the device and the programming. So much easier to program than the Nano and ESP8266.

I've tried to attach the code but can't figure out how so I put it in the message for now.

Code:
#!/usr/bin/env python

# GPIO.VERSION '0.6.3'
# Raspberry Pi 3 Model B Rev 1.2

import RPi.GPIO as GPIO
import schedule
import astral
import datetime
from pytz import timezone
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(True)
GPIO.setup(4, GPIO.OUT) # Motor FWD
GPIO.output(4, False)
GPIO.setup(5, GPIO.OUT) # Motor REV
GPIO.output(5, False)
GPIO.setup(6, GPIO.OUT) # Door Lock
GPIO.output(6, False)
GPIO.setup(7, GPIO.OUT) # Lights
GPIO.output(7, False)
GPIO.setup(26, GPIO.IN,pull_up_down=GPIO.PUD_DOWN) # Up Door Switch
GPIO.setup(27, GPIO.IN,pull_up_down=GPIO.PUD_DOWN) # Down Door Switch

# Construct our location.  Longitude west and latitude south are negative
coordinates = ["Poplar Bluff", "USA", 36.763084, -90.413871, "US/Central", 110]
pbmo = astral.Location(info=(coordinates))
pbmo.solar_depression = "civil"

egglight = 840 # minutes of daylight desired
timeformat = "%I:%M %p"

sunrise = timezone('US/Central').localize(datetime.datetime.now())
sunset = timezone('US/Central').localize(datetime.datetime.now())
lighton = timezone('US/Central').localize(datetime.datetime.now())

def update():
    global sunrise
    global sunset
    global lighton
    sunrise = pbmo.sunrise(datetime.date.today())
    sunset = pbmo.sunset(datetime.date.today())

    # amount of daylight in HH:MM:SS
    daylight = sunset - sunrise
    print "Daylight {}".format(daylight)

    lightmin = daylight.seconds / 60
    if egglight > lightmin:
        extralight = egglight - lightmin
        lighton = sunrise - datetime.timedelta(minutes=extralight)

    now = timezone('US/Central').localize(datetime.datetime.now())

    print "Lights On {}".format(lighton.strftime(timeformat))
    print "Sunrise {}".format(sunrise.strftime(timeformat))
    print "Sunset {}".format(sunset.strftime(timeformat))

def status(runtime):
    global sunrise
    global sunset
    global lighton
    now = timezone('US/Central').localize(datetime.datetime.now())
    #print now.tzinfo
    #print lighton.tzinfo
    print(datetime.datetime.now().strftime("%H:%M"))
    #print lighton < now
    #print now < sunrise
    #print now > sunrise
    #print now < lighton
    #print now > sunset

    if lighton < now and now < sunrise:
        print "The light should be on"
        GPIO.output(7, True) # Lights

    if now > sunrise:
        print "The light should be off"
        GPIO.output(7, False) # Lights

    if lighton < now and now < sunset:
        if not GPIO.input(26) and runtime < 60: # Up Door Switch
            print "The door is opening"
            GPIO.output(4, True) # Motor FWD
            GPIO.output(6, True) # Door Lock

    if now < lighton or now > sunset:
        if not GPIO.input(27) and runtime < 60: # Down Door Switch
            print "The door ise closing"
            GPIO.output(5, True) # Motor REV
            GPIO.output(6, True) # Door Lock

    if GPIO.input(26): # Up Door Switch
        print("The Door is Open")
    else:
        print("Pin 26 is LOW")

    if GPIO.input(27): # Down Door Switch
        print("The Door is Closed")
    else:
        print("Pin 27 is LOW")

    if GPIO.input(4): # Motor FWD
        print "GPIO 4 is HIGH"

def motor(): # monitor the motor when moving
    pass
update()

runtime = 0
schedule.every(1).minutes.do(status, runtime)
schedule.every().hour.do(update)
schedule.every().day.at("02:00").do(update)

try:
    while True:
        schedule.run_pending()
        #global runtime
        if GPIO.input(6): # the door lock is on meaning the door is moving
            runtime += 1
            print runtime

        if runtime >= 60:
            GPIO.output(4, False) # Motor FWD
            GPIO.output(5, False) # Motor REV
            GPIO.output(6, False) # Door Lock

        if GPIO.input(4) and GPIO.input(26): # Motor FWD and Up Door Switch
            GPIO.output(4, False) # Motor FWD
            GPIO.output(6, False) # Door Lock
            runtime = 0

        if GPIO.input(5) and GPIO.input(27): # Motor REV and Down Door Switch
            GPIO.output(5, False) # Motor REV
            GPIO.output(6, False) # Door Lock
            runtime = 0

        if not GPIO.input(4) and not GPIO.input(5) and not GPIO.input(6):
            if GPIO.input(26) or GPIO.input(27):
                runtime = 0

        time.sleep(1)

except KeyboardInterrupt:
    # here you put any code you want to run before the program
    # exits when you press CTRL+C
    print "\nKeyBoard Interrupt"

except Exception,e:
    # this covers all other exceptions
    print str(e)

finally:
    GPIO.cleanup() # this ensures a clean exit

JT
 
I used to be an automation engineer and industrial electrician but retired a long time ago. That was integrating industrial grade logic controllers and robotics, etc..
I just took up tinkering with the Arduino uno and thought it would do the job for doors, lights, pumps, ventilation, etc.
What are the main advantages of the Raspberry over the Arduino?
 
Last edited:
Awesome! My husband plans to put a Raspberry Pi in the coop once we have it finished, for something like this (though I'm not sure we'll do the automated door). I think he had plans for dimming the light to more closely match sunrise/sunset, by monitoring the levels of natural light at various times of year (We are also both software engineers).
 
I used to be an automation engineer and industrial electrician but retired a long time ago. That was integrating industrial grade logic controllers and robotics, etc..
I just took up tinkering with the Arduino uno and thought it would do the job for doors, lights, pumps, ventilation, etc.
What are the main advantages of the Raspberry over the Arduino?

The two huge advantages the RPi has over the Arduino is you can log into the RPi with SSH and monitor things and change things. Another advantage is you can write and execute programs from another PC. Another advantage is you can plug in a HDMI monitor, USB mouse and USB keyboard and use the RPi just like any other Linux PC.

The Arduino you can't even tell what is loaded lol or if it is running.

JT
 
Awesome! My husband plans to put a Raspberry Pi in the coop once we have it finished, for something like this (though I'm not sure we'll do the automated door). I think he had plans for dimming the light to more closely match sunrise/sunset, by monitoring the levels of natural light at various times of year (We are also both software engineers).

You can rip the relevant parts out of the above code and add a 3.3v relay to turn on the lights based on how many minutes of daylight you want them to have and they will go off at sunrise. Sunset is natural lights out. The astral part along with the lat/long for your location and you get all the times of sun you need.

JT
 
I finally got the OLED working with the RPi and I'm a happy guy. I can now verify that the RPi has the correct time and other things . Sorry for the crappy photo.
 

Attachments

  • RPi-01.jpg
    RPi-01.jpg
    707.8 KB · Views: 30
Neat! Do you have a separate box where you keep your electronics? How do you power it? From the house? We built a separate box off the side of the coop where we plan to keep our electronics--It is far enough from the house that we were going to try to run it off a golf cart battery or something.
 
Neat! Do you have a separate box where you keep your electronics? How do you power it? From the house? We built a separate box off the side of the coop where we plan to keep our electronics--It is far enough from the house that we were going to try to run it off a golf cart battery or something.

Right now the RPi is sitting on my table while I mess with the programming. I have a section below the next boxes for my electronics. I have an extension cord run over to the coop.

JT
 
This is great. I'm not familiar with python but I'm definitely gonna learn.

Python is the easiest IMHO language to program in. If you have a spare PC of any type laying around download Linux and put it on that PC. I hang out on the IRC Freenode #linuxcnc if you want to chat.

I added some more content to my Raspberry Pi section this morning.
http://gnipsel.com/raspberrypi/index.html

Chicken specific programming and other info will be in the Chicken section.
http://gnipsel.com/chickens/index.html

JT
 
Last edited:

New posts New threads Active threads

Back
Top Bottom