asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Wireless Home Automation Using .NET and X10
By Robert Chartier
Rating: 4.0 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction


    An area I have been interested in for a long time is home automation. Essentially, home automation is the ability to control devices around your house from your PC. For example, if you go away on holidays, you could schedule your PC to rotate your lights on a normal schedule. Turn them all off during the day, then back on in the evening and finally all off again at night. This of course would give the impression that you are at home and potentially thwart robbery attempts. Other examples would be having the coffee pot and toaster automatically begin at 7:00 am and controlling the temperature of your house on a given schedule, etc.

    I have chosen to focus on one specific home automation product for this article, the X10 Firecracker System. It is an extremely inexpensive and easy-to-use product and will give us the most basic understanding of home automation potential. You will need to purchase and install the hardware to take full advantage of this article. Personally I just hit eBay and grabbed a few of the devices from a few vendors. Usually you can find everything much cheaper than from the X10.com site itself, not to mention not having to deal with X10's ads.

    This article consists of two main sections. The first section walks through using the .NET Framework 1.1 (C#) to commuicate with the Serial IO port. The final product of the first part will be a reusable class library that can be leveraged in any sort of application. The second section of this article shows how to wrap the library into an ASP.NET mobile control for controlling any enabled device in your home from a wireless browser on any wireless device.

    Section I: Creating the Reuseable X10 Library

    I have chosen to take two projects from the Web and combine them into this single project. The first project, an X10 Firecracker class, was originally published on the GotDotNet VB Team page (http://www.gotdotnet.com/team/vb/) and has since been ported to C# by Brian Vallelunga (http://brian.vallelunga.com/code/x10/). I decided to take most of this library for creating the message that gets sent over the COM port. I will swap in the Serial Communication library from Corrado Cavalli (http://www.codeworks.it/net/VBNetRs232.htm) for actually sending the messages. The reason for this is because Mr. Cavalli's library is more complete and can also receive data coming in over the port.

    Note: If you are interested, dig around the provided source code. It also contains code that you can use to receive commands coming from the COM port with the MR26a (http://www.x10.com/products/x10_mr26a.htm) product.

    Making the Communications Library

    We first need to take Mr. Cavalli's library and compile it into a DLL, which we can use in our actual class and which will gather and send the message over the port. In the download (http://www.codeworks.it/net/rs232.zip), you will find the file CRs232.vb. This is the core library for RS232 communication with your computer's COM port. Compile this into its own class library using either Visual Studio or the VBC.EXE command line compiler. I named the output DLL in my solution to "Communications.dll". If you did use Visual Studio to compile this, you can exit it now, and take note as to where the Communications.dll was created. We will need to add it as a reference to our new project.

    Getting the Visual Studio Environment Ready

    We need to take Mr.Vallelunga's library, swap out his Serial IO code, and implement the same calls to our new Communications.dll. I first created a new Visual C# Class library named "X10Unified" in a new Solution. We will also add a new Console App to this Solution in order to test it. There are two references to set in this solution. In the Console App, set a Project Reference to the X10Unified project, and then in the X10Unified project, set a reference to our new "Communications.dll" file.

    Upgrading the Serial IO Code

    Now that we have our development environment setup, let's take a look at the code.

    I first changed the way the the X10 library is created ("Firecracker.cs"). I decided to take advantage of the "Singleton Design Pattern" (http://www.c-sharpcorner.com/Code/2003/Jan/SingletonPattern.asp). Essentially this allows us to have ONLY a single instance of our class created. This instance then will be used by all calling applications. The main reason for this is because the library relates to the single serial port instance. If we had more than one application trying to access the Serial port, it would definitely throw an exception. So now we limit that access to a single point in the code and a single class, so if more than one caller needs to use the resource, they will have to sit and wait for it to be freed.

    Here is the rewrite of the class initialization. Notice that it does in fact use the Singleton Pattern and that we have moved the serial port initialization to use the new library.

    #region Singleton pattern to avoid more than once instance of the class

    private Firecracker()                         {                                              

    }

    Communications.Rs232 comm=null;

    private Firecracker(int commPort)                            {

      comm = new Communications.Rs232();

      comm.Port=commPort;

      comm.BaudRate=9600;

      comm.DataBit=8;

      comm.StopBit=Communications.Rs232.DataStopBit.StopBit_1;

      comm.Parity=Communications.Rs232.DataParity.Parity_None;

      comm.Timeout=1000;

     

      this.CommPort = commPort;

     

    }                              

    privatestaticvolatile Firecracker instance=null;

     

    publicstatic Firecracker GetInstance(int CommPort) {

      //Double-Check Lock singletone

      if(instance==null) {

        lock(typeof(Firecracker)) {

          if(instance==null) {

            instance = new Firecracker(CommPort);

          }

        }

      }

      return instance;

    }

     

    #endregion

    The next major rewrite of code is the method named "SendCommand". Here is the new implementation. Notice that the Serial Communication IO uses the new library and not Vallelunga's library. You will find this new implementation much more stable than the previous build.

    publicbool SendCommand(char houseCode, int deviceCode, Commands cmd)                       {

     

      // Send to the mCommPort serial port a binary stream consisting of the house code

      // device code, and any additional binary data that helps execute the desired command.;

     

      string binaryCmd = HeaderCode + GetBinaryHouseCode(houseCode) + GetBinaryGroupCode(deviceCode);

       

      switch (cmd.ToString())                                  {

        case "TurnOn":

          binaryCmd += GetBinaryCommandCode(deviceCode, BinaryCommandCodes.TurnOnIndex);

          break;

        case "TurnOff":

          binaryCmd += GetBinaryCommandCode(deviceCode, BinaryCommandCodes.TurnOffIndex);

          break;

        case "MakeBrighter":

          binaryCmd += MakeBrighterCommandCode;

          break;

        case "MakeDimmer":

          binaryCmd += MakeDimmerCommandCode;

          break;

      }

     

     

      int delay = 30;

      binaryCmd +=  FooterCode;

      comm.Open();

      comm.Dtr=false;

      comm.Rts=false;

      System.Threading.Thread.Sleep(delay);

      comm.Dtr=true;

      comm.Rts=true;

      System.Threading.Thread.Sleep(delay);

      for(int bit=0;bit<binaryCmd.Length;bit++) {

        if (Convert.ToInt32(binaryCmd.Substring(bit, 1)) == 0) {

          comm.Dtr=true;

          comm.Rts=false;

        } else {

          comm.Rts=true;

          comm.Dtr=false;

        }

        System.Threading.Thread.Sleep(delay);

        comm.Rts=true;

        comm.Dtr=true;

        System.Threading.Thread.Sleep(delay);

      }

      //comm.Write(b);

     

      comm.Close();

      returntrue;

     

    }

    First this code gathers the necessary bits to send to the serial port in the "binaryCmd" variable. This is of course based on the house code, device code, and the command that you chose to send. The loop may seem strange to you. This is where we actually send the command to the serial port itself. We need to loop over each bit that we gathered and set the state of the DTR (data set ready) and RTS (request to send) lines. For more information on the Firecracker (CM17A) Communications Specification see the "7A programming protocol.txt" document (http://www.mscorlib.com/7A%20programming%20protocol.txt).

    Testing the New Library

    With this new communications piece in place we are ready to test out the new library. In the Console App project, let's add a few lines of code to test the application.

    //get the instance of the firecracker library

    X10Unified.Senders.Firecracker f = X10Unified.Senders.Firecracker.GetInstance(1);

    //send a command to the device

    f.SendCommand('A', 3, X10Unified.Senders.Firecracker.Commands.TurnOn);

    Make sure that you change the port used, the house code, and the device code to something that you have working in your environment and then run the app to test it. In my case that command turns on my desk lamp.

    I have included the full source for this solution in the download for the article; it is the "X10Unified.sln" solution for Visual Studio .NET 2003. Take time now to compare what I have explained above and what is included in that solution because in the next section we build on top of this library. The final output of this library is a new DLL library named "X10Unified.dll".

    In this section I gave you a fairly quick tour of how I brought together a few libraries to create a new, more reliable and reusable X10 Firecracker communications library. In the next section I will show you how to wrap this new library in a mobile control so we can use a mobile device to control the appliances.

    Section II Adding Mobility >>

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Sep 3, 2003 - Programming for the Palm Part 3 - Creating a Windows Installer
    In the third and final installment of the Programming for the Palm series, Robert Chartier shows how to create a Windows Installer that will install and register the Palm application and Palm conduit on the target machine.
    [Read This Article]  [Top]
    Jul 22, 2003 - Programming for the Palm Part 2 - The Synchronization Process
    In the second part of this three-part series on programming for the Palm, Robert Chartier shows how to work with the Palm synchronization process and how to handle data transferring, importing, and uploading to various destinations using the Palm Conduit Developer Kit and VS .NET.
    [Read This Article]  [Top]
    Jun 24, 2003 - Programming for the Palm Part 1 - Creating the Palm Application
    The first part of this three part series walks through the process of creating a mobile blog application using a BASIC development environment for Palm OS devices called NS Basic. Subsequent articles will focus on synchronizing the data to the desktop using C# and creating an installer.
    [Read This Article]  [Top]
    Apr 22, 2003 - Creating a Mobile Portal
    In the past, developers typically relied on XML and XSLT to create sites that needed to target multiple client platforms. Today, with ASP.NET mobile controls, developers only need to focus on creating one application. In this article, Rob Chartier shows how easy it is to use the ASP.NET mobile controls to create a full-blown wireless portal.
    [Read This Article]  [Top]
    Mar 27, 2001 - Using ASP to Send a Wireless Text Message
    Even though SMS is now in high gear, developers remain slated with restrictive limits to carrier resources. Sending an SMS message via e-mail requires the acceptance of several hidden flaws. Joe Lauer shows how to avoid these complications by sending a wireless text-message through the use of ASP.
    [Read This Article]  [Top]
    Jun 22, 2000 - HDML and ASP Go Hand in Hand
    Learn how to create applications for wireless devices using the Handheld Device Markup Language (HDML) and ASP. Article covers everything from setting the MIME content type for HDML in ASP to passing data between languages to accessing environment variables from ASP.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry