44_TEK603.pm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # $Id: 44_TEK603.pm 11877 2016-08-02 11:10:12Z eisler $
  2. ####################################################################################################
  3. #
  4. # 44_TEK603.pm
  5. #
  6. # Copyright: Stephan Eisler
  7. # Email: fhem.dev@hausautomatisierung.co
  8. #
  9. # This file is part of fhem.
  10. #
  11. # Fhem is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation, either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # Fhem is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  23. #
  24. ####################################################################################################
  25. package main;
  26. use strict;
  27. use warnings;
  28. use Digest::CRC; # libdigest-crc-perl
  29. sub TEK603_Initialize($);
  30. sub TEK603_define($$);
  31. sub TEK603_doInit($);
  32. sub TEK603_undef($$);
  33. sub TEK603_ready($);
  34. sub TEK603_read($);
  35. sub TEK603_reconnect($);
  36. sub TEK603_Initialize($) {
  37. my ($hash) = @_;
  38. require $attr{global}{modpath} . '/FHEM/DevIo.pm';
  39. $hash->{ReadFn} = 'TEK603_read';
  40. $hash->{ReadyFn} = 'TEK603_ready';
  41. $hash->{DefFn} = 'TEK603_define';
  42. $hash->{UndefFn} = 'TEK603_undef';
  43. $hash->{AttrList} = 'do_not_notify:0,1 dummy:1,0 loglevel:0,1,2,3,4,5,6 ';
  44. }
  45. sub TEK603_define($$) {
  46. my ($hash, $def) = @_;
  47. my @a = split('[ \t][ \t]*', $def);
  48. my $name = $a[0];
  49. my $dev = $a[2];
  50. $hash->{DEF} = $dev;
  51. my $msg = '';
  52. if( @a != 3) {
  53. $msg = 'wrong syntax: define <name> TEK603 {none | devicename}';
  54. Log3 $name, 3, $msg;
  55. return $msg;
  56. }
  57. DevIo_CloseDev($hash);
  58. $hash->{PORTSTATE} = $hash->{STATE};
  59. if($dev eq 'none') {
  60. Log3 $name, 3, "device is none, commands will be echoed only";
  61. $attr{$name}{dummy} = 1;
  62. return undef;
  63. }
  64. $hash->{DeviceName} = $dev;
  65. my $ret = DevIo_OpenDev($hash, 0, 'TEK603_doInit');
  66. return $ret;
  67. }
  68. sub TEK603_doInit($) {
  69. my ($hash) = @_;
  70. my $po = $hash->{USBDev};
  71. my $dev = $hash->{DeviceName};
  72. my $name = $hash->{NAME};
  73. # Parameter 115200, 8, 1, even, none
  74. $po->reset_error();
  75. $po->baudrate(115200);
  76. $po->databits(8);
  77. $po->stopbits(1);
  78. $po->parity('none');
  79. $po->handshake('none');
  80. $po->dtr_active(1);
  81. $po->rts_active(1);
  82. if (!$po->write_settings) {
  83. undef $po;
  84. $hash->{STATE} = $name . 'Error on write serial line settings on device ' . $dev;
  85. Log3 $name, 3, $hash->{STATE};
  86. return $hash->{STATE} . "\n";
  87. }
  88. Log3 $name, 3, "connected to device $dev";
  89. $hash->{STATE} = 'open';
  90. $hash->{PORTSTATE} = $hash->{STATE};
  91. return undef;
  92. }
  93. sub TEK603_undef($$) {
  94. my ($hash, $name) = @_;
  95. foreach my $d (sort keys %defs) {
  96. if(defined($defs{$d}) && defined($defs{$d}{IODev}) && $defs{$d}{IODev} == $hash) {
  97. Log3 $name, 4, "deleting port for $d";
  98. delete $defs{$d}{IODev};
  99. }
  100. }
  101. DevIo_CloseDev($hash);
  102. $hash->{PORTSTATE} = $hash->{STATE};
  103. return undef;
  104. }
  105. sub TEK603_ready($) {
  106. my ($hash) = @_;
  107. return DevIo_OpenDev($hash, 1, 'TEK603_doInit') if($hash->{STATE} eq 'disconnected');
  108. # This is relevant for windows/USB only
  109. my $po = $hash->{USBDev};
  110. my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags) = $po->status;
  111. return ($InBytes > 0);
  112. }
  113. sub TEK603_read($) {
  114. my ($hash) = @_;
  115. my $name = $hash->{NAME};
  116. my $buf = DevIo_SimpleRead($hash);
  117. return '' if(!defined($buf));
  118. # convert to hex string
  119. $hash->{buffer} = unpack ('H*', $buf);
  120. my $lenght = hex(substr($hash->{buffer},4,4))*2;
  121. #my $cmd = substr($hash->{buffer},8,2);
  122. #my $flags = substr($hash->{buffer},10,2);
  123. my $time = sprintf '%02d:%02d:%02d', hex(substr($hash->{buffer},12,2)), hex(substr($hash->{buffer},14,2)), hex(substr($hash->{buffer},16,2));
  124. #my $epromStart = hex(substr($hash->{buffer},18,4));
  125. #my $epromEnd = hex(substr($hash->{buffer},22,4));
  126. my $payloadlenght = $lenght - 26 - 4;
  127. my $payload = substr($hash->{buffer},26,$payloadlenght);
  128. my $crc = substr($hash->{buffer},26 + $payloadlenght,4);
  129. my $ctx = Digest::CRC->new(width=>16, init=>0x0, poly=>0x1021, refout=>0, xorout=>0);
  130. $ctx->add(pack 'H*',(substr($hash->{buffer},0,26 + $payloadlenght)));
  131. my $digest = $ctx->hexdigest;
  132. return '' if($crc ne $digest);
  133. # payload
  134. my $temp = sprintf '%.2f', ((hex(substr($payload, 0,2)) - 40 - 32) / 1.8);
  135. my $Ullage = hex(substr($payload,2,2)) * 256 + hex(substr($payload,4,2));
  136. my $RemainingUsableLevel = hex(substr($payload,6,2)) * 256 + hex(substr($payload,8,2));
  137. my $TotalUsableCapacity = hex(substr($payload,10,2)) * 256 + hex(substr($payload,12,2));
  138. return '' if($temp eq "-40.00" && $Ullage eq "0"); # TankLevel=NO_DATA
  139. # Calculations
  140. my $RemainingUsablePercent = round($RemainingUsableLevel / $TotalUsableCapacity * 100,01);
  141. #Log3 $name, 5, $hash->{buffer};
  142. Log3 $name, 5, "Time:$time Temp:$temp Ullage:$Ullage RemainingUsableLevel:$RemainingUsableLevel RemainingUsablePercent:$RemainingUsablePercent TotalUsableCapacity:$TotalUsableCapacity";
  143. readingsBeginUpdate($hash);
  144. readingsBulkUpdate($hash, "Time", $time);
  145. readingsBulkUpdate($hash, "Temperature", $temp);
  146. readingsBulkUpdate($hash, "Ullage", $Ullage);
  147. readingsBulkUpdate($hash, "RemainingUsableLevel", $RemainingUsableLevel);
  148. readingsBulkUpdate($hash, "RemainingUsablePercent", $RemainingUsablePercent);
  149. readingsBulkUpdate($hash, "TotalUsableCapacity", $TotalUsableCapacity);
  150. readingsEndUpdate($hash, 1);
  151. }
  152. sub TEK603_reconnect($) {
  153. my ($hash) = @_;
  154. my $name = $hash->{NAME};
  155. Log3 $name, 3, "Wrong Data received. We reconnect Device";
  156. # Sometime the device sends a lot of waste and we must reconnect.
  157. DevIo_CloseDev($hash);
  158. $hash->{PORTSTATE} = $hash->{STATE};
  159. select(undef, undef, undef, 0.1);
  160. DevIo_OpenDev($hash, 0, 'TEK603_doInit');
  161. }
  162. 1;
  163. =pod
  164. =begin html
  165. <a name="TEK603"></a>
  166. <h3>TEK603</h3>
  167. <ul>
  168. The TEK603 is a fhem module for the Tekelek TEK603 Eco Monitor a liquid level monitor designed for residential and small commercial applications.
  169. It works in conjunction with a TEK653 Sonic transmitter mounted on the top of the tank.
  170. <br /><br /><br />
  171. <b>Prerequisites</b><br>
  172. The module requires the perl module Digest::CRC<br />
  173. On a debian based system the module can be installed with<br />
  174. <code>
  175. sudo apt-get install libdigest-crc-perl<br />
  176. </code>
  177. <br /><br />
  178. <a name="TEK603_Define"></a>
  179. <b>Define</b>
  180. <ul>
  181. <code>define &lt;name&gt; TEK603 /dev/ttyUSBx</code><br />
  182. <br />
  183. Defines an TEK603 Eco Monitor device connected to USB.<br /><br />
  184. Examples:
  185. <ul>
  186. <code>define OelTank TEK603 /dev/ttyUSB0</code><br />
  187. </ul>
  188. </ul><br />
  189. <a name="TEK603_Readings"></a>
  190. <b>Readings</b>
  191. <ul>
  192. <li>Time<br />
  193. TEK603 internal Time</li>
  194. <li>Temperature<br />
  195. Sensor Temperature</li>
  196. <li>Ullage<br />
  197. Sensor Measured Ullage</li>
  198. <li>RemainingUsableLevel<br />
  199. This is the usable level, with deductions due to the sensor offset and outlet height. (Liters)</li>
  200. <li>RemainingUsablePercent<br />
  201. This is the usable level in percent (calculated from RemainingUsableLevel and TotalUsableCapacity)</li>
  202. <li>TotalUsableCapacity<br />
  203. This is the usable volume, with deductions due to the sensor offset and outlet height. (Liters)</li>
  204. </ul><br />
  205. </ul><br />
  206. =end html
  207. =cut