Posts Tagged ‘sunrise’

We’ll once of the first things I did was turn on the entrance lights, but here in FL, the time it gets dark can easily change during the year.

So here is how I get the sunset / sunrise time for my area. Remember Im using a Windows based system using VB 2005.

The code below basically calls a website and get’s a XML file in return from there I parse the data and extract what I need.

First are my import statements (Some may not be needed). I basically used the free tools from Earthtools.org WebServices (Excellent free service) you may also need to modify the Latitude and longitude for you area.

The variables “sunset” and “sunrise” will contain what Im looking for, from there I store it in my database and a public variable and use it thought the program.

Some of the things I do with this information is;

  1. Turn on the entrance lights 5 minutes before the garage lights.
  2. Enable the sensors to automatically turn on rooms when entered 1 hour before sunset, for example Bathrooms, Den, hallways etc. You dont want to hallway light to turn on and off its its daytime. 🙂
  3. I turn on the main kitchen light when its 30 minutes prior to sunset.
  4. Turn off all house lights when it hits sunrise on a weekend. So any ‘night lights’ like our sink light will go off and not wait till someone wakes up to turn it off.

Here is a extract of my code….

VB2005 --------------------------------------------------- start here
Option Strict Off
 Option Explicit On
 Imports System.Xml.XPath
 Imports System.IO
 Imports System.Net
 Imports System
 Imports System.Data
'------------------- FUNCTION TO READ SUNRISE /SUNSET FLORIDA AND STORE TO FILE.
Public Function Getdayinformation() As Object
 '---------------- READ AND WRITE SUNRISE / SUNSET INFORMATION FILE
Dim r As String
 Dim sunset As Object
 Dim sunrise As Object
 Dim result As Object
 Dim xml As Object
 Dim m As Object
 Dim d As Object
Dim Webclient1 As New WebClient  '  use Webclient to read string.
Dim objXML As MSXML2.DOMDocument
 Dim tempdate As Date
 Dim ObjElem As MSXML2.IXMLDOMElement
objXML = New MSXML2.DOMDocument
 xml = "http://www.earthtools.org"
 d = Trim(Str(VB.Day(Now)))
 m = Trim(Str(Month(Now)))
'---------- get future dates
 xml = xml + "/sun/28.30/-81.41"
 xml = xml + "/" + d
 xml = xml + "/" + m
 xml = xml + "/-4/0"
Try
 result = Webclient1.DownloadString(xml)
 Exit Try
 Catch ex As Exception
 Getdayinformation = False
 Exit Function
 End Try
'------------------------ PARSE THE XML STRING TO GET THE SUNRISE / SUNSET KEY'S
objXML.loadXML(result)
 ObjElem = objXML.selectSingleNode("//morning/sunrise")
 sunrise = ObjElem.nodeTypedValue
 tempdate = sunrise
ObjElem = objXML.selectSingleNode("//evening/sunset")
 sunset = ObjElem.nodeTypedValue
r = VB6.Format(sunrise) & Chr(13)
 r = r & VB6.Format(sunset)
' -------------------------------------- just sent it to a file for our webpage to read and post. :)
File.WriteAllText("data\daysettings.txt", r)
 Getdayinformation = True
 End Function

MORE AND PART #2 HERE