Friday, 9 December 2016

Home Security DIY - Updated Control Panel

Here is the updated Control Panel:

You will note the separate on/off buttons have been replaced with single sliders with active colours (blue background when "on", grey when "off").  This is much more like the kinds of settings screens you typically see on mobile devices.  they are easy to code also: just one line per control in the HTML and mostly one-line calls in Javascript.

I also played around with some of the tiling layout generators, but couldn't find anything that really kept the flexibility across phone/tablet/computer, and was lightweight enough not to take a hit on the performance.

The other differences are all in the Javascript code: I converted all the server calls to jQuery ($.get and $.getJSON).  On the server side, all the CGI scripts are in perl or python, depending on the service they are calling - which can be in perl (file I/O), python (lifxlan-master and Honeywell thermostat using therm.py), bash shell (for mochad and curl to web services like IFTTT, openweathermap/Dark Sky).

The big advantage of DIY is seen here - in total there are about 500 lines of code and they run lightning-fast.  It is clean and simple, has a common look-and-feel across all platforms , and when someone opens the front door, I get notifications on 7 different platforms (X10 devices, linphone, SSH, Google mail, IFTTT Maker, lifx, and notify-my-android).



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).

Sunday, 30 October 2016

X10 Home Security DIY - Notification


So, I've got some old X10 Pro stuff in a box, and I see what commercial products like iSmartAlarm can do, so here's the goal:

A do-it-yourself Home Security System not unlike iSmartAlarm:

  • A central hub
  • Window and door sensors
  • A motion sensor
  • Notifications
  • Arm and disarm functions on phones and tablets
  • A central control panel

My Proposal:


  • Use my desktop linux PC as the central hub, with the ActiveHomePro CM15A controller to receive RF events and to send out X10 powerline commands
  • Use my old X10 window and door sensors
  • Use the mochad linux TCP gateway to connect to the CM15A.  Mochad is a daemon that runs on a linux platform waiting for RF commands to be received and for powerline commands to be transmitted using the CM15A.
  • Use a bash script (bashX10.sh) to listen for events on the mochad port and to send various notifications, e.g.
  • Using linphonec for linux, Android and iPhone hosts to receive messages
  • Using curl to send to the IFTTT Maker channel, which can trigger all kinds of other things, like an events log in Google Drive, SMS messages to cell phones, and so on
  • Using lifxlan-master to send commands directly to LIFX bulbs over the LAN (rather than going through IFTTT, which is slower and requires internet access)
  • Using nc (netconnect) to initiate commands back to the cm15a - like the chime, and light and appliance modules

Notifications

The flowchart for notifications is as follows:




We send out notifications to:
- Android & iOS phones and linux desktops on my LAN (using linphone and notify-my-android)
- the IFTTT Maker channel (to Google Drive and to anywhere on the cloud)
- LIFX bulbs over my LAN using lifxlan-master
- X10 devices using x10cmd to send pl commands back through the CM15A

For now, the IFTTT link is used to keep a log of the notification events in Google Drive (see IFTTT to show how to link the IFTTT Maker channel to Google Drive in a simple recipe), and to notify my cell phone via SMS if I am not home.

The "at home" status is determined by pinging my cell phone on the LAN using nmap and awk from the bashX10.sh script.

References

[1] Lifxlan-master
[2] mochad
[3] linphone
[4] IFTTT

Alternatives:  

I could have used Home Assistant but they don't support the X10 RF sensors; someone has just started supporting mochad, but it is early days yet.  I could also have purchased an iSmartAlarm system, but where's the fun in that?  The incremental cost for this system is $0 - the hardware was sitting around doing nothing, and my time is (almost) free - and as you can see, I am leveraging the expert development efforts of many other fine developers who make their code freely available on the net.

Notifications: 

There are so many options for notifying to other devices, and chat on linphone is not exactly fit for this purpose (it is really a VOIP application, and the interface is quite finicky when used without a SIP account).

I've tried using SSH as well , but it only works on devices that are on the LAN and have SSH (available on Android, linux, and iOS, but not on a Chromebook without enabling Developer mode).

Pushover is a commercial choice, and the cost is quite reasonable - it can support notifications on phones, tablets, linux and windows computers, and chromebooks - it might be worth a try.

Pushbullet is another choice - free, but seems somehow to have lost its way, and it is not supported on my old Android cell.

I am presently using notify-my-android to push notifications to our Android phones when we are not home.  it is free for low volumes, and it is quite reliable.