Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Controlling Your Lights with Your PC

Chris Walker
ControlThink LC

Difficulty: Easy

Time Required: 1-3 hours

Cost: $50-$100

Software: Z-Wave PC SDK

Hardware: Z-Wave PC SDK

Download:

December 20, 2006

Controlling your lights with a remote is pretty cool, but doing it from your PC opens up a whole world of fun possibilities.

In this article, we'll add your computer to the Z-Wave network which we set up in the previous article. We'll run software to demonstrate how to set up and activate scenes with the PC. And then we'll see how easy it is to write our own code and totally personalize the system.

What you need

For this project, you'll need a copy of ThinkEssentials, our Z-Wave PC SDK, and a compatible Z-Wave USB Stick. Here, we'll use the Z-Wave PC SDK bundle which includes all three. You can get it for $69 from http://www.controlthink.com.

usbstickIntermatic's HA22 USB Stick; we'll plug this into our computer.

Once you have the kit, simply plug the Z-Wave USB stick into your PC. If a USB extension cable was provided, plug the USB stick into it instead and then move the USB stick away from walls and metal objects for best reception. Finally, install the USB stick drivers and both software products (all of which are on the CD).

Now that the software and hardware are installed, we're ready to get started.

Adding your computer to the Z-Wave network

To add your computer to the network we set up in the previous article, we'll use the ThinkEssentials software (although you can also join a network with a line or two of code—see the Z-Wave PC SDK Primer sample code ). Go ahead and start the software up now. Then, switch to the Advanced Settings tab on the left side of the screen.

If you haven't set up your Z-Wave network with a remote already, you could set it up with your PC instead--using the ThinkEssentials software or by writing some code. If you'd rather go that route, you can find full details in the Z-Wave PC SDK Primer .

AdvancedSettingsTab.gif
The ThinkEssentials Advanced Settings tab; we'll add our computer to the network here.

On the Advanced Settings tab, click the “Join Existing Network” button. This will put our USB stick into a mode where it can be added into an existing network. On your HA09 (Tabletop Remote), flip open its lid and press and hold the INCLUDE button for a few seconds until it displays “COPY”. If you have an HA07 (Handy Remote) instead, hold down INCLUDE until the red and green LEDs start flashing. Finally, press the CHANNEL 1 ON button to start adding your PC to the network. This process may take five or ten seconds to get started.

joinnetwork.jpg
The JOIN EXISTING NETWORK button (left); holding the INCLUDE button (right)

Once your computer is added to the network, ThinkEssentials may close automatically and save the changes. Simply restart the software.

Controlling your devices from the PC

Switch to the Home tab in ThinkEssentials, and you'll see icons for each of your devices at the bottom-right corner of the window. To turn them on or off, simply click on them. Go ahead, try this now. If you have lamp modules or dimmable light switches, simply right-click on them for dimming options.

If you'd like, you can also draw your home's floor plan, set up mood lighting (scenes), set up scheduled events for those scenes, etc. I'll leave these to your experimentation; if you need any guidance, a shortcut to the User's Guide is included in the Start Menu Programs folder alongside the software.

Turning on lights from Visual Basic Express or Visual C# Express

When you're done experimenting with the ThinkEssentials software, go ahead and close it so we can use the Z-Wave USB stick from Visual Studio Express. You can download Microsoft's Visual Studio Express. If you've set up any scheduled events, you'll need to right-click on the software's system tray icon and select “Exit” from the pop-up menu.

trayexit.jpg
To completely shut down ThinkEssentials, right-click on its tray icon and click Exit.

Now, we're ready to get started controlling our lights using Visual Studio Express. With Visual Studio Express, we can personalize our home in unlimited ways. For now, we'll start out simple and turn on the lights.

Creating our Visual Basic Express and Visual C# Express project

With this sample, we're going to turn on all of our lights. So to prepare for this sample, go ahead and turn off all the lights connected to Z-Wave devices (plug-in modules).

Then, start up Visual Basic Express or Visual C# Express. Create a Windows Application named “TurnOnLights”. Once you've created the project, select “Add Reference…” from the Project menu, switch to the dialog's Browse tab, and then add in the ControlThink.ZWave.dll file that came with the Z-Wave PC SDK. If you don't know where it is, look in your Program Files folder for a ControlThink and Z-Wave PC SDK folder.

addreference.gif
To add the Z-Wave assembly to the project, select Add Reference from the Project menu…

BrowseForDll.gif
In the Add Reference dialog, change to the Browse tab and then add in ControlThink.ZWave.dll.

Once you have added in the Z-Wave assembly, double-click on the default form; this should open up the Form_Load method in code. Once there, we'll create an instance of the ZWaveController object. This will let us connect to our Z-Wave network with the USB stick. In the Form_Load method, add the following code:

Visual Basic

Dim zwaveController As New ControlThink.ZWave.ZWaveController()

Visual C#

ControlThink.ZWave.ZWaveController zwaveController = new ControlThink.ZWave.ZWaveController();

Next, we'll connect to the network. It only takes two lines of code to get “online” with the home.

Visual Basic

zwaveController.Connect()

Visual C#

zwaveController.Connect();

Now that we've created the necessary code to connect to the network through our Z-Wave USB stick, we'll add just a few more lines of code to turn on all the lights. Every device in our network is ultimately based on the ZWaveDevice object, and this class gives us the ability to turn on and off devices as well as change their dim level so we'll use it here.

Let's also include a Try…Catch block here; our remotes and some other devices cannot be turned on and off and consequently throw an exception if we try.

Visual Basic

For Each device As ControlThink.ZWave.Devices.ZWaveDevice In zwaveController.Devices

Try

device.PowerOn()

Catch ex As Exception

End Try

Next

Visual C#


foreach(ControlThink.ZWave.Devices.ZWaveDevice device in zwaveController.Devices)

{

try

{

device.PowerOn();

}

catch(Exception ex)

{}

}

That's it. Run your code, and watch your lights turn on!

Of course, you can also turn everything off. To do that instead, just change the PowerOn method to PowerOff:

Visual Basic

For Each device As ControlThink.ZWave.Devices.ZWaveDevice In zwaveController.Devices

Try

device.PowerOff()

Catch ex As Exception

End Try

Next

Visual C#


foreach(ControlThink.ZWave.Devices.ZWaveDevice device in zwaveController.Devices)

{

try

{

device.PowerOff();

}

catch(Exception ex)

{}

}

What to try next

Now that you've experienced the basics of using Z-Wave from Visual Studio Express, here are a few ideas you can use to personalize your own home:

1. device.Level can be set to 0 for off, or 1-99 for dim levels (99 = full on).

2. zwaveController.Devices is a collection of all the devices in your network. Each of those devices has a unique ID on the network called a Node ID. Access the devices individually by index, or use zwaveController.Devices.GetByNodeID(…) to access each device by its unique Node ID.

3. Alarm Clock sample from Coding4Fun, and customize it to turns up the heating set point 30 minutes before the alarm goes off.

About the author

Chris Walker is President and Chief Software Architect of ControlThink, creator of the Z-Wave SDKs for .NET platforms, and is an outspoken advocate of reliable home control technology. He is determined to make home control technology easy to use and affordable for all homeowners. Chris Walker http://channel9.msdn.com/ShowPost.aspx?PostID=267636 http://channel9.msdn.com/ShowPost.aspx?PostID=267637 Easy $50-$100 1-3 hours Z-Wave PC SDK]]> ]]>

Tag:

Follow the Discussion

  • MEME

    i want something where you don't have to buy anything

                 %^$&%*)^)*(^%*($%&^#$%*&&*$%

  • anonomusanonomus

    this isn't a hack it is a how to use an existing piece of hard ware to control lights no hacking involved just simple programing. this page should be moved it should not be classed as hardware

  • GerardGerard

    of course u need to buy something, this is hardware programming. do software programming if u dont wanna buy smting

  • EdwinEdwin

    This is pure marketting of Z-Wave products in disguise. Making it happen from Visual Studio Express Edition is no big deal either.

    Useful article for someone who desperately wants to programatically control his/her home but totally useless for most of us who look for hard technical solutions at an affordable price.

    Very disappointing.

  • Strong BadStrong Bad

    "anonomus", you do have a point about the simplicity of this page but watch your spelling and punctuation!

  • ZippyZippy

    Strong Bad; who cares about his grammar? get a life!

  • For Babies onlyFor Babies only

    This is not productive. Buying the whole system and writing two lines of code doesn't mean you have done something. This project is only for babies. NOT PRODUCTIVE AT ALL. 1out of 10

  • BlackBurnBlackBurn

    I like the article and I think I'm going to play with it, it would be very nice to integrated it with VMC, for example lower the lights if you're start a movie Smiley

  • scoobscoob

    cool!!! but hope it would not cause any harm to the circuits n switches

  • ForeignForeign

    Can I use this kit for any electric installation like the Israly one ? Also do I need something special in lamps or net or GPS net ?

  • JamesJames

    Awesome, truly awesome.  I have had a fantacy since as a child of technological capabilities like these.  Looks like my dream is coming true.    

  • C++ GuyC++ Guy

    How about a version that works with VC++ (Express)?

  • anotherAnoanotherAno

    I agree to anonomus, I don't want to see how to use a library of hardware-seller, I want to create my own library for my own stuff like a controller and I can't see this VB & C# stuff a such a section anymore. Hardware development needs C and maybe some inline-assembler

  • adxadx

    ??? HW needs C and/or assembler? VB works just as well as anything else, in fact I prefer to use something high-level like VB alongside assembler, rather than C to do it all.

  • gadgetChrisgadgetChris

    Well, for me it was useful and I think it is h/w related and appropriate.

  • markmark

    maybe i can use this to my project..,

  • David DohmDavid Dohm

    Some of you should really get a life. This is a good basic tutorial for someone who wishes to dabble in home control using the two programming enviornments. Take it for what it is, not some illusionary free for nada miracle!

  • pazzerpazzer

    Ditto that Dave..

    There are more flexible ways of doing this with X10 hardware, using *unix and MS products, and there's more appropriate hardware than this Z-wave stuff, but none the less, a good introduction for MS huggers. Check out X10 protocol, and you'll probably find better reading than this..

  • johnnyjohnny

    try zwave, it is a whole new level from x10.

    but I wish there was other pc interfaces

  • BartekBartek

    try mcontrol embeddedautomation.com

  • ComputerFrekComputerFrek

    I totally do not understand what is this

  • Clint RutkasClint I'm a "developer"

    @ComputerFrek, it is using your computer to control you home so it can be automated.

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.