Friday, September 26, 2014

Home Automation using C# and x10 Modules and Computer Interface

In this blog, the step 1 to step 5 shows you how to set up the home automation using X10 technologies (where to purchase and how to setup). The remaining step shows you how to control devices via X10 using C#.

Step 1: Purchase X10 Appliance Modules / Lamp Modules


The first step is to purchase X10 appliance module with optional lamp modules (purchase a lamp module only if you want to dim a lamp).

The x10 appliance modules allow one to control many different home appliances such as lamp, electric fan, fridge, etc, via a computer interface such as CM15Pro. The approach is not intrusive, in other words, there is no need to modify your home circuits or whatever. Simply plug the appliance module into any of the wall socket, then plug any devices (to be controlled) onto the socket on the appliance module. 

The lamp module is similar to the appliance module except it allows dimmable lamp attached to be dimmed via a computer interface such as CM15Pro. By the way, if the lamp you try to control is not dimmable (such as those cheap lamp sold at Japan Home shop), then don't bother to by a lamp module as non-dimmable lamp can be easily controlled by a appliance module (any way lamp module does not work well with non-dimmable lamp)

A computer interface control the appliance module or lamp module via either power line or radio frequency, the appliance module or lamp module in turns can then switch on or off the devices plugged into its socket. 

If you are staying in Singapore or some other common wealth countries which follow UK plug standard, you can purchase the appliances modules / lamp modules from UK Automation at http://www.uk-automation.co.uk/

For the appliance module, I would recommend AM12U (http://www.uk-automation.co.uk/products/Marmitek-X10-Appliance-Module-AM12U.html). The picture below shows the AM12U module:


For the lamp module, I LM12U (http://www.uk-automation.co.uk/products/Marmitek-X10-Lamp-Module-LM12U.html) will do. The picture below shows the LM12U module:
As each appliance module can control a single device, therefore, suppose you have 4 devices to control, you will need to buy 4 units of AM12U.

Step 2: Purchase the Computer Interface CM15Pro


Next is to purchase the computer interface CM15Pro (http://www.uk-automation.co.uk/products/Marmitek-X10-CM15Pro-Computer-Interface-with-Transceiver.html). The picture below shows the CM15Pro computer interface:


The CM15Pro computer interface comes with the power line USB connector which can be plugged into your computer. it also has the ActiveHomePro software which once installed, allows one to control the appliance modules from the ActiveHomePro.

Step 3: Set up CM15Pro and ActiveHomePro 


Step 3.1: Power up the CM15Pro via its power cable. 

Step 3.2: Disable your anti-virus software and install the ActiveHomePro, During the installation, when prompted, plug in the USB cable from the CM15Pro to your computer. Wait for the installation to complete.

Step 4: Set up appliance module.


For each appliance module, configure its address using the two dials in front of the module as shown in the figure below:


It is important that you set each module to a unit address such as "A1", "B3" (where "A" is the House and "1" is the Unit). 

Once configured, plug the device power plug into the module's socket, and then attach the module's plug to any socket at your house.

Step 5: Testing


Suppose you have set up two modules in step 4 with addresses "A1", "B3". On your computer, launch the ActiveHomePro, and then tries the user interface which allows you to switch "A1" ON and OFF. it should work as expected.


Step 6: Download ActiveHome SDK 


Download the ActiveHome SDK from http://tuicemen.com/downloads/ahsdk_install.exe 

Once downloaded, install the adsdk_install.exe

Step 7: Run C# sample codes in the SDK


The ActiveHome SDK comes with a set of C# sample codes which shows you how to turn ON/OFF a device connected to an appliance module. You may notice some error messages while building, if so simple delete the pfx key file which is not needed. Another error that you may encounter is the follow message:

ActiveHomeScriptLib.ActiveHomeClasscannot be embeddedUse the applicable interface instead

If you encounter the above error message, simply change the "Embed Interop Types" of the referenced Interop.ActiveHomeScriptLib.dll to "False" in your VC# project.

You should now compile and successfully run by controlling the "A1" and "B3" device ON or OFF.

Step 8: Write your own C# codes to control.


I have attached here a small sample project which shows user how to control the devices via C# as well as how to obtain the "ON/OFF" status of the devices using C#.
The sample code can be downloaded from : My Sample Codes

My Venture

Below is one of my applications which uses the above technique in its implementation:


Here is the one which remote control via iphone:

Tuesday, September 2, 2014

Upload App Engine Endpoint project

Step 1: create the project at Google Developer Console and copy the project id for later usage.

Step 2: import the app engine project into Eclipse (follow this link on how to do this: http://czcodezone.blogspot.sg/2014/09/import-and-run-app-engine-endpoint.html). Expand the project directory in the "project explorer" of Eclipse to the folder "[project_folder]->main->webapp->WEB-INF" and double-click to open "appengine-web.xml". Change the text element in the xml element "<application>"
to the project id copied earlier in step 1.

Step 3: Right-click the project in the "project explorer" of Eclipse and select "Run As->Run Configuration". In the run configuration dialog, right-click "Maven Build" and select "New". enter the configuration name (e.g. "training-upload") and select the base directory using the "Browse Workspace" button. Next enter "appengine:update" into the "Goals" and click "Apply". Now click "Run" button.

Step 4: To reupload, simply right-click the project in the "project explorer" and select "Run As->Maven Build" and select the earlier created configuration (e.g. the "training-upload" specified earlier in Step 3).

Import and Run App Engine Endpoint project in Eclipse J2EE

Step 1: Download J2EE Eclipse if you have not done so, the link is http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/keplersr2

Now unzip and launch the eclipse IDE

Step 2: Import the App Engine Endpoint project (which can be created using maven, e.g., following this link: http://czcodezone.blogspot.sg/2014/09/using-maven-to-create-app-engine.html) into Eclipse. To do so, right-click "project explorer" in Eclipse and select "Import->Import". Next, in the import dialog, select "Maven->Existing Maven Projects", and follow the steps to import the maven app engine endpoint project.

Step 3:  To run the imported project, right-click the project in the "project explorer" and select "Run As->Run Configurations". In the run configuration dialog, right-click "Maven Build" and select "New", now enter the configuration name on top of the page and enter "appengine:devserver" in the "Goals" textfield. In click "Browse Workspace" below the "Base Directory" and select the imported project. Finally click on "Apply" button, and then "Run", the maven will then go and compile and download related libraries and start running the project".

When the project is running, the output command line in Eclipse will read "[INFO] INFO: Dev App Server is now running". Now you can open the url http://localhost:8080/ in your browser and see the app engine endpoint web app running.

Step 4: To see the available endpoint api, after the app is running go to the link http://localhost:8080/_ah/api/explorer

Setup Maven on Windows

Step 1: Download Maven from http://maven.apache.org/download.cgi?Preferred=ftp://mirror.reverse.net/pub/apache/ (in my case i download the version "Maven 3.2.3 (Binary zip)"). Unzip it to a location (in my case, i have it unzipped at "C:\maven")

Step 2: Add the "C:\maven\bin" to the PATH environment variable in your computer (For users who don't know how to do this, follow this link http://www.computerhope.com/issues/ch000549.htm)

Step 3: Now you can start using Maven in command line. For example, try something to generate archetype like the one below:

$mvn archetype:generate

Basic Hadoop Command Lines and Simple mapper and reducer programs

Upload a file to Hadoop DFS

To upload a file (e.g. purchases.txt) to the hadoop DFS, use the following command:

$hadoop fs -put purchases.txt myinput

This will upload the file "purchases.txt" to Hadoop DFS within the directory named "myinput"

List files in Hadoop DFS

To list files in Hadoop DFS, use the following command:

$hadoop fs -ls

This will list the files in Hadoop DFS

To list files in "myinput", use the following command:

$hadoop fs -ls myinput

Process file in Hadoop DFS using mapper and reducer programs

To process data in files uploaded to Hadoop DFS via mapper and reducer programs, use the following commad:

$hadoop jar [hadoop.jar location_path] -mapper mapper.py -reducer reducer.py -file mapper.py -file reducer.py -input myinput -output joboutput

Where [hadoop.jar location_path] is the path to the hadoop jar file; mapper.py is the mapper python program, and reducer.py is the reducer python program, "myinput" is the input to mapper.py, and "joboutput" is the output folder in Hadoop DFS which stores outputs from reducer.py

After running the above command,  the user can list the content in "joboutput" using the following command:

$hadoop fs -ls joboutput

Note the actual output is stored in the file "part00000" with the "joboutput" folder. To view this file, run the following command

$hadoop fs -cat joboutput/part-00000

To download this file to local machine, run the following command

$hadoop fs -get joboutput/part-00000 output.txt

This will save the output to the file "output.txt"

To rerun the Hadoop processing on "myinput", please remember to delete the "joboutput" created previously using the following command before rerun:

$hadoop fs -rm -r -f joboutput

Alternatively, just specify output to be a different name such as "joboutput2"

If you are using Cloudera CDH, you can run the MapReduce using the command:

$hs mapper.py reducer.py myinput joboutput 

The above line replace the commands "hadoop jar [hadoop jar location] -mapper mapper.py -reducer reducer.py -file mapper.py -file reducer.py -input myinput -output joboutput", and is provided by the CDH daemon version

Simple Mapper and Reducer program in python

In this section, I will create simple programs mapper.py and reducer.py which sums the values of the 4th column in the "myinput" (which is purchases.txt) groupped by the values in the 1st column in the "myinput".

below shows the content of a simple mapper.py

def mapper()
    for line in sys.stdin:
        # strip off extra whitespace, split on tab and put the data in an array
        data = line.strip().split("\t")
        if len(data)==4:
            keyItem, item2, item3, valueItem = data
            print "{0}\t{1}".format(keyItem, valueItem)

 The above mapper.py program assumes that input file (e.g, purchases.txt mentioned earlier) has 4 columns, and the first column is used as the key while the last column is used as the value. the mapper program simply output the "[keyItem]\t[valueItem]" to the reducer program.

When the "[keyItem]\t[valueItem]" data series are passed to the reducer program, a shuffle and sort is performed such that all these data are sorted by the "[keyItem]" value.

below shows the content of a simple reducer.py

currentKey = None
currentTotalValue = 0

def reducer()
    for line in sys.stdin
        data = line.split("\t")
        keyItem, valueItem = data
        if currentKey == None or currentKey != keyItem:
              if currentKey != None:
                   print "{0}\t{1}".format(currentKey, currentTotalValue)
              currentKey = keyItem
              currentTotalValue = 0
       currentTotalValue += float(valueItem)
   
   if currentKey != None:
       print "{0}\t{1}".format(currentKey, currentTotalValue)

The above reducer program sums the valueItem based on the keyItem. This can be done in the above implementation because the stdin input for the reducer are sorted based on "[keyItem]"

Using Maven to create App Engine Endpoints project via archetype generation

Step 1: launch your console or terminal, and navigate to the folder where you want to create your appengine endpoints project.

Step 2: Now launch maven to generate archetype (if you are on Windows the console warns "mvn" is not a command, it is because you have not installed and add maven to the system path, follow this link to setup the maven environment: http://czcodezone.blogspot.sg/2014/09/setup-maven-on-windows.html)

$mvn archetype:generate 

Step 3: Use "appengine" as filter

$appengine 

This will list 3 archetypes.

Step 4: Now use "endpoints" as the filter

$endpoints 

This will list 2 archetypes.

Step 5: Now enter "2" to select the second archetype (which is a hello-world endpoints project).

$2

This will list 3 versions of the archetype 2.

Step 6: Now enter "3" to select the latest version of the archetype 2.

$3 

Step 7: Now maven will ask the "groupId", which is just the java package name, so i enter "com.appengine.training"

$com.appengine.training

Step 8: Now maven will ask the "artifactId", which is the project package folder name, so i enter "training"

$training 

Step 9: Now maven will ask the "version", so I just press the ENTER key to accept the default

Finally: next few steps just press the ENTER key and "y" to accept YES, which completes the process of building a hello-world appengine end point project using maven. You can then load the project into eclipse to do further development