{"id":276,"date":"2008-06-03T00:21:44","date_gmt":"2008-06-03T00:21:44","guid":{"rendered":"http:\/\/dalelane.co.uk\/blog\/?p=276"},"modified":"2008-06-03T07:58:44","modified_gmt":"2008-06-03T07:58:44","slug":"currentcost-hacking-starting-to-identify-appliance-power-usage","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=276","title":{"rendered":"CurrentCost hacking &#8211; starting to identify appliance power usage"},"content":{"rendered":"<p>I needed a break from work tonight, so went back to playing with the <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=265\">CurrentCost meter<\/a> &#8211; a chance to try a few new things.<\/p>\n<p><strong>The objective<\/strong><br \/>\nI want to make a start on identifying how much electricity different things in my house use. To begin, I&#8217;m going to start with a very manual user-driven approach: <\/p>\n<p>Subscribe to updates from the CurrentCost meter, and when a significant change in usage occurs, ask me what I&#8217;ve just switched on or off, and collect that information to build up a record of how much electricity different devices use.<\/p>\n<p><strong>How?<\/strong><br \/>\nIt&#8217;s already quite late, so I just wanted to hack a quick first version together. I decided to write it as a small Java app. <\/p>\n<p>As I&#8217;ve mentioned before, I&#8217;m publishing the CurrentCost readings to a small broker running on <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=267\">my home server<\/a>. The plan was to write a Java application that uses <a target=\"_blank\" href=\"http:\/\/mqtt.org\/\">MQTT<\/a> to subscribe to updates from the broker. <\/p>\n<p>Why? Because I&#8217;ve not used Java on the Slug before, or with MQTT. (<em>Is that not a good enough reason? \ud83d\ude42 <\/em>)<\/p>\n<p>I&#8217;ve written it as a command-line app, because it&#8217;s a quick way to run it from different devices around the house. (That is, by cheating \ud83d\ude42 I&#8217;m actually running the app on the home server, using PuTTY \/ <a target=\"_blank\" href=\"http:\/\/www.pocketputty.net\/\">PocketPuTTY<\/a> \/ SSH etc. to run it from my ThinkPad, PDAs, mobile, EEE PC, etc.). <\/p>\n<p><!--more--><strong>Step 1 &#8211; Get Java on to the Slug<\/strong><br \/>\nI&#8217;m using <a href=\"http:\/\/jikes.sourceforge.net\/\" target=\"_blank\">Jikes<\/a> for SlugOS\/LE. I found instructions to set it up on <a href=\"http:\/\/www.nslu2-linux.org\/wiki\/HowTo\/UseJavaOnTheSlug\" target=\"_blank\">nslu2-linux.org<\/a>, and found the packages via <a href=\"http:\/\/ipkgfind.nslu2-linux.org\/\" target=\"_blank\">ipkgfind<\/a>: <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.8em\">wget http:\/\/ipkg.nslu2-linux.org\/feeds\/optware\/slugosle\/cross\/unstable\/classpath_0.97.1-1_arm.ipk\r\nwget http:\/\/ipkg.nslu2-linux.org\/feeds\/optware\/slugosle\/cross\/unstable\/jamvm_1.5.1-1_arm.ipk\r\nwget http:\/\/ipkg.nslu2-linux.org\/feeds\/optware\/slugosle\/cross\/unstable\/zlib_1.2.3-2_arm.ipk\r\nwget http:\/\/ipkg.nslu2-linux.org\/feeds\/optware\/slugosle\/cross\/unstable\/jikes_1.22-1_arm.ipk\r\n\r\nipkg install classpath_0.97.1-1_arm.ipk jamvm_1.5.1-1_arm.ipk zlib_1.2.3-2_arm.ipk \r\nipkg install jikes_1.22-1_arm.ipk -force-depends<\/pre>\n<p><strong>Step 2 &#8211; Get the Java MQTT client library<\/strong><br \/>\nI found a link to <a href=\"http:\/\/www.ibm.com\/support\/docview.wss?rs=171&#038;uid=swg24006006&#038;loc=en_US&#038;cs=utf-8&#038;lang=en\" target=\"_blank\">a Java client library<\/a> on <a href=\"http:\/\/mqtt.org\/\" target=\"_blank\">mqtt.org<\/a>. <\/p>\n<p>The <a href=\"ftp:\/\/ftp.software.ibm.com\/software\/integration\/support\/supportpacs\/individual\/ia92.zip\">zip file<\/a> there contains a <code>J2SE\/wmqtt.jar<\/code> which I copied to the server. <\/p>\n<p><strong>Step 3 &#8211; Find some existing Java MQTT client sample code<\/strong><br \/>\nThere is a good <a href=\"http:\/\/www.ibm.com\/developerworks\/websphere\/library\/techarticles\/0508_oconnell\/0508_oconnell.html\" target=\"_blank\">developerWorks article<\/a> with source code for writing MQTT subscribers in Java. I started with <a href=\"http:\/\/www.ibm.com\/developerworks\/websphere\/library\/techarticles\/0508_oconnell\/0508_oconnell.html#N100E3\" target=\"_blank\">the Callbacks example<\/a> which gives you enough code to subscribe to the broker, and get the data out of messages received as a byte array.<\/p>\n<p><strong>Step 4 &#8211; Compiling and running<\/strong><br \/>\nTo compile:<\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.8em\">\/opt\/bin\/jikes -classpath \/opt\/share\/jamvm\/classes.zip:\/opt\/share\/classpath\/glibj.zip:\/opt\/mqtt\/java\/J2SE\/wmqtt.jar:. CurrentCostTrainer.java<\/pre>\n<p>To run:<\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.8em\">\/opt\/bin\/jamvm -classpath \/opt\/share\/jamvm\/classes.zip:\/opt\/share\/classpath\/glibj.zip:\/opt\/mqtt\/java\/J2SE\/wmqtt.jar:. CurrentCostTrainer<\/pre>\n<p>I did run into a small problem running this, which required me to install the <code>file<\/code> package.<\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.8em\">ipkg install file<\/pre>\n<p>The <a href=\"http:\/\/blog.weefeel.net\/?p=11\" target=\"_blank\">reason for this is documented here<\/a>.<\/p>\n<p><strong>Step 5 &#8211; The app<\/strong><br \/>\nThat was pretty much it &#8211; the bits of the app that I wrote are fairly trivial. The app runs at the command line, stores the last known reading from the CurrentCost meter, and if a new reading is significantly different from it, it prompts for a description of what changed.<\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.8em\">00:55:46 : I noticed a change of 20W. What did you just switch off? [unknown] : BT Vision\r\n00:56:01 : I noticed a change of 82W. What did you just switch off? [unknown] : TV<\/pre>\n<p>To save me having to watch the prompt the whole time, I added a line to make it beep to get my attention when it prints a prompt for the user. <\/p>\n<p>The descriptions entered by the user are being written to a text file. <\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.8em\">dalelane@minipooter:~$ more CurrentCostTraining.xml\r\n&lt;ccupdate&gt;\r\n  &lt;WDiff&gt;9&lt;\/WWDiff&gt;\r\n  &lt;descr&gt;XBox 360 - standby&lt;\/descr&gt;\r\n&lt;\/ccupdate&gt;\r\n&lt;ccupdate&gt;\r\n  &lt;WDiff&gt;55&lt;\/WWDiff&gt;\r\n  &lt;descr&gt;ThinkPad&lt;\/descr&gt;\r\n&lt;\/ccupdate&gt;\r\n&lt;ccupdate&gt;\r\n  &lt;WDiff&gt;54&lt;\/WWDiff&gt;\r\n  &lt;descr&gt;ThinkPad&lt;\/descr&gt;\r\n&lt;\/ccupdate&gt;\r\n&lt;ccupdate&gt;\r\n  &lt;WDiff&gt;20&lt;\/WWDiff&gt;\r\n  &lt;descr&gt;BT Vision&lt;\/descr&gt;\r\n&lt;\/ccupdate&gt;\r\n&lt;ccupdate&gt;\r\n  &lt;WDiff&gt;82&lt;\/WWDiff&gt;\r\n  &lt;descr&gt;TV&lt;\/descr&gt;\r\n&lt;\/ccupdate&gt;<\/pre>\n<p>The source, if it is helpful for anyone, can be found <a href=\"http:\/\/dalelane.co.uk\/blog\/post-images\/080602-CurrentCostTrainer.java.txt\">here<\/a>. <\/p>\n<p><strong>What next?<\/strong><br \/>\nSome ideas&#8230;<\/p>\n<p>Instead of cheating and running the client on the server itself, the <b>query prompts could be properly sent to a mobile<\/b>. <\/p>\n<p>Previous results could be examined before generating the query, which would let the <b>query prompt include allowing some possible suggestions<\/b>. E.g. &#8220;I think you&#8217;ve just switched on the television. Is this correct?&#8221;<\/p>\n<p>Comparisons need to be made <b>smarter than just between consecutive CurrentCost measurements<\/b> &#8211; it needs to look beyond that to try and identify changes. <\/p>\n<p>It&#8217;s currently based on the simplistic assumption that appliances have a roughly consistent power usage. The next step would be to try and <b>identify curves in power usage graphs<\/b>, rather than just measure initial power surges when you first start a device.<\/p>\n<p>All in all, it&#8217;s a very simple first step&#8230; but it&#8217;s a start. There is a certain geeky fun in switching something on or off anywhere in your house, and having your phone beep and say &#8220;I noticed you switched something on&#8221; \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I needed a break from work tonight, so went back to playing with the CurrentCost meter &#8211; a chance to try a few new things. The objective I want to make a start on identifying how much electricity different things in my house use. To begin, I&#8217;m going to start with a very manual user-driven [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[194,219,213,197,199,200],"class_list":["post-276","post","type-post","status-publish","format-standard","hentry","category-code","tag-currentcost","tag-java","tag-nslu","tag-nslu2","tag-slug","tag-slugos"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=276"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/276\/revisions"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}