import ephem import datetime import time import RPi.GPIO as GPIO # Define hours to open before sunrise OPEN_BEFORE_SUNSET = 14 # Setup GPIO aliases closedoor = 17 opendoor = 18 ropelights = 27 # Setup GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(closedoor, GPIO.OUT) GPIO.setup(opendoor, GPIO.OUT) GPIO.setup(ropelights, GPIO.OUT) def is_daytime(): # Determine current time now = datetime.datetime.now() # Determine next sunrise and sunset o=ephem.Observer() o.lat='45.2000' o.long='-93.5667' s=ephem.Sun() s.compute() next_sunrise = ephem.localtime(o.next_rising(s)) next_sunset = ephem.localtime(o.next_setting(s)) # Calculate next adjusted sunrise openhours = datetime.timedelta(hours=OPEN_BEFORE_SUNSET) next_sunrise_adjusted = (next_sunset - openhours) # Print Varibles for Debugging print "Checking for Daytime." print "It is currently %s." % now print "Next sunset subtract 14 hours %s." % next_sunrise_adjusted #print next_sunsrise_hr_adjusted print "Next sunrise is at %s." % next_sunrise print "Next sunset is at %s." % next_sunset # Determine if its daytime if (next_sunset < next_sunrise): return (next_sunset < next_sunrise) else: return (next_sunrise > next_sunset) def is_dawn(): # Determine current time now = datetime.datetime.now() # Determine next sunrise and sunset o=ephem.Observer() o.lat='45.2000' o.long='-93.5667' s=ephem.Sun() s.compute() next_sunrise = ephem.localtime(o.next_rising(s)) next_sunset = ephem.localtime(o.next_setting(s)) #Take now and replace hours with 1 and 9 am. This prevents us from #checking now against adjusted sunrise in case adjusted sunrise is one day ahead. today1am = now.replace(hour=1, minute=00) today9am = now.replace(hour=1, minute=00) # Calculate next adjusted sunrise openhours = datetime.timedelta(hours=OPEN_BEFORE_SUNSET) next_sunrise_adjusted = (next_sunset - openhours) # Print Varibles for Debugging print "Not Daytime, checking for Dawn" print "It is currently %s." % now print "Next sunset subtract 14 hours %s." % next_sunrise_adjusted print "Next sunrise is at %s." % next_sunrise print "Next sunset is at %s." % next_sunset print "0100 looks like this %s." % today1am print "0900 looks like this %s." % today9am # Determine if its Dawn if (next_sunrise < next_sunset): if (now > today1am) and (now < today9am): return (now > next_sunrise_adjusted) else: return (next_sunrise > next_sunset) def is_nighttime(): # Determine current time now = datetime.datetime.now() # Determine next sunrise and sunset o=ephem.Observer() o.lat='45.2000' o.long='-93.5667' s=ephem.Sun() s.compute() next_sunrise = ephem.localtime(o.next_rising(s)) next_sunset = ephem.localtime(o.next_setting(s)) # Calculate next adjusted sunrise openhours = datetime.timedelta(hours=OPEN_BEFORE_SUNSET) next_sunrise_adjusted = (next_sunset - openhours) # Print Varibles for Debugging print "Not Daytime or Dawn, checking for Nighttime." print "It is currently %s." % now print "Next sunset subtract 14 hours %s." % next_sunrise_adjusted print "Next sunrise is at %s." % next_sunrise print "Next sunset is at %s." % next_sunset # Determine if its nighttime if (next_sunrise < next_sunset): return (next_sunrise < next_sunset) else: return (next_sunrise > next_sunset) def main(): while True: # Determine if its nighttime, or daytimne and take action if is_daytime(): print "It's Daytime, open the Door and turn off the lights" GPIO.output(ropelights, GPIO.LOW) GPIO.output(closedoor, GPIO.LOW) GPIO.output(opendoor, GPIO.HIGH) elif is_dawn(): print "It's Dawn, open the Door and Turn on the Lights" GPIO.output(closedoor, GPIO.LOW) GPIO.output(ropelights, GPIO.HIGH) GPIO.output(opendoor, GPIO.HIGH) elif is_nighttime(): print "It's Nighttime, close the Door and Turn off the Lights" GPIO.output(opendoor, GPIO.LOW) GPIO.output(ropelights, GPIO.LOW) GPIO.output(closedoor, GPIO.HIGH) time.sleep(60) print "Restaring loop" main()