Monday, 14 November 2016

X10 Home Security DIY - Arm / Disarm


Most home security systems come with an entry panel that is used to Arm or Disarm the system upon entry or exit.  Some come with key tags (or apps on your phone) that will arm or disarm the function automatically - we'll take a look at that option in a later post.  For now, let's look at implementing a simple arm and disarm function using a keycode.

There are several methods that can be used to implement this function on a web server:
  1. Using a global environment variable - purists would no doubt prefer this method, as it demonstrates their advanced coding skills and understanding of Linux environments.
  2. Using a file.  That is, you store the ARM/DISARM status in a file accessible from the server.  I prefer this method - it is similar to how passwords are stored, and the file can be encrypted and protected more easily.
We may also need to implement an arm delay - to allow for someone hitting the ARM button in the control panel just before opening the door and exiting the building.  Or maybe we just delay the disarm write until the very next window/door sensor event has completed.

Implementation


We modify the X10arm.pl script called from the control panel (when the ARM button is pressed) to open the X10.log file and write the status "X10 Alarm System is ARMED" into the file.  Similarly, we modify the X10disarm.pl script to write the status "X10 Alarm System is DISARMED" into the log file.

Now, when a trigger event happens (window/door sensor, motion sensor), we condition the notification response based on the X10 Alarm status (which we read from the X10.log file) in the bashX10.sh script.  If the system is ARMED, we send out all kinds of notifications; if the system is DISARMED, we limit the notifications (to flashing the lights, or popping up a desktop notification on my desktop, for example).

We further modify the X10disarm button to call a separate HTML file first - here is where we implement the keycode.  Instead of calling X10disarm.pl from the control panel HTML, we call security.htm instead - and this file contains a simple form to produce a keycode panel, as shown below:

 

Again, we use some nice styling to produce hover effects and shading (thankyou w3schools for the styling library), store the entered codes into a Javascript variable, and when the Submit button is pressed, we compare the submitted code to the stored and encrypted passcode, and then set the X10.log status to "X10 Alarm System is DISARMED" if the code matches.  We can even allow three attempts or some such method to prevent unauthorized access, but I wouldn't recommend relying on this method alone - after all, notifications would already have been sent out by the motion sensor or window/door sensor logic if someone unauthorized entered your building.  

Browser hackers will also note that the user could always right-click and select "inspect code" in Google Chrome in order to examine the html file and variables (which is another reason to put the keycode in a separate file), so we don't want to disallow all notifications.

A note on External Security of the Control Panel


If you expose your control panel to the internet (by providing a port forward through your router to the Abyss server port that services it) then you will need to implement at least SSL (https:) security.  Me, I prefer not to run the risk at all - the control panel and the entire server are only visible if you are connected to my LAN, and then again only if you are a recognized IP stored in the reservations list on my router.  It is simple enough to get a message or status out of my LAN (e.g. to IFTTT); it is very difficult indeed to get in.

Sunday, 6 November 2016

X10 Home Security DIY - Control


So we want a system that we can control from all devices - computers, tablets, and smartphones (iOS and Android).  Generally, this would imply developing apps for each platform, but there is one application that works on all these devices - an internet browser.

This means that if we develop the functionality to work on a server, and develop a set of mobile-friendly pages, then we can develop the control mechanism once, but access it from all devices using the browser - this is definitely the way to go for a DIY project.

From previous development efforts, I've found that the Abyss web server from Aprelium is a very flexible and easy-to-install server.  With a bit of HTML, Javascript, and CSS development knowledge it is remarkable how easy it is to develop websites.

The other key piece is the same mochad daemon that we use to catch X10 RF commands.  This function came with a set of scripts that have the ability to send out X10 powerline commands, so we can use it to control X10 devices by calling the commands directly from HTML buttons.

Control Flowchart



any LAN client --> LAN
--> Abyss web server on localhost:8080 -->
--> index.html Control Panel --> X10 powerline commands -->
--> mochad daemon running on port 1099 --> netcat TCP
--> X10 CM15A controller --> household wiring -->
--> X10 devices

At the same time, we can use the lifxlan-master scripts to send commands to LIFX bulbs on the LAN, and even send commands to IFTTT Maker channel to control other devices (like the Thermostat fan).  We'll integrate an ARM/DISARM function into the control panel (and add a numeric keypad for an access code) in a later post.

Here is the control panel.


Here's the html file that implements the control panel

A few comments on the code:

The file uses a simple table with buttons to perform the control functions.  A little bit of fancy styling is used to make the table and the buttons rounded, and perform some simple effects (like changing the cursor when it is over the buttons, and some simple animations to give the feedback that a button is clicked).  Each of the buttons then calls a perl script (x10cmd.pl) with parameters indicating the command to be performed. The perl script then calls the system function for the button - e.g. lifxlan-master for the LIFX bulb, or the x10cmd shell script for the x10 commands, and so on. 

The styling also makes the control panel usable from mobile devices (I tried to use very common-denominator styling so that it will work even from very old Android and iPhone phones).  This has the added advantage of raising the WAF (Wife Approval Factor), as it will work from her fancy new mobile phone as well.

The perl scipt x10cmd.pl gives simple feedback that the command was executed in a basic html page.  I plan to migrate all of this code, and the Abyss web server, to a standalone Linux PC so that it does not load down my desktop (although from what I have seen so far, it is hardly noticeable).