Elk Data Logging with PHP Code

HoustonFirefox

Active Member
Hi all,

I'm in the process of installing some solar panels to power my Elk M1 Gold as well as powering attic fans, mobile device charging stations, etc. With that in mind I thought it would be a good idea to install two additional temperature sensors to see if the attic fans were doing their job. I installed one of the sensors in an inconspicuous place under an eave to measure the outside temp as well as another at a high point in my attic.

remember that the whole point of this is to cut down on my sumer A/C bills.

My question was... how to log the data that the Elk has access to?

I have a spare Linux server that I run my web server on so I figured that would work fine since it's on 24/7 and only consumes 14 watts of power. In addition, it has a 1TB hard drive in it that sits idle most of the time. It is a TS-109 Pro server and I HIGHLY recommend it (check it out at www.qnap.com).

Anyhoo, I'm learning PHP and thought this would be a good exercise so I wrote a small PHP script that opens a connection with the Elk M1 Gold over it's non-secure port, grabs the Elk date and time as well as all of the temperatures on the box. Again, I only have three (so far). These are the Keypad (house) temp, the Outside Temp and the Attic Temp.

So, without further ado, here is the script:


[codebox]<?php // Begin TLOG.PHP
// This script logs onto your Elk M1, Grabs the date/time/temp and writes it to a file specified in $logfilename

FUNCTION fnStripCRLF($data)
{
while ((substr($data,-1) == "\n") or (substr($data,-1) == "\r"))
{
$data = substr($data,0,strlen($data)-1); // strip offending character
}
return $data;
}

$ElkIP = "192.168.1.61"; // CHANGE THIS TO POINT TO YOUR ELK ETHERNET EXPANDER
$ElkNSPort = 2163; // CHANGE THIS TO POINT TO YOUR ELK ETHERNET EXPANDER NON-SECURE PORT
$logfilename = "tlog.log"; // CHANGE THIS IF NEEDED


$fp = fsockopen($ElkIP, $ElkNSPort, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$fmtstr = "";
$out = "06rr0056\r\n"; // get real time clock data
fwrite($fp, $out); // send request for clock
$elkclkdata = fgets($fp, 255); // get the response
// echo "Clock Data: " . $elkclkdata . "<br>";
$elkclkdata = fnStripCRLF($elkclkdata); // strip CR/LF
$fmtstr = substr($elkclkdata, 15,2) . "-" . // Year
substr($elkclkdata, 13,2) . "-" . // Month
substr($elkclkdata, 11,2) . " " . // Day
substr($elkclkdata, 10,1) . " " . // DOW (1=Sunday)
substr($elkclkdata, 8,2) . ":" . // Hour
substr($elkclkdata, 6,2) . ":" . // Minute
substr($elkclkdata, 4,2) . " " . // Second
substr($elkclkdata, 18,1); // DST? (0/1)
// echo "fClock Data: " . $fmtstr . "<br>"; // Uncomment to debug
$elkclkdata = $fmtstr;

$out = "06lw0057\r\n"; // get all temperatures
fwrite($fp, $out); // send request for temps
$elkdata = fgets($fp, 255); // get the response
// echo "Temp Data : " . $elkdata . "<br>"; // Uncomment to debug
$elkdata = substr($elkdata, 4, strlen($elkdata)); // strip header information
$elkdata = substr($elkdata, 0, strlen($elkdata)-4) . "\r\n"; // strip checksum
// echo "Temp Data : " . $elkdata . "<br>"; // Uncomment to debug

fclose($fp); // close the file

$fh = fopen($logfilename, "a+"); // open the log file
fwrite($fh, $elkclkdata . " " . $elkdata); // write the data
fclose($fh); // close the log file
echo $elkdata; // display the data if in interactive mode
}
?>[/codebox]




Once you run it, then it will write data to the file specified in the $logfilename variable. Make sure you change the IP and the port number to point to whatever you are using. I set up a cron (scheduled job) to fire off this script once every 5 minutes and the data can then be analyzed any way you want it.

Here is a sample of the output:

[codebox]08-06-12 5 18:09:53 0 11700000000000000000000000000000000000000000000000000000000000000000000000000000
000015814500000000
08-06-12 5 18:14:53 0 11700000000000000000000000000000000000000000000000000000000000000000000000000000
000015814600000000
08-06-12 5 18:19:53 0 11700000000000000000000000000000000000000000000000000000000000000000000000000000
000015814600000000
08-06-12 5 18:29:52 0 11700000000000000000000000000000000000000000000000000000000000000000000000000000
000015814500000000
08-06-12 5 18:34:52 0 11700000000000000000000000000000000000000000000000000000000000000000000000000000
000015714500000000
08-06-12 5 18:39:52 0 11700000000000000000000000000000000000000000000000000000000000000000000000000000
000015714400000000
08-06-12 5 18:44:52 0 11700000000000000000000000000000000000000000000000000000000000000000000000000000
000015614400000000
08-06-12 5 18:49:53 0 11700000000000000000000000000000000000000000000000000000000000000000000000000000
000015614400000000
08-06-12 5 18:54:53 0 11700000000000000000000000000000000000000000000000000000000000000000000000000000
000015514400000000
08-06-12 5 18:59:53 0 11700000000000000000000000000000000000000000000000000000000000000000000000000000
000015514400000000
08-06-12 5 19:14:52 0 11700000000000000000000000000000000000000000000000000000000000000000000000000000
000015414400000000[/codebox]

Note that the software I wrote to analyze the data automatically down-converts the temperatures from what the Elk provides. All Elk keypad temperatures need to have 40 subtracted from them while external temperature probes need to have 60 subtracted from them to arrive at the final temp. I think Elk did this since the M1 cannot store negative numbers and they wanted to be able to log below-zero temperatures.

Regardless, taking the last line as an example, you'll see that the temps are three digits long. The first temp (117) is the front door keypad temp. If you subtract 40, you'll get the temp (77 degrees F). This is followed by a bunch of zeros (in groups of 3) that correspond to missing temperature probes and then we finally arrive at the value 154 (attic temp, subtract 60 = 94 degrees) and then the outside temp shown by 144 (outside temp, subtract 60= 84 degrees).

* * * * *

Note that the code does not check the Checksum values to see if the data is valid, but I've yet to get a bad packet.

Spanky, check the code in the Elk M1 RS-232 Protocol Document for computing the checksum, I've spent days trying but I think it's wrong since I can never come up with a correct checksum by strictly following the directions in the document. :D

Again, I'm just learning PHP so the script isn't the prettiest in the world. I know a lot of people here don't/can't/won't buy HomeSeer or CQC for home automation and want to write their own so I figured this would be a nice thing to write about since I know there is a lot of interest.

I plan on writing an entire PHP class library to encapsulate all of the Elk functionality so you can roll your own website that talks with the Elk.

I'd be interested to see any examples that others may have.

Enjoy the code!
 
There's plenty of room in the ELK M1 world for another application ... but if your goal is to provide a free alternative, you should know there are existing good solutions.

If you search this forum you'll find at least two free Home Automation programs featuring comprehensive ELK M1 drivers. Both have built-in web servers, scripting languages, drivers for other peripherals, etc. There's also a free standalone M1 driver implemented in Perl.

See: Premise, Automation Engine, ELK M1::Control

You might like what you see and decide to participate in furthering their development. At the very least, you can study their M1 drivers for use in your project.
 
There's plenty of room in the ELK M1 world for another application ... but if your goal is to provide a free alternative, you should know there are existing good solutions.

If you search this forum you'll find at least two free Home Automation programs featuring comprehensive ELK M1 drivers. Both have built-in web servers, scripting languages, drivers for other peripherals, etc. There's also a free standalone M1 driver implemented in Perl.

See: Premise, Automation Engine, ELK M1::Control

You might like what you see and decide to participate in furthering their development. At the very least, you can study their M1 drivers for use in your project.


123, yep, thanks. I knew about these alternatives but since I haven't seen anything PHP-related I thought I'd throw out a code snippet to let everyone know that it can be done.

I have PHP Sockets disabled on my server for security reasons and further wanted to let the PHP folks know there was a work-around by using the fopen() file system commands to talk with the Elk M1.

Thanks for the post
 
Houston:

I am interested in this TS-109 server. I looked at the website and it seems just a NAS with some additional functions. Can you run any application on this thing? DO you think it could work as a CQC server?
 
Houston:

I am interested in this TS-109 server. I looked at the website and it seems just a NAS with some additional functions. Can you run any application on this thing? DO you think it could work as a CQC server?


MavRic, what an interesting name, especially since I own a '76 two-door Maverick :p

Anyhoo, yep! The TS-109 server is one of the best servers I ever bought. I bought it strictly as a NAS storage device because it had a Gigabit network adapter but came to find out that it is So Much More! It runs Debian Linux (fully configurable via a Telnet or SSH client), comes with Apache to host web sites (I'm hosting 5 sites on mine (one is http://www.pegasuswoodcraft.com. In addition it has a Download Station running Bittorrent if you want to offload those huge downloads to a 14 watt box running all of the time (instead of a 1000 watt PC on for days waiting for your files!). It also has the ability to back up every PC in your house on a scheduled basis should you choose.

It stands about 9 inches high and is about 2 inches wide. It needs no fan (14 watts!), has 128mb of memory and a 500Mhz RISC ARM processor but you'll need to add your favorite SATA drive. The TS-109 Pro II has 256mb of memory at a slightly higher costs.

I also have two D-Link DNS-323 NAS boxes with two 1tb drives in each bay (4tb total) but the access to them is much harder and you can't really configure the Linux install on them like you can on the QNAP.

I'm not sure what platform Cocoontech is running on but I'd bet they could host the entire site on the QNAP with no problems whatsoever.

The folks at QNAP update the firmware about once a month to add new features and functionality so that is a good thing. The latest addition is the inclusion of a NVR update (Network Video Recorder) that supports a lot of cameras and offers full control of them... At No Extra Cost!

I don't work for QNAP nor am I paid by them but can't get over how they sell this box with so many features for such a low price (less than $250).

To answer your questions about running apps... YES! Anything that runs under Linux can be installed on this box. Some examples:

Joomla CMS,
Apache Web Server (included),
FTP (duh!),
Bittorrent download client

and too many others to mention. I recently added the PERL module to mine to check out 123's PERL scripts and it runs them just fine.

To get a perspective, imagine running a dedicated Linux PC, but using only 14 watts of juice!

Can't say enough about it and plan on getting the TS-209 Pro II soon (raid drives, 256mb of RAM, faster processor).

I'll write up a review of it soon and post it in the reviews section. In the attached picture, the QNAP is the device on the shelf furthest to the left to give you an idea of it's size (one of the DNS-323 is second from the right, next to the WAP).
 

Attachments

  • 100_1155.jpg
    100_1155.jpg
    746.7 KB · Views: 21
ya, there's been a lot of requests for a built in newsgroup reader but only a 3rd party nzb grabber at this point. definitely a nice device though. like the joomla support. thanks for posting.
 
HoustonFireFox,
Request the M1SDK.exe from ELK. It generates the Checksum on strings which should make your debugging easier.

edit: Send me a PM today with your email and I will send you the M1SDK.exe. Anyone else that needs it, I will send it to you also.
 
My HA server is an HP e-PC sourced from eBay for $50. Less than 30W and it runs Win XP Pro hosting both MisterHouse and Premise (I'm in transition from one to the other). I can't say enough good things about recycling these old codgers ... and the price is right. :p
 
I have written a PHP script which has an M1 driver as well (with full CRC/checksum support), runs on my OpenWRT based Linksys WRT54G router. This same PHP script (single file!) also maintains an IRC connection, monitors internet (and resets cablemodem through M1 if it locks up), supports remote control through e-mail (e.g., opening garage door using a cellphone text/email message) and a few other HA features. PHP is extremely powerful, and if you want to go low power, a linksys router will work well. I might post the code in the near future, just have to add a few more features and clean it up. I am planning on creating a Flash based interface to edit the settings (on a side note, I have also written an M1 driver in Flash ActionScript 3.0, but you guys knew that one already).
 
Back
Top