Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SquishIt and NancyNancy and returning a pdf »
    comments

    Introduction

    People who follow me might have heard me complain about plugwise and their poor service and late delivery. So I have at least some mixed feelings about plugwise, to say the least.

    But their product is awesome and works.

    So what does plugwise do I hear you ask. Well, you plug them in to an electricity socket and then you plug your device into the plugwise. You can now see how much electricity the device or devices are using and you can turn them on or off from a distance or via a schedule. This opens lots of opportunities. The bad part, their software only works on windows and you need a Zigbee compatible USB-stick to communicate with them. Their are some efforts to make them work on linux and I even read that you can control them via the Raspberry Pi. This opens lots of opportunities. One being that I can build my own webserver to control the devices and then log in to that server from my phone, tablet or other computers that has a browser. And so today I got it working.

    You can read all on the plugwise website.

    Source

    I will use the builtin webserver from the source software to communicate with. For this you need to open the source software.

    Then click on Program and Enable the webserver.

    you can now just browse to http://localhost:8080 and see your modules and their energy consumption and you can turn them on or off.

    I could now just adapt those files. But it will be easier if I just create a second webapplication that talks to that server. This way I can program it like I want.

    Nancy

    So how do talk to that server and get information. Easy. Well not really. Anyway. Go to you program files (x86) folder search for plugwise then Plugwise Source then www where the webpage hides and then the xml folder. In this folder you will find two files. appliances.xml and blocks.xml.

    If you open the aplliances.xml file it will look like this.

    1. <% include '/xml/blocks.xml' %>
    2. <PlugwiseSource Version="1.0">
    3.   <Appliances>
    4. <%
    5.   foreach Plugwise.Appliances
    6.     Write System.Blocks['xml_Appliance']
    7.   /foreach
    8. %>  
    9.   </Appliances>
    10. <Modules>
    11. <%
    12.   foreach Plugwise.Modules
    13.     Write System.Blocks['xml_Module']
    14.   /foreach
    15. %>
    16.   </Modules>
    17. </PlugwiseSource>

    That is weird. That looks like a scripting language. And it is.

    If you go to http://localhost:8080/xml/appliances.xml you will see this.

    1. This XML file does not appear to have any style information associated with it. The document tree is shown below.
    2. <PlugwiseSource Version="1.0">
    3. <Appliances>
    4. <Appliance Id="1" Name="Computer" Type="computer_desktop" PowerUsage="14,2000" TotalUsage="0,86" PowerState="on" Module="3" Image="/sysimg/32/computer_desktop_on.png"/>
    5. <Appliance Id="3" Name="Dishwasher" Type="dishwasher" PowerUsage="0,0000" TotalUsage="0,08" PowerState="on" Module="10" Image="/sysimg/32/dishwasher_on.png"/>
    6. <Appliance Id="7" Name="Microwave oven" Type="oven_microwave" PowerUsage="2,5000" TotalUsage="0,05" PowerState="on" Module="7" Image="/sysimg/32/oven_microwave_on.png"/>
    7. <Appliance Id="9" Name="Nas and switch" Type="computer_desktop" PowerUsage="4,9000" TotalUsage="0,62" PowerState="on" Module="9" Image="/sysimg/32/computer_desktop_on.png"/>
    8. <Appliance Id="8" Name="Refrigerator" Type="refrigerator" PowerUsage="0,0000" TotalUsage="0,55" PowerState="on" Module="8" Image="/sysimg/32/refrigerator_on.png"/>
    9. <Appliance Id="2" Name="TV" Type="tv" PowerUsage="-0,2000" TotalUsage="0,57" PowerState="off" Module="4" Image="/sysimg/32/tv_off.png"/>
    10. <Appliance Id="6" Name="TV 2" Type="tv" PowerUsage="204,8000" TotalUsage="3,08" PowerState="on" Module="2" Image="/sysimg/32/tv_on.png"/>
    11. <Appliance Id="4" Name="Water heater vessel" Type="water_heater_vessel" PowerUsage="0,0000" TotalUsage="1,11" PowerState="on" Module="11" Image="/sysimg/32/water_heater_vessel_on.png"/>
    12. <Appliance Id="5" Name="Water heater vessel 2" Type="water_heater_vessel" PowerUsage="0,0000" TotalUsage="6,71" PowerState="off" Module="5" Image="/sysimg/32/water_heater_vessel_off.png"/>
    13. </Appliances>
    14. <Modules>
    15. <Module Id="6" Name="278C42A" MAC="000D6F000278C42A" Type="-1"/>
    16. <Module Id="1" Name="278683C" MAC="000D6F000278683C" Type="0"/>
    17. <Module Id="3" Name="2786B62" MAC="000D6F0002786B62" Type="2" PowerUsage="14,1655" RelayState="closed" Appliance="1"/>
    18. <Module Id="4" Name="2786D98" MAC="000D6F0002786D98" Type="2" PowerUsage="-0,1812" RelayState="open" Appliance="2"/>
    19. <Module Id="7" Name="2786DA0" MAC="000D6F0002786DA0" Type="2" PowerUsage="2,4894" RelayState="closed" Appliance="7"/>
    20. <Module Id="10" Name="2786F1D" MAC="000D6F0002786F1D" Type="2" PowerUsage="0,0000" RelayState="closed" Appliance="3"/>
    21. <Module Id="8" Name="278B571" MAC="000D6F000278B571" Type="2" PowerUsage="0,0000" RelayState="closed" Appliance="8"/>
    22. <Module Id="11" Name="278B5C5" MAC="000D6F000278B5C5" Type="2" PowerUsage="0,0000" RelayState="closed" Appliance="4"/>
    23. <Module Id="9" Name="278B834" MAC="000D6F000278B834" Type="2" PowerUsage="4,8539" RelayState="closed" Appliance="9"/>
    24. <Module Id="5" Name="278C37A" MAC="000D6F000278C37A" Type="2" PowerUsage="0,0000" RelayState="open" Appliance="5"/>
    25. <Module Id="2" Name="D35C4B" MAC="000D6F0000D35C4B" Type="1" PowerUsage="204,7996" RelayState="closed" Appliance="6"/>
    26. </Modules>
    27. </PlugwiseSource>

    Yep that is all the information we need to make a website.

    We can now create our nancy website, which you can find on github.

    I guess you all know how to make modules and views and the rest to get it to work.

    And here is a service to read the data.

    1. IList<PlugwiseApplianceModel> FindAll()
    2.         {
    3.             var xml = XDocument.Load("http://localhost:8080/xml/appliances.xml");
    4.  
    5.             var q = from b in xml.Descendants("Appliance")
    6.             select new PlugwiseApplianceModel
    7.             {
    8.                 Name = b.Attribute("Name")==null?"":b.Attribute("Name").Value,
    9.                 Id = b.Attribute("Id") == null ? "" : b.Attribute("Id").Value,
    10.                 TotalUsage = b.Attribute("TotalUsage") == null ? "" : b.Attribute("TotalUsage").Value,
    11.                 Type = b.Attribute("Type") == null ? "" : b.Attribute("Type").Value,
    12.                 PowerUsage = b.Attribute("PowerUsage") == null ? "" : b.Attribute("PowerUsage").Value,
    13.                 PowerState = b.Attribute("PowerState") == null ? "" : b.Attribute("PowerState").Value,
    14.                 Image = b.Attribute("Image") == null ? "" : b.Attribute("Image").Value,
    15.                 Module = b.Attribute("Module") == null ? "" : b.Attribute("Module").Value                  
    16.             };
    17.             return q.ToList();
    18.         }

    Easy as Pie.

    The clue is in the blocks.xml file where they format the output.

    1. <%
    2.  format 'Module.PowerUsage' as '{0:0.0000}'
    3.  format 'Appliance.PowerUsage' as '{0:0.0000}'
    4.  format 'Appliance.TotalUsage' as '{0:0.00}'
    5. %>
    6. <% block 'xml_Appliance'%>
    7. <Appliance Id="<%=.Id%>" Name="<%=.Name%>" Type="<%=.Type%>" PowerUsage="<%=.PowerUsage%>" TotalUsage="<%=.TotalUsage%>" PowerState="<%=.PowerState%>" Module="<%=.Module.Id%>" Image="<%$_Base%>/sysimg/32/<%=.StatusImageName%>.png" />
    8. <%/block%>
    9. <% block 'xml_Module'%>
    10. <Module Id="<%=.Id%>" Name="<%=.Name%>" MAC="<%=.MACAddress%>" Type="<%=.Type%>" <%if .Type>0%> PowerUsage="<%=.PowerUsage%>" RelayState="<%=.RelayState%>" Appliance="<%=.Appliance.Id%>" <%/if%> />
    11. <%/block%>
    12. <% block 'xml_Group'%>
    13. <Group Id="<%=.Id%>" Name="<%=.Name%>" />
    14. <%/block%>
    15. <% block 'xml_Room'%>
    16. <Room Id="<%=.Id%>" Name="<%=.Name%>" Appliances="<%=.Appliances%>" />
    17. <%/block%>

    As you can see I added the blocks for Group and Rooms.

    I for one made a modules.xml to get that data seperatly from the appliances and I made a rooms.xml to get the rooms data.

    I also made a module.xml to get the data from a given module.

    1. <% include '/xml/blocks.xml' %>
    2. <PlugwiseSource Version="1.0">
    3.   <Groups>
    4. <%
    5.   $param = Request.Get["moduleid"]
    6.   $module = Module($param)
    7. %>
    8. <Module Id="<%=$module.Id%>" Name="<%=$module.Name%>" MAC="<%=$module.MACAddress%>" Type="<%=$module.Type%>" <%if $module.Type>0%> PowerUsage="<%=$module.PowerUsage%>" RelayState="<%=$module.RelayState%>" Appliance="<%=$module.Appliance.Id%>" <%/if%>
    9. />
    10.   </Groups>
    11. </PlugwiseSource>

    Which you can call like this http://localhost:8080/xml/module.xml?moduleid=6

    So that is how this all works.

    And this is what it looks like for now.

    But I'm sure you can imagine where we are going with this. It will be easy to add a mobile version and so on. It was getting started that was difficult, since there is not to much documentation to learn from or examples.

    Conclusion

    It is fairly simple to get this to work. Now I just need to buy a cheap, low energy consuming PC that runs Windows and that can serve as webserver.
    And yes the next step is to be able to turn on and off the modules. But that is simple if you read the documentation.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    nancy, plugwise
    InstapaperVote on HN

    1 comment

    Comment from: Steven Robbins [Visitor] · http://www.grumpydev.com
    Steven Robbins Or a Raspberry Pi... :-)
    01/06/13 @ 07:34

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)