| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- # $Id: 44_TEK603.pm 17651 2018-10-31 12:05:23Z eisler $
- ####################################################################################################
- #
- # 44_TEK603.pm
- #
- # Copyright: Stephan Eisler
- # Email: fhem.dev@hausautomatisierung.co
- #
- # This file is part of fhem.
- #
- # Fhem is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 2 of the License, or
- # (at your option) any later version.
- #
- # Fhem is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with fhem. If not, see <http://www.gnu.org/licenses/>.
- #
- ####################################################################################################
- package main;
- use strict;
- use warnings;
- use Digest::CRC; # libdigest-crc-perl
- sub TEK603_Initialize($);
- sub TEK603_define($$);
- sub TEK603_doInit($);
- sub TEK603_undef($$);
- sub TEK603_ready($);
- sub TEK603_read($);
- sub TEK603_reconnect($);
- sub TEK603_Initialize($) {
- my ($hash) = @_;
- require $attr{global}{modpath} . '/FHEM/DevIo.pm';
- $hash->{ReadFn} = 'TEK603_read';
- $hash->{ReadyFn} = 'TEK603_ready';
- $hash->{DefFn} = 'TEK603_define';
- $hash->{UndefFn} = 'TEK603_undef';
- $hash->{AttrList} = 'do_not_notify:0,1 dummy:1,0 loglevel:0,1,2,3,4,5,6 ' .
- $readingFnAttributes;
- }
- sub TEK603_define($$) {
- my ($hash, $def) = @_;
- my @a = split('[ \t][ \t]*', $def);
- my $name = $a[0];
- my $dev = $a[2];
- $hash->{DEF} = $dev;
- my $msg = '';
- if( @a != 3) {
- $msg = 'wrong syntax: define <name> TEK603 {none | devicename}';
- Log3 $name, 3, $msg;
- return $msg;
- }
- DevIo_CloseDev($hash);
- $hash->{PORTSTATE} = $hash->{STATE};
- if($dev eq 'none') {
- Log3 $name, 3, "device is none, commands will be echoed only";
- $attr{$name}{dummy} = 1;
- return undef;
- }
- $hash->{DeviceName} = $dev;
- my $ret = DevIo_OpenDev($hash, 0, 'TEK603_doInit');
- return $ret;
- }
- sub TEK603_doInit($) {
- my ($hash) = @_;
- my $po = $hash->{USBDev};
- my $dev = $hash->{DeviceName};
- my $name = $hash->{NAME};
- # Parameter 115200, 8, 1, even, none
- $po->reset_error();
- $po->baudrate(115200);
- $po->databits(8);
- $po->stopbits(1);
- $po->parity('none');
- $po->handshake('none');
- $po->dtr_active(1);
- $po->rts_active(1);
- if (!$po->write_settings) {
- undef $po;
- $hash->{STATE} = $name . 'Error on write serial line settings on device ' . $dev;
- Log3 $name, 3, $hash->{STATE};
- return $hash->{STATE} . "\n";
- }
- Log3 $name, 3, "connected to device $dev";
- $hash->{STATE} = 'open';
- $hash->{PORTSTATE} = $hash->{STATE};
- return undef;
- }
- sub TEK603_undef($$) {
- my ($hash, $name) = @_;
- foreach my $d (sort keys %defs) {
- if(defined($defs{$d}) && defined($defs{$d}{IODev}) && $defs{$d}{IODev} == $hash) {
- Log3 $name, 4, "deleting port for $d";
- delete $defs{$d}{IODev};
- }
- }
- DevIo_CloseDev($hash);
- $hash->{PORTSTATE} = $hash->{STATE};
- return undef;
- }
- sub TEK603_ready($) {
- my ($hash) = @_;
- return DevIo_OpenDev($hash, 1, 'TEK603_doInit') if($hash->{STATE} eq 'disconnected');
- # This is relevant for windows/USB only
- my $po = $hash->{USBDev};
- my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags) = $po->status;
- return ($InBytes > 0);
- }
- sub TEK603_read($) {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- my $buf = DevIo_SimpleRead($hash);
- return '' if(!defined($buf));
- # convert to hex string
- $hash->{buffer} = unpack ('H*', $buf);
- my $lenght = hex(substr($hash->{buffer},4,4))*2;
- #my $cmd = substr($hash->{buffer},8,2);
- #my $flags = substr($hash->{buffer},10,2);
- my $time = sprintf '%02d:%02d:%02d', hex(substr($hash->{buffer},12,2)), hex(substr($hash->{buffer},14,2)), hex(substr($hash->{buffer},16,2));
- #my $epromStart = hex(substr($hash->{buffer},18,4));
- #my $epromEnd = hex(substr($hash->{buffer},22,4));
- my $payloadlenght = $lenght - 26 - 4;
- my $payload = substr($hash->{buffer},26,$payloadlenght);
- my $crc = substr($hash->{buffer},26 + $payloadlenght,4);
- my $ctx = Digest::CRC->new(width=>16, init=>0x0, poly=>0x1021, refout=>0, xorout=>0);
- $ctx->add(pack 'H*',(substr($hash->{buffer},0,26 + $payloadlenght)));
- my $digest = $ctx->hexdigest;
- return '' if($crc ne $digest);
- # payload
- my $temp = sprintf '%.2f', ((hex(substr($payload, 0,2)) - 40 - 32) / 1.8);
- my $Ullage = hex(substr($payload,2,2)) * 256 + hex(substr($payload,4,2));
- my $RemainingUsableLevel = hex(substr($payload,6,2)) * 256 + hex(substr($payload,8,2));
- my $TotalUsableCapacity = hex(substr($payload,10,2)) * 256 + hex(substr($payload,12,2));
- return '' if($temp eq "-40.00" && $Ullage eq "0"); # TankLevel=NO_DATA
- # Calculations
- my $RemainingUsablePercent = round($RemainingUsableLevel / $TotalUsableCapacity * 100,01);
- #Log3 $name, 5, $hash->{buffer};
- Log3 $name, 5, "Time:$time Temp:$temp Ullage:$Ullage RemainingUsableLevel:$RemainingUsableLevel RemainingUsablePercent:$RemainingUsablePercent TotalUsableCapacity:$TotalUsableCapacity";
- readingsBeginUpdate($hash);
- readingsBulkUpdate($hash, "Time", $time);
- readingsBulkUpdate($hash, "Temperature", $temp);
- readingsBulkUpdate($hash, "Ullage", $Ullage);
- readingsBulkUpdate($hash, "RemainingUsableLevel", $RemainingUsableLevel);
- readingsBulkUpdate($hash, "RemainingUsablePercent", $RemainingUsablePercent);
- readingsBulkUpdate($hash, "TotalUsableCapacity", $TotalUsableCapacity);
- readingsEndUpdate($hash, 1);
- }
- sub TEK603_reconnect($) {
- my ($hash) = @_;
- my $name = $hash->{NAME};
- Log3 $name, 3, "Wrong Data received. We reconnect Device";
- # Sometime the device sends a lot of waste and we must reconnect.
- DevIo_CloseDev($hash);
- $hash->{PORTSTATE} = $hash->{STATE};
- select(undef, undef, undef, 0.1);
- DevIo_OpenDev($hash, 0, 'TEK603_doInit');
- }
- 1;
- =pod
- =item summary devices communicating with TEK603
- =begin html
- <a name="TEK603"></a>
- <h3>TEK603</h3>
- <ul>
- The TEK603 is a fhem module for the Tekelek TEK603 Eco Monitor a liquid level monitor designed for residential and small commercial applications.
- It works in conjunction with a TEK653 Sonic transmitter mounted on the top of the tank.
- <br /><br /><br />
- <b>Prerequisites</b><br>
- The module requires the perl module Digest::CRC<br />
- On a debian based system the module can be installed with<br />
- <code>
- sudo apt-get install libdigest-crc-perl<br />
- </code>
- <br /><br />
- <a name="TEK603_Define"></a>
- <b>Define</b>
- <ul>
- <code>define <name> TEK603 /dev/ttyUSBx</code><br />
- <br />
- Defines an TEK603 Eco Monitor device connected to USB.<br /><br />
- Examples:
- <ul>
- <code>define OelTank TEK603 /dev/ttyUSB0</code><br />
- </ul>
- </ul><br />
- <a name="TEK603_Readings"></a>
- <b>Readings</b>
- <ul>
- <li>Time<br />
- TEK603 internal Time</li>
- <li>Temperature<br />
- Sensor Temperature</li>
- <li>Ullage<br />
- Sensor Measured Ullage</li>
- <li>RemainingUsableLevel<br />
- This is the usable level, with deductions due to the sensor offset and outlet height. (Liters)</li>
- <li>RemainingUsablePercent<br />
- This is the usable level in percent (calculated from RemainingUsableLevel and TotalUsableCapacity)</li>
- <li>TotalUsableCapacity<br />
- This is the usable volume, with deductions due to the sensor offset and outlet height. (Liters)</li>
- </ul><br />
- </ul><br />
- =end html
- =cut
|