Simple Coop Door

This code is for a simple coop door that opens at sunrise and closes at sunset. The time is updated once a day at 2AM. This is for a Raspberri Pi 3 and two relays driving something with built in limit switches.

The Python code

#!/usr/bin/env python

Chicken Door UNTESTED
Raspberry Pi 3 Model B Rev 1.2
Simple door opener/closer based on location. Door opens at sunrise and closes
at dusk. This is designed for linear motor type of doors that have limit switches
built in. Two relays are used, one for open and one for closed. Wiring the motor
to the common terminals and 0vdc to the N/C terminals and 24vdc to the N/O terminals.
When one relay closes it sends 24vdc to the motor and the other relay is all
ready connected to 0vdc so the motor runs. When you close the second relay the
motor runs the opposite direction.

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

# Motor FWD Physical Pin 7
GPIO.setup(4, GPIO.OUT)
GPIO.output(4, False)

# Motor REV Physical Pin 29
GPIO.setup(5, GPIO.OUT)
GPIO.output(5, False)

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

# create global variables
dawn = timezone('US/Central').localize(
sunrise = timezone('US/Central').localize(
sunset = timezone('US/Central').localize(
dusk = timezone('US/Central').localize(

doorOpen = False
doorClosed = False
runtime = 0

def update():
        global dawn
        global sunrise
        global sunset
        global dusk
        dawn = pbmo.dawn(
        sunrise = pbmo.sunrise(
        sunset = pbmo.sunset(
        dusk = pbmo.dusk(

def status():
        global dawn
        global sunrise
        global sunset
        global dusk

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

        if sunrise < now and now < dusk:
                if not doorOpen:
                        GPIO.output(4, True) # Motor FWD

        if now < sunrise or now > dusk:
                if not doorClosed:
                        print "The door is closing"
                        GPIO.output(5, True) # Motor REV


        while True:

                 #the door lock is on meaning the door is moving in Auto Mode
                if GPIO.input(4): # opening the door
                        runtime += 1
                        if runtime >= 75:
                                GPIO.output(4, False) # Motor FWD
                                doorOpen = True
                                doorClosed = False
                                runtime = 0

                if GPIO.input(5): # closing the door
                        runtime += 1
                        if runtime >= 75:
                                GPIO.output(5, False) # Motor FWD
                                doorClosed = True
                                doorOpen = False
                                runtime = 0


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)

        GPIO.cleanup() # this ensures a clean exit