In this second installment on X10, I want to show you how easy it is to send signals on the CM15a. In PART I, I showed you how to use the SDK to log messages (build a viewer, basically), so the logical next step is to have the ability to send instructions to switches, plugs, and outlets, right?
My goal is to keep the code in this project as simple as possible, so that it is easy for someone unfamiliar with VB to understand. Keep in mind, however, that there are multiple examples for various languages, as I mentioned last week. You’re not stuck with VB if you don’t like it!
Also, keep in mind that I’m not an expert by any stretch of the imagination. I spent the first 15 years of my career in QA and have worked mostly in web development since – I play around with other languages and other technologies, but it isn’t something I hammer on all the time. I’m just showing you how to get started and how to integrate different things into one cohesive framework.
Check out the image below. In the designer, I enlarged the form window, resized the text box to free up space at the bottom of the form, and added a few items – a SEND button, three combo boxes (I still want to call them selects – I code for the web at my day job), and three labels. One select is for House Code, one is for Unit Code, one is for Command.
In the next image, I’ve clicked the first combo box. You can set up an array or collection (or even query a table with the data) if you want to use the Data Bound option, but this is a quick and dirty project, so I just used Unbound mode. I entered A through P as these represent the house codes available in X10.
I repeated this for the second Combo, except I entered numbers 1-16 to represent the unit codes. In the third Combo, I entered On and Off, which are the only two commands we’re going to work with in this example. Remember, some X10 devices can use BRIGHT and DIM, as well.
Next, I added the default value for each combo box. I usually type everything by hand – a programmer I used to work with once recommended that and it became a habit I never kicked. You don’t need to, you can just double click each Combo box to get the Load sub started. Here’s one way that you can set the default value for the combo box:
Finally, I added the code to send the command. When we do a more robust tool, I’ll have set variables and have functions to handle different tasks, but, for now, we’re just going to put the code in a Try Catch and execute when the button clicks. Speaking of Try/Catch – just use it. You can program specifically for the scenario that you want, but if there is anything off, you can end up with an exception, so why not add a few extra lines of code, even on something simple?
For example, in the code above, if I didn’t use Try/Catch and one or more of the combo boxes still had the default value of “SELECT” when the SEND button is clicked, the program will crash. In our example, you can see that if that occurs, the app will display a message box instructing the user to make valid selections!
The actual meat of the code is “ActiveHomeObj.SendAction(“sendplc”, ComboBox1.SelectedItem.ToString() & ComboBox2.SelectedItem.ToString() & ” ” & ComboBox3.SelectedItem.ToString())” which will send the concatenated values of the three combo boxes as a command over the power line (sendplc) using the CM15A interface through the ActiveHome SDK.
Finally, you can see the app running in Debug mode. Notice that our sensor from the last article is displaying data in the text box, along with the command I just submitted (F5 On). It’s that easy to send commands!
Going forward, we’re going to take a detour from X10 to explore MQTT. Since we’re going to use MQTT to share data from our X10 interface, as well as receiving data back, we’ll need to learn how to set up a client and a broker, as well as illustrating how messaging works.
SmarterHome.club is the website for our Facebook community, The Smarter Home Club – which is an umbrella for all kinds of smart home technologies – home automation, security, custom electronics, weather stations, alternative energy, you name it. DIY focused.
If you’re interested in joining the Smarter Home Club’s Facebook group, please follow this link: