From dc066883cb246e6a2357ea69c08d9af5dd9d8716 Mon Sep 17 00:00:00 2001 From: hcspad <> Date: Mon, 9 Jun 2014 12:21:58 +0000 Subject: [PATCH] Added the new module 36_EMT7110.pm git-svn-id: https://svn.fhem.de/fhem/trunk@6093 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/36_EMT7110.pm | 305 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 306 insertions(+) create mode 100644 fhem/FHEM/36_EMT7110.pm diff --git a/fhem/CHANGED b/fhem/CHANGED index a989bbf8d..5e6fb6030 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - feature: 36_EMT7110: added this new module - feature: 36_JeeLink: added initCommands attribute and flash command (by HCS) - feature: SYSMON: DECT Temperatur - bugfix: SYSMON: prevent some warnings diff --git a/fhem/FHEM/36_EMT7110.pm b/fhem/FHEM/36_EMT7110.pm new file mode 100644 index 000000000..ecf8c2b39 --- /dev/null +++ b/fhem/FHEM/36_EMT7110.pm @@ -0,0 +1,305 @@ + +# $Id$ +# +# TODO: + +package main; + +use strict; +use warnings; +use SetExtensions; + +sub EMT7110_Parse($$); + +sub +EMT7110_Initialize($) +{ + my ($hash) = @_; + + $hash->{Match} = "^OK\\sEMT7110\\s"; + $hash->{SetFn} = "EMT7110_Set"; + #$hash->{GetFn} = "EMT7110_Get"; + $hash->{DefFn} = "EMT7110_Define"; + $hash->{UndefFn} = "EMT7110_Undef"; + $hash->{FingerprintFn} = "EMT7110_Fingerprint"; + $hash->{ParseFn} = "EMT7110_Parse"; + $hash->{AttrFn} = "EMT7110_Attr"; + $hash->{AttrList} = "IODev". + " accumulatedPowerOffset". + " pricePerKWH". + " $readingFnAttributes"; +} + +sub +EMT7110_Define($$) +{ + my ($hash, $def) = @_; + my @a = split("[ \t][ \t]*", $def); + + if(@a != 3 ) { + my $msg = "wrong syntax: define EMT7110 "; + Log3 undef, 2, $msg; + return $msg; + } + + $a[2] =~ m/(\d|[abcdef]|[ABCDEF]){4}/i; + return "$a[2] is not a valid EMT7110 address" if( !defined($1) ); + + my $name = $a[0]; + my $addr = $a[2]; + + + return "EMT7110 device $addr already used for $modules{EMT7110}{defptr}{$addr}->{NAME}." if( $modules{EMT7110}{defptr}{$addr} + && $modules{EMT7110}{defptr}{$addr}->{NAME} ne $name ); + + $hash->{addr} = $addr; + + $modules{EMT7110}{defptr}{$addr} = $hash; + + AssignIoPort($hash); + if(defined($hash->{IODev}->{NAME})) { + Log3 $name, 3, "$name: I/O device is " . $hash->{IODev}->{NAME}; + } else { + Log3 $name, 1, "$name: no I/O device"; + } + + return undef; +} + +##################################### +sub +EMT7110_Undef($$) +{ + my ($hash, $arg) = @_; + my $name = $hash->{NAME}; + my $addr = $hash->{addr}; + + delete( $modules{EMT7110}{defptr}{$addr} ); + + return undef; +} + +##################################### +sub +EMT7110_Get($@) +{ + my ($hash, $name, $cmd, @args) = @_; + + return "\"get $name\" needs at least one parameter" if(@_ < 3); + + my $list = ""; + + return "Unknown argument $cmd, choose one of $list"; +} + +sub +EMT7110_Fingerprint($$) +{ + my ($name, $msg) = @_; + + return ( "", $msg ); +} + +# // Format +# // +# // OK EMT7110 84 81 8 237 0 13 0 2 1 6 1 -> ID 5451 228,5V 13mA 2W 2,62kWh +# // OK EMT7110 84 81 8 247 1 12 0 56 1 13 1 -> ID 5451 229,5V 268mA 56W 2,69kWh +# // OK EMT7110 ID ID VV VV AA AA WW WW KW KW Flags +# // | | | | | | | | | | | `--- Bit0: Connected Bit1: Pairing +# // | | | | | | | | | | `--- AccumulatedPower * 100 LSB +# // | | | | | | | | | `------ AccumulatedPower * 100 MSB +# // | | | | | | | | `--- Power (W) LSB +# // | | | | | | | `------ Power (W) MSB +# // | | | | | | `--- Current (mA) LSB +# // | | | | | `------ Current (mA) MSB +# // | | | | `--- Voltage (V) * 10 LSB +# // | | | `----- Voltage (V) * 10 MSB +# // | | `--- ID +# // | `------- ID +# // `--- fix "EMT7110" +sub +EMT7110_Parse($$) +{ + my ($hash, $msg) = @_; + my $name = $hash->{NAME}; + + my( @bytes, $addr,$voltage,$current,$power,$accumulatedPower,$accumulatedPowerMeasured,$connected,$pairing ); + if( $msg =~ m/^OK EMT7110/ ) { + @bytes = split( ' ', substr($msg, 11) ); + + $addr = sprintf( "%02X%02X", $bytes[0], $bytes[1] ); + $voltage = ($bytes[2]*256 + $bytes[3] ) / 10.0; + $current = $bytes[4]*256 + $bytes[5]; + $power = $bytes[6]*256 + $bytes[7]; + $accumulatedPowerMeasured = ($bytes[8]*256 + $bytes[9]) / 100.0; + $connected = ($bytes[10] & 0x01); + $pairing = ($bytes[10] & 0x02) >> 1; + + } else { + DoTrigger($name, "UNKNOWNCODE $msg"); + Log3 $name, 3, "$name: Unknown code $msg, help me!"; + return undef; + } + + if($pairing > 0) { + return undef; + } + else { + my $raddr = $addr; + my $rhash = $modules{EMT7110}{defptr}{$raddr}; + my $rname = $rhash?$rhash->{NAME}:$raddr; + + my $accumulatedPowerOffset = AttrVal( $rname, "accumulatedPowerOffset", 0); + $accumulatedPower = $accumulatedPowerMeasured - $accumulatedPowerOffset; + + my $costs = $accumulatedPower * AttrVal( $rname, "pricePerKWH", 0); + + if( !$modules{EMT7110}{defptr}{$raddr} ) { + Log3 $name, 3, "EMT7110 Unknown device $rname, please define it"; + + return "UNDEFINED EMT7110_$rname EMT7110 $raddr"; + } + + my @list; + push(@list, $rname); + + $rhash->{lastReceiveTime} = TimeNow(); + + readingsBeginUpdate($rhash); + readingsBulkUpdate($rhash, "voltage", $voltage); + readingsBulkUpdate($rhash, "current", $current); + readingsBulkUpdate($rhash, "power", $power); + readingsBulkUpdate($rhash, "accumulatedPowerMeasured", $accumulatedPowerMeasured); + readingsBulkUpdate($rhash, "accumulatedPower", $accumulatedPower); + readingsBulkUpdate($rhash, "costs", $costs); + + + my $state = "V: $voltage"; + $state .= " C: $current"; + $state .= " P: $power"; + $state .= " A: $accumulatedPower"; + + readingsBulkUpdate($rhash, "state", $state) if( Value($rname) ne $state ); + readingsEndUpdate($rhash,1); + + return @list; + } + +} + +##################################### +sub +EMT7110_Set($@) +{ + my ($hash, @a) = @_; + + my $name = shift @a; + my $cmd = shift @a; + my $arg = join(" ", @a); + + + my $list = "resetAccumulatedPower"; + return $list if( $cmd eq '?' || $cmd eq ''); + + + if($cmd eq "resetAccumulatedPower") { + CommandAttr(undef, "$name accumulatedPowerOffset " . $hash->{READINGS}{accumulatedPowerMeasured}{VAL}); + } + else { + return "Unknown argument $cmd, choose one of ".$list; + } + + return undef; +} + +sub +EMT7110_Attr(@) +{ + my ($cmd, $name, $attrName, $attrVal) = @_; + + return undef; +} + +1; + +=pod +=begin html + + +

EMT7110

+ + +=end html +=cut