Archive for June, 2008

Part 2.

As requested, here are some live examples,  this is of course using the SDM library in VB 2005, the same works for VB6.
Since my blog is really about my home project, a VERY GOOD site,  which is the one I used to start which has lots of specs and examples in VB6 about these commands. Its called, I wouldn’t like to re-invent the wheel.

My examples are using the SDM.SendinsteonRaw command, I find them faster since you don’t wait for a return.  For our examples we can use the source as “00 00 00” or the actually # on the PLC and the
rate is the actual intensity of the command. you can also use the sm.SendINSTEON(Device ID,command 1, command 2,hops) but it will wait for a reply.  I like to catch the response on the way back. And example using that command would be
sm.SetOnLevelText(“07.B1.12”, “100%”) – And here you DO have to use the dots. 🙂  A list of most of the commands that can be used when the SDM is properly load are here

We’ll use as examples for our program that the device we want to control is

In our program previously defined,
You can download the SDM device manager here.  Also some information regarding the PLC can be found here (This is the one I have) – worth every $

Option Explicit on
Friend WithEvents Sm As SDM3Server.SDM3

 Public Sub MainMenu_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
 Sm = New SDM3Server.SDM3  - Create a instance of the SDM using (SM)

 Dim Mac as string = "11 22 BB"
 Dim Rate as integer = 255

Mac = “11 22 BB”
Rate = Intensity of the device (Since we are using raw commands this runs from 0,256 and needs to be converted to HEX”
The last #3, which is the Hops= I keep as 3 (Always works for me.)

Notes: All values are in HEX, also you need to put spaces in your device address, in my case I pull them off a SQL Database but they come in as “11 22 33” if you put (.) dots or any other characters the command wont work.

Turn onSm.SendINSTEONRaw(“00 00 00 ” + Mac + ” 05 11 ” + Hex(Rate), 3) – Turn on device to the desired rate to 255 is the same as Sm.SendINSTEONRaw(“00 00 00 ” + Mac + ” 05 11 FF“), 3) – Turn on 100%
This will turn on the device at the predefined RAMP rate so if you have setup a slow ramp rate this is the one to use.  For example ICON Dimmers have no ramp rate (options) so this will turn on at .1 seconds

Turn on fastSm.SendINSTEONRaw(“00 00 00 ” + Mac + ” 05 12 ” + Hex(Rate), 3) – Turn on device to the desired rate to 255 is the same as Sm.SendINSTEONRaw(“00 00 00 ” + Mac + ” 05 11 FF“), 3) – Turn on 100%
This will turn on the device at fast redgardless of the  predefined RAMP rate.

Turn offSm.SendINSTEONRaw(“00 00 00 ” + Mac + ” 05 13 00″, 3)
This will turn off the device at the predefined RAMP rate so if you have setup a slow ramp rate this is the one to use.

Turn off fastSm.SendINSTEONRaw(“00 00 00 ” + Mac + ” 05 14 ” + Hex(Rate), 3) – Turn on device to the desired rate to 255 is the same as Sm.SendINSTEONRaw(“00 00 00 ” + Mac + ” 05 11 FF“), 3) – Turn on 100%
This will turn on the device at fast redgardless of the  predefined RAMP rate. For example ICON Dimmers have no ramp rate (options) so this will turn off at .1 seconds

Brighten up one step sm.SendINSTEONRaw(“00 00 00 “+Mac+” 05 15 00″, 3)
Each device has 32 levels of so this will just bump down or up one.  Good for dimming without knowing the previous value.  You can dim to 0.

Dim down one step sm.SendINSTEONRaw(“00 00 00 “+Mac+” 05 16 00″, 3)
Each device has 32 levels of so this will just bump down or up one.  Good for dimming without knowing the previous value.  You can dim to 0.

Get status of device – sm.SendINSTEONRaw(“00 00 00 11 22 BB 05 19 00″, 3)
Using Alternet command sm.GetOnLevelText “11.22.BB”

Sending a Inteson group message; Groups allow you to send messages to a set of devices (1,2,10,100,200 with one command) Only bad things is you dont get a reply from the devices
I have found I had to run this twice sometimes but (We’ll get to groups later) .

Turn off group #20 (Remember HEX!) – sm.SendINSTEONRaw(“00 00 00 00 00 20 CF 13 00″, 3) ‘ SEND broadcast group 20 OFF!

Turn on group #20 – sm.SendINSTEONRaw(“00 00 00 00 00 20 CF 11 FF”, 3) ‘ SEND broadcast group 20 ON

The PLC also send X10 commands, for this I use theSendX10 command, this command WILL pause and wait for a response. For example

Sm.SendX10(“D02,DON”) ‘ TURN ON Device D02
Sm.SendX10(“D02,DOFF”) ‘ TURN ON Device D02

Lets say you want to check a email account every couple of minutes and this was react such as run a couple of commands and email you back?

For example I have already setup

  • Status checks of all lights
  • Turn on or off the whole house or individual lights
  • Randomize the house lights which will turn on my media player and play music remotely as well.
  • Open / close the garage,
  • Email me when movement is detected after I’ve turned the house off
  • The sky’s the limit.

What im currently using is outlook checking a pop3 account every 5 min, since Mapi interfaces really good with VB I use this. This way outlook itself checks every 5 minutes and you code just checks the email every 3. Again my code is Vb2005 / Windows

First setup outlook as your normally would, make sure it checks your email every 2-3 minutes. Then in your code, you will need to make sure you first reference the “Microsoft Office object 12 or 11 “ library into your code. Depends your version it may have object version 11 I’ve used this with Outlook 2000,2003,2007. I suspect by a macro you wont need to have outlook running but in my case its no biggie.

In your imports section include the following

Friend Class MainMenu

In your class definion add the following, i've commented each line

‘——– SET UP EMAIL goodies

Friend Class MainMenu
    Public inboxitem As Microsoft.Office.Interop.Outlook.MailItem  '--- we will use this object to send emails out
    Public objOutlook As Microsoft.Office.Interop.Outlook._Application ' --- Reference the outlook object, and load it if not already
    Public objNS As Microsoft.Office.Interop.Outlook._NameSpace '--------- The Outlook namespace, used throught the code.
    Public objInBox As Microsoft.Office.Interop.Outlook.MAPIFolder'------ The folder object that we will assign the inbox to.
    Public objItems As Microsoft.Office.Interop.Outlook.Items'-------- Each of the items in the inbox, we will loop thru this to read the individual emails.

Then once the program starts we need to set the session that will hold the connection to outlook, this is done in the Load event

Public Sub MainMenu_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
objOutlook = New Microsoft.Office.Interop.Outlook.Application()  '--- Assign the variable objOutlook to the application
objNS = objOutlook.Session '--------- Tie to the active session, if its not running it will load automatically

Then you can set a timer to check your email every certain amount of time executing the function CheckMail()

In the function that will do the actual reading, we will start with the assigning of variables and then check the emails and search for special text inside the body of the emails.

Public function Checkemail() as Boolean

objInBox = objNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox) ' --- Assign ObjInBox to map to the default inbox.
objItems = objInBox.Items.Restrict("[UnRead]=True") '----- assign individual items BUT filter only the UNREAD ones!

Dim outmail As Microsoft.Office.Interop.Outlook.MailItem  '---- This will hold the outgoing mail object
Dim iCount As Integer = objItems.Count  '------ Will hold the amount of NEW email, will return 0 if all email has already been read.

 For i = 1 To iCount

           inboxitem = objItems.Item(i)  '----------- ASSIGN A EMAIL ITEM TO THE INBOXITEM, from there we can query stuff like date,subject sender etc.
            If InStr(LCase(inboxitem.Body), "ouse on") > 0 Then  ' --- check for certain words in the Body, in this case 'house on' this also works for VB6
                Occupy() ' ---- Run another special function or code somewhere else

                '------ Here we create our reply email. ------------------
                outmail = objOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
                outmail.To = inboxitem.SenderEmailAddress '-------- Get the sender email for the inboxitem object.
                outmail.Subject = "* Reply"
                outmail.Body = "NICE TO HEAR FROM YOU, HOUSE ON. WAITING YOUR ARRIVAL."
            End If
 inboxitem.UnRead = False '- Mark email as read so its not checked twice next time
next i

Checkemail=True '----- Just to return something

End Function

And thats it!. If you like you can add attachments, HTML text and others, I would checkout Microsofts site for more details on what you can do with the outlook client!.


In my hallway I have a good ‘ol three way switch this is how I changed it. Just like in my previous entry, you can have switches controlling other switches by creating scenes. So a 3 way switch is basically that. Two switches controlling each other.

In 3 way switches you have a primary and secondary, the primary is the one which actually carries the load to the light fixture. The secondary just follows. But you may find the case (like me) where the secondary switch box doesnt have any power, since in traditional switches the power is fed from the primary.

Since there are a two wires connecting the primary to secondary (known as the traveler) we will jump the black wire to the line and use it to power the secondary switch. In my case it was easy to find the traveler wires since 1st it will be in a strand with 2nd traveler and it wont be with the line or neutral.

Once setup, and power ‘on’ you link both of them to each other and create a ‘scenes’ and each dimmer switch holds plenty of them, So you can with one switch control all the others (without needing a computer) , but not only turn on how about turn on to a certain level, and even control the ramp rate (Which is how fast the light turns on) – A very neat effect!. And presto you have created your virtual 3 way switch, the response is very fast. And technically you can add more if you like. I cant imagine a 10 way switch. 🙂 but technically you can do it, all this without a dedicated computer.

Here is the wiring diagram off the Smarthome page for it.


As for reading them in the program I just look for one of them, since eventually both will respond.

See ya


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 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
Public Function Getdayinformation() As Object
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 = ""
 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"
 result = Webclient1.DownloadString(xml)
 Exit Try
 Catch ex As Exception
 Getdayinformation = False
 Exit Function
 End Try
 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


Ok, Ive got switches turning on and off, but I want more!. More googling and browsing around the sites I found that there is even devices to talk and read what the insteon network is doing. This is also available for X10 but what really interested me was the ability to get the status of the device, for example when you turned it on, you can get a confirmation that it really was received and really ‘on’.? After saving up a little I ended up purchasing the serial interface. They come also in USB and even modem versions. But what interested me was I like to be able to get the direct connection with the device and bypass all the usb stuff plus I had the ability to download ‘timers’ if the PC wasn’t going to be used all the time. Below is a picture of the unit. It includes all the necessary hardware. including the rj45 to serial cable.

The best part is you can download the PC software (referred to as SDM) , and all is done thru COM objects. The way COM objects work is that you can reference the device by any programming language it and it automatically sorts strings back to you (In a sort of way). A very good site which has even examples that got me started can be found here. I’ve even posted some of the code below which I expanded for VB 2005 so I’ll put some example referencing the object. It you look at any of the switch and even the external ones you’ll see the 3 Hex characters, each unique to each device. So once you start see the data flow you will see these addresses inside of them, some as responding and some as replying. Once you plug in the code you will see what I mean, I have found using a excel sheet to keep all these addresses makes like much easier.

1st you Google “Smarthome SDM” and you’ll find direct downloads to it. Its a regular windows install package

2nd Reference the object in your program, its called ‘Smarthome Device Manager’

3rd Add to your code to reference it as well as to be able to catch the events, example

Class MainMenu
Friend WithEvents Sm As SDM3Server.SDM3

When I load my mainmenu even I have the following
Public Sub MainMenu_Load(...... )
  Sm =New SDM3Server.SDM3
 End Try

Now you can catch events from the device, the main event to catch and look for is.

Public Sub sm_OnText(ByVal strInsteonStatus As String) Handles Sm.OnText

var = Split(strInsteonStatus, "=")
Select Case LCase(var(0))

	Case "setonleveltext"
		data = Split(var(1), ",")
		device = data(0)
		value = data(1)

	Case "receiveinsteonraw"
		data = var(1)
		bytes = Split(data," ")
		If LBound(bytes) = 0 And UBound(bytes) = 9 Then
			AddrFrom = bytes(1) & bytes(2) & bytes(3)
			AddrTo = bytes(4) & bytes(5) & bytes(6)
			Flags = bytes(7)
			Command1 = bytes(8)
			Command2 = bytes(9)

			If AddrFrom="05BBAF" And AddrTo="05FB8E" And Command1="11" Then
				MsgBox "Received Button 4 On"

			ElseIf AddrFrom="05BBAF" And AddrTo="05FB8E" And Command1="13" Then
				MsgBox "Received Button 4 Off"

			End If
		End If
	Case Else

End Select

This will bring back all the sent,received strings, you later then have to sort them out.

I used some of the previous mentioned wepage’s code and expanded from that.

Best way is to watch the strings go by.Here is a pic of mine installed and ready to go.

* Note that you do need to ‘link’ the switches to your PLC or computer interface, even though you aren’t really turning it on but its the only way I was able to get it to see the commands.

Here are some examples (Lets say a switch has the id AA.BB.CC and my PLC (Computer interface is ) “11.22.33”

#receiveinsteonraw=02 AA BB CC 11 22 33 45 11 01  —- device acknowledging its been turned on (Notice 11 for on)

#receiveinsteonraw=02 AA BB CC 11 22 33 45 12 01  —- device acknowledging its been turned on double pressed up(Notice 12 for FAST on)

#receiveinsteonraw=04 AA BB CC 11 22 33 25 13 00 – device acknowledging its been turned off (notice the #13)

#receiveinsteonraw=04 AA BB CC 11 22 33 25 14 00 – device acknowledging its been turned off  with double tap down (notice the #14)

2nd Part here. MORE HERE

So after much thought I’ve decided to put down what i’ve been working on for the last couple of months. One thing we (wife and myself) have always been interested in home automation. Now that we are proud home in owners now is my chance.

After poking around and reading about X10, (which has been out for over 20 years), I was a bit sad to see that it not 100% reliazble, and I didn’t want to run into the many problems.

Even after to many years out. X10 works, basically by using the existing power lines to send messages, sounds simple right but here is the catch. And many devices you can find on ebay extremely cheap!. But as usual there is a small little detail, many home have 2-110v legs going into the home , so MANY,MANY times it wont reach its destination and eventually work sporadically.

Sure, there are fixes and other ‘patches’ but at the end it sounds like 1000 gadgets to have a simple switch working, and when you have many components like that something is bound to fail sometime. With the couple of X10 devices I have troubleshooting that is a nightmare. Not to complain but many x10 switches arent really a compelment to a room, and I was not looking to worsen the look of the switch. Which is what I wanted to avoid, but don’t be fooled its not that x10 is bad and I have used it for a couple of good things which you’ll see here.

So in comes Smarthome, and I was reading how their technology called Insteon, uses the PowerLine and “RF” to talk to devices, and better yet, each device acts as a repeater, so in theory the more you have installed the better your network gets! (This has proven true in my case) . But since it’s around 3 years old, new products are still being made, but as mentioned before its still compatible with the older x10 technology so you can get great gadgets such as remotes for like $4 on ebay!.

So lets start.

My first starter kit was a outdoor lighting set I purchased called FreeWire which was Black and decker’s take on x10 for less than $40, which contained a couple of lamp dimmers and a simple table remote.


The way you configure “x10 devices” is you have a “House” code which run from A-P and a unit code from “1-16.” And you assign your devices on one of those two. Neat, and simple! Inmediatly I found that plugging in the base into one ‘leg’ of the house it wasn’t able to power up one of the modules. (Damn.. just what was reading about…) oh well it still gives anyone a good start. At least I got some good outdoor modules which are rain proof.

So on to Insteon! (The grand daddy)

There were different starter kits out there, I went for the cheapest one, $99. which is shown in the picture below,

Kits comes with the desk remote, two lamp dimmers and two couplers. Installation was a breeze, no how codes, no #1-16. Basically you tap/tap to configure them, each devices has its own memory and mind. You keep one pressed until it flashes and walk to the one you want to control and wait for it flash and presto!! Better yet once you install the two included couplers I had no problems installing the devices and reaching them anywhere in the house!! This was so easy I first did our bedroom, no more need to jump off the bed to turn off the light.! I even got an additional controller off ebay for $25. New at this time of writing go for $34. Worth each penny.

Ok I’m now hooked! Once of the nice features of these little devices is that you can control what they call the ramp rate., so you can slowly fade the light on and off intervals from .1 second to minutes!!!. Note: This doesn’t apply to CFL bulbs (Unless they are the dimmable types)

So I’ve little by little i’ve gone ahead and changed the switches of the house and hopefully make life a little easier. Right now there are two types of switches out there, what I call the expensive (Smarthome ) and the not so expensive ones (Icons). I love both of them and both come in Dimmer and on/off format. Below you can see from left to right, the first is the Icon On/Off switch followed by the dimmer, a regular on/off and another Dimmer.

Reading and experimenting more, which will talk about, Insteon like x10 can do ‘groups’ where a single button can control more than one device! (But we will get to that later)

A couple of weeks go by and its very cool to be able to turn off a light switch from a remote. But I wanted more!

Here’s the specs off of the Smarthome site on how to install them. Basically you replace the existing one and included the neautral as well.

I’ll include pics of my next install so you can see how it looks in the real world. Once you get used to it, you can install them in no time!

Here is a pic of the back of the dimmer

Here is a ‘external’ dimmer device which can dim / turn on /off any device on it. Your lamp would connect on the bottom. And the devices even has a pass thru non-controled plug so you dontloose your outlet. Im stocking up on these for the chirstmas decorations this year!

Keep posted part #2 coming up. Now that we got the basic’s lets dig in deeper!