Advertisement
Deddieslab

Rpi_buitenlicht_v21.py

Aug 17th, 2014
8,520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. #Libraries/modules
  2. import RPi.GPIO as GPIO
  3. from time import sleep, localtime
  4. import datetime
  5. import ephem
  6.  
  7. #-----------------------------------------------------------#
  8. #                      General parameters                   #
  9. #-----------------------------------------------------------#
  10.  
  11. #display no warnings (on command line)
  12. GPIO.setwarnings(False)
  13. #use RPi board pin numbers
  14. GPIO.setmode(GPIO.BOARD)
  15. #set pin 18 and 22 as output
  16. GPIO.setup(18, GPIO.OUT)
  17. GPIO.setup(22, GPIO.OUT)
  18.  
  19. delay_s     = 60 #delay in seconds
  20.  
  21. #Get Latitude and Longitude from home
  22. #(http://itouchmap.com/latlong.html)
  23. home_lat  = '51.5'
  24. home_long = '5.5'
  25.  
  26. welcome_mess = "From Dusk Till Dawn v2.1 (08/2014)"
  27. print "\n"
  28. print welcome_mess
  29.  
  30. while 1:
  31.  
  32.      #-----------------------------------------------------------#
  33.      #                Determine sunrise/sunset                   #
  34.      #-----------------------------------------------------------#
  35.  
  36.      #determine present date/time
  37.      now_double = datetime.datetime.now()
  38.      print "\n"
  39.      print "Date/Time: ", now_double
  40.      #print "hour: ", now_double.hour
  41.      #print "minu: ", now_double.minute
  42.  
  43.      #determine previous/next sunset and -rise
  44.  
  45.      #determine position of observer
  46.      o = ephem.Observer()
  47.      o.lat  = home_lat
  48.      o.long = home_long
  49.      #define sun as object of interest
  50.      s = ephem.Sun()
  51.      sunrise = o.next_rising(s)
  52.      sunset  = o.next_setting(s)
  53.  
  54.      sr_next = ephem.localtime(sunrise)
  55.      ss_next = ephem.localtime(sunset)
  56.    
  57.      print "next sunset : ", ss_next
  58.      print "next sunrise: ", sr_next
  59.  
  60.      #-----------------------------------------------------------#
  61.      #                    Determine day/night                    #
  62.      #-----------------------------------------------------------#
  63.      
  64.      if sr_next > ss_next:
  65.        #day
  66.        print "Turn off lights"
  67.        status = 0
  68.      else:
  69.        #night
  70.        print "Turn on lights"
  71.        status = 1
  72.  
  73.      #Update GPIO with (new) status
  74.      GPIO.output(18, status)
  75.      GPIO.output(22, status)
  76.      
  77.      sleep(delay_s)   #wait
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement