Unknown Kadath

X10 Lamp Control Over The Internet With Live Streaming Video Example

Posted on May 13th, 2011 by James Litten

In this project, we are trying to find the simplest way to allow public access to X10 home automation devices while maintaining a secure environment that does not allow anything damaging to happen. The video and button below are live and in my home. This post will describe how this is done.  

Currently using webcam snapshots instead of stream (delay of up to 5 seconds but much less bandwidth and no ads).
Click this button to turn the lamp on/off

Summary of Operation

This blog post is being served to your browser from a server on the internet provided by GoDaddy hosting. On this server I have created a database using MySQL which has a row in it representing the lamp in the streaming video you see above. In that row is a field representing the state of the lamp (on / off). The button that controls the state of the lamp, simply toggles that field in the database. In my house there is an XAMPP server running on a Windows 7 PC which has a page open on it that does two things …
  1. It checks the field on the MySQL server at litten.com that represents the state of the lamp once per second.
  2. It sends an X10 command through the X10 PC interface to the lamp, setting it to that state.
This keeps all of the command and control of the lamp inside my secure home network. All that exists in the wild are three state values of the lamp. On, off or something else (things break or have unexpected results in the wild). figure I shows the whole setup.

fig. I

 

MySQL Database

On my hosted server, I have the ability to make MySQL databases. Almost all hosting services have this these days.

Create new mySQL db

Most hosts have an option for creating new MySQL databases in their control panel. I used to make these from the command line but nowadays I just use the control panel for generic databases like this one. I created a new database called x10.  

Add a table

I rarely use the command line to make tables anymore. I find using the phpmyadmin interface sufficient for most of my needs. You should be able to access phpmyadmin from the control panel at your hosting account. Here are the settings for the table that I created for this project. The table is named x10state. There is a field named device that is a varchar(20) with a default value of the word default and another field named state that is a tinyint(1) with a default value of 0.

CREATE TABLE IF NOT EXISTS `x10state` (
`device` varchar(20) NOT NULL default ‘default’,
`state` tinyint(1) NOT NULL default ’0′
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

Add new restricted users

We create a user that can only access the database from this server (replace mypassword with your own password)  

CREATE USER ‘x10user’@'localhost’ IDENTIFIED BY ‘mypassword‘;
GRANT SELECT,INSERT,UPDATE,DELETE
ON x10.*
TO ‘x10user’@'localhost’;

  We create another user (with the same username as above) that can only access the database from this server (replace mypassword with your own password and replace MY FIOS ADDRESS with the address that your home server has. You can find it by going to http://www.displaymyhostname.com/ while on the PC that your home webserver will be on.

CREATE USER ‘x10user’@’MY FIOS ADDRESS‘ IDENTIFIED BY ‘mypassword‘;
GRANT SELECT,INSERT,UPDATE,DELETE
ON x10.*
TO ‘x10user’@’MY FIOS ADDRESS‘;

 

ON/ OFF Button

The On / Off button is placed into this post using an IFRAME and consists of two PHP pages with access to the MySQL database and a form to submit the action of clicking it so that it can change the state value of the lamp in the database.
The IFRAME element looks like this…

<iframe id="frame1" src="http://litten.com/switch.php"  height="50"
width="100" frameborder="0" scrolling="no"></iframe>
switch.php gets the state of the lamp from the database and applies it as the label for the submit button. The action for the submit button is changestate.php.

The code for switch.php

<?php
$db_host = 'localhost';
$db_user = 'x10user';
$db_pwd = 'mypassword';
$database = 'x10';
$table = 'x10state';

if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");

if (!mysql_select_db($database))
die("Can't select database");

$result = mysql_query("SELECT * FROM {$table}
 WHERE device = 'lamp'");
if (!$result) {
die("failed");
}

$row = mysql_fetch_row($result);
$state = $row[1];

$lampstate = "OFF";
if ($state == 1) {
$lampstate = "ON";
}
?>

<form method=POST action="http://litten.com/changestate.php">
<input type="submit" name="switch"
 value= "<?php echo $lampstate ?>">
</form>
 
The form in switch.php has the action changestate.php which simply changes the value of the state of the lamp. If it is off, then clicking the button sets it to 1 (on) and if it is on then clicking the button sets it to 0 (off).

The code for changestate.php

<?php
$db_host = 'localhost';
$db_user = 'x10user';
$db_pwd = 'mypassword';
$database = 'x10';
$table = 'x10state';

if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");

if (!mysql_select_db($database))
die("Can't select database");

$result1 = mysql_query("SELECT * FROM {$table}
 WHERE device = 'lamp'");
if (!$result1) {
die("failed");
}

$row = mysql_fetch_row($result1);
$state = $row[1];

$onoff = 1;
if ($state == 1) {
$onoff = 0;
}

$result2 = mysql_query("UPDATE {$table}
 SET state = {$onoff} WHERE device = 'lamp'");
if (!$result2) {
die("failed2");
}

$lampstate = "ON";
if ($state == 1) {
$lampstate = "OFF";
}
?>

<form method=POST action="http://litten.com/changestate.php">
<input type="submit" name="switch"
 value= "<?php echo $lampstate ?>">
</form>
 

Video Streaming

UPDATE: I am currently testing a solution that uses my webcams ability to detect when the light turns on or off. When a change is detected, it takes a snapshot and uploads it using Dorgem (dorgem.sourceforge.net). That snapshot is shown in an IFRAME above the button. It’s not as pretty as a live stream but it is far more economical and has no ads.

The video stream is currently accomplished using a free account at Ustream (www.ustream.tv) which is supported by ads. It is easy to setup and works very well. Another option would be to use something like Dorgem (dorgem.sourceforge.net). I am using an inexpensive webcam with the generic Windows USB Webcam Drivers.

Web Server on Home PC

Now we need to set up a webserver on our home network that is capable of displaying a PHP web page that will periodically check the MySQL database at litten.com to get the value of the lamp’s state and then send that value to our X10 module controlling the lamp. The easiest way to set up a server that can serve PHP pages on a Windows 7 PC like I am using for this is to use XAMPP (XAMPP ) (Wikipedia entry for XAMPP)

X10 PC Interface via a PHP page

Now that we have a webserver on our home network. We need a few things to make this work. On our XAMPP webserver, inside the htdocs directory, create a new folder called X10. Inside of that folder create a file called refresher.php. You will be able to access this file in your browser on that server/PC by going to http://localhost/x10/refresher.php Inside the X10 folder you also need to put a copy of ahcmd.exe which can be found in AHSDK\samples\php after installing The ActiveHome Scripting SDK. Inside the X10 folder you also need to put copies of msvcp71.dll and msvcr71.dll which are needed to support ahcmd.exe This file will check the MySQL database on the public webserver (in my case at litten.com) and retrieve the state value for the lamp. Then it will use an exec command in PHP to send the state to the actual lamp. Here is the code for refresher.php  

<META HTTP-EQUIV=Refresh CONTENT='1; URL=refresher.php'>
<?php
$db_host = 'litten.com';
$db_user = 'x10user';
$db_pwd = 'mypassword';
$database = 'x10';
$table = 'x10state';

if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");

if (!mysql_select_db($database))
die("Can't select database");

$result = mysql_query("SELECT * FROM {$table} WHERE device = 'lamp'");
if (!$result) {
die("failed");
}
$row = mysql_fetch_row($result);
$state = $row[1];
echo $state;

$cmd = 'on';
if ($state == 0){
$cmd = 'off';
}

$cmdstring = "A1 ".$cmd;
echo $cmdstring;
exec("ahcmd.exe sendplc $cmdstring");
?>
This page must be open in a browser on your server/PC in order for the lamp to work. To keep this as simple as possible I am using a META tag to refresh the page once per second. There are other options for this.
The command string in this case uses A1 which is the house code/unit code of the X10 module that I have the lamp plugged into. Yours may be different. It is sent in this line.

exec("ahcmd.exe sendplc $cmdstring");
When $cmdstring is A1on it sends the X10 command to turn the device with house code A and unit code 1 (which is my lamp in this example) on.  

Please leave comments if you have questions or use my contact page if you’d prefer to ask me privately.

One Response to “X10 Lamp Control Over The Internet With Live Streaming Video Example”

James LittenMay 15th, 2011 at 5:31 pm

I’ve gotten several private messages asking why not just password protect access directly to the server inside my home network.
The answer is as follows.
I’ve been asked by a client to find a secure way for them to make control of a device publicly accessible while protecting it from unexpected or malicious access. This X10 setup is only to test the technique of having the device check for settings on the internet and control itself based on what the public has asked it to do instead of letting the public actually control it.
So far it is working great. I am glad that I picked a lamp that I don’t mind having go on/off constantly. It is going on/off many times every hour :)
Thanks to all of you helping me out with this.