#Libraries/modules
import RPi.GPIO as GPIO
from time import sleep, localtime
import datetime
import ephem
#-----------------------------------------------------------#
# General parameters #
#-----------------------------------------------------------#
#display no warnings (on command line)
GPIO.setwarnings(False)
#use RPi board pin numbers
GPIO.setmode(GPIO.BOARD)
#set pin 18 and 22 as output
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
delay_s = 60 #delay in seconds
#Get Latitude and Longitude from home
#(http://itouchmap.com/latlong.html)
home_lat = '51.5'
home_long = '5.5'
welcome_mess = "From Dusk Till Dawn v2.1 (08/2014)"
print "\n"
print welcome_mess
while 1:
#-----------------------------------------------------------#
# Determine sunrise/sunset #
#-----------------------------------------------------------#
#determine present date/time
now_double = datetime.datetime.now()
print "\n"
print "Date/Time: ", now_double
#print "hour: ", now_double.hour
#print "minu: ", now_double.minute
#determine previous/next sunset and -rise
#determine position of observer
o = ephem.Observer()
o.lat = home_lat
o.long = home_long
#define sun as object of interest
s = ephem.Sun()
sunrise = o.next_rising(s)
sunset = o.next_setting(s)
sr_next = ephem.localtime(sunrise)
ss_next = ephem.localtime(sunset)
print "next sunset : ", ss_next
print "next sunrise: ", sr_next
#-----------------------------------------------------------#
# Determine day/night #
#-----------------------------------------------------------#
if sr_next > ss_next:
#day
print "Turn off lights"
status = 0
else:
#night
print "Turn on lights"
status = 1
#Update GPIO with (new) status
GPIO.output(18, status)
GPIO.output(22, status)
sleep(delay_s) #wait