X10 Lamp Control Over The Internet With Live Streaming Video Example
Posted on May 13th, 2011 by James Litten
Currently using webcam snapshots instead of stream (delay of up to 5 seconds but much less bandwidth and no ads).
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 …- It checks the field on the MySQL server at litten.com that represents the state of the lamp once per second.
- It sends an X10 command through the X10 PC interface to the lamp, setting it to that state.

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
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.- A lamp. I am using a nice Himalayan Salt lamp from Amazon.com.
- An X10 3-pin Appliance Module (AM466). To plug the lamp into. Then it is plugged into an outlet.
- An ActiveHome Professional Computer Interface (CM15A). That is plugged into an outlet and attached to your PC via USB cable.
- The ActiveHome Scripting SDK
- A copy of msvcp71.dll and msvcr71.dll (be careful, this site tries to trick you to click on ads)
<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.