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.
Intermatic’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 .

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.

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.

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.

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

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. Controlling Your Lights with Your PC Chris Walker http://channel9.msdn.com/ShowPost.aspx?PostID=267636 http://channel9.msdn.com/ShowPost.aspx?PostID=267637 Easy $50-$100 Z-Wave PC SDK]]> ]]>