23_WEBTHERM.pm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # $Id: 23_WEBTHERM.pm 4793 2014-02-02 11:20:16Z betateilchen $
  2. ################################################################
  3. #
  4. # This module will connect a webbased thermometer
  5. # to your fhem installation.
  6. #
  7. # Further informations about required hardware:
  8. # http://www.wut.de/e-57w0w-ww-dade-000.php
  9. #
  10. # (c) 2010 Sacha Gloor (sacha@imp.ch)
  11. #
  12. # corrections & documentation added for fhem
  13. # 2013-07-30 by betateilchen ®
  14. #
  15. # This file is part of fhem.
  16. #
  17. # Fhem is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU General Public License as published by
  19. # the Free Software Foundation, either version 2 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # Fhem is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU General Public License
  28. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  29. #
  30. ################################################################
  31. package main;
  32. use strict;
  33. use warnings;
  34. use Data::Dumper;
  35. use LWP::UserAgent;
  36. use HTTP::Request;
  37. sub
  38. WEBTHERM_Initialize($)
  39. {
  40. my ($hash) = @_;
  41. $hash->{DefFn} = "WEBTHERM_Define";
  42. $hash->{AttrList} = "loglevel:0,1,2,3,4,5,6";
  43. }
  44. sub
  45. WEBTHERM_Define($$)
  46. {
  47. my ($hash, $def) = @_;
  48. my $name=$hash->{NAME};
  49. my @a = split("[ \t][ \t]*", $def);
  50. my $host = $a[2];
  51. my $host_port = $a[3];
  52. my $interval=$a[4];
  53. $attr{$name}{interval}=$interval if $interval;
  54. return "Usage: define <name> WEBTHERM <ip-address> <port-nr> <poll-interval>" if(int(@a) != 5);
  55. $hash->{Host} = $host;
  56. $hash->{Host_Port} = $host_port;
  57. InternalTimer(gettimeofday()+$interval, "WEBTHERM_GetStatus", $hash, 0);
  58. return;
  59. }
  60. sub
  61. WEBTHERM_GetStatus($)
  62. {
  63. my ($hash) = @_;
  64. my $err_log='';
  65. my $line;
  66. my $name = $hash->{NAME};
  67. my $host = $hash->{Host};
  68. my $interval=$attr{$name}{interval}||300;
  69. InternalTimer(gettimeofday()+$interval, "WEBTHERM_GetStatus", $hash, 0);
  70. if(!defined($hash->{Host_Port})) { return(""); }
  71. my $host_port = $hash->{Host_Port};
  72. ### 2013-07-30 corrected by betateilchen
  73. # my $URL="http://".$host."/Single".$host_port;
  74. my $URL="http://".$host.":".$host_port."/Single";
  75. ### end-of-correction
  76. my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeout => 3);
  77. my $header = HTTP::Request->new(GET => $URL);
  78. my $request = HTTP::Request->new('GET', $URL, $header);
  79. my $response = $agent->request($request);
  80. $err_log.= "Can't get $URL -- ".$response->status_line
  81. unless $response->is_success;
  82. if($err_log ne "")
  83. {
  84. Log GetLogLevel($name,2), "WEBTHERM $name ".$err_log;
  85. return;
  86. }
  87. my $body = $response->content;
  88. my $text='';
  89. my @values=split(/;/,$body);
  90. my $last=$values[$#values];
  91. my $state=$last;
  92. $state=~s/,/./g;
  93. $state=substr($state,0,-2);
  94. my $sensor="temperature";
  95. Log 4, "WEBTHERM_GetStatus: $name $host_port ".$hash->{STATE}." -> ".$state;
  96. $text="Temperature: ".$state;
  97. $hash->{STATE} = "T: ".$state;
  98. $hash->{CHANGED}[0] = $text;
  99. readingsSingleUpdate($name, $sensor, $state, 1);
  100. return;
  101. }
  102. 1;
  103. =pod
  104. not to be translated
  105. =begin html
  106. <a name="WEBTHERM"></a>
  107. <h3>WEBTHERM</h3>
  108. <ul>
  109. This module connects a <a href="http://www.wut.de/e-57w0w-ww-dade-000.php">Web-Thermometer made by W&T</a> to your FHEM installation.<br/>
  110. Currently this module is no longer maintained, but it should work in its current state.<br/>
  111. It is provided "as is" for backward compatibility.<br/>
  112. <br />
  113. <a name="WEBTHERM_Define"></a>
  114. <b>Define</b>
  115. <ul><br/>
  116. <code>define &lt;name&gt; WEBTHERM &lt;ip-address&gt; &lt;port-nr&gt; &lt;interval&gt;</code><br/>
  117. <br/>
  118. Defines a WEBTHERM device at given ip and port.</br>
  119. Values are polled periodically defined by given interval (in seconds).<br/>
  120. Read temperature is written into reading "state".<br/>
  121. </ul>
  122. <br/><br />
  123. <a name="WEBTHERM_Set"></a>
  124. <b>Set</b>
  125. <ul>
  126. N/A
  127. </ul>
  128. <br/><br />
  129. <a name="WEBTHERM_Get"></a>
  130. <b>Get</b>
  131. <ul>
  132. N/A
  133. </ul>
  134. <br/><br />
  135. <a name="WEBTHERM_Attr"></a>
  136. <b>Attr</b>
  137. <ul>
  138. N/A
  139. </ul>
  140. </ul>
  141. =end html
  142. =begin html_DE
  143. <a name="WEBTHERM"></a>
  144. <h3>WEBTHERM</h3>
  145. <ul>
  146. Sorry, keine deutsche Dokumentation vorhanden.<br/><br/>
  147. Die englische Doku gibt es hier: <a href='http://fhem.de/commandref.html#WEBTHERM'>WEBTHERM</a><br/>
  148. </ul>
  149. =end html_DE
  150. =cut