From 0cc81ab348c3514c4ec87664c38d4f8a529373e8 Mon Sep 17 00:00:00 2001 From: yoda_gh Date: Mon, 7 May 2018 20:58:27 +0000 Subject: [PATCH] 52_I2C_HDC1008.pm: improve and restructure I2C error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The code used to ignore I2C write errors reported by the IODev (e.g. when triggering a measurement) and just continued. While the subsequent read should fail if sensor doesn't have values, it sometimes didn't for me, leading to sporadic reading of 125°C and/or 100% rH. In case of write errors, just retry current step until it succeeds. git-svn-id: https://svn.fhem.de/fhem/trunk@16703 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/52_I2C_HDC1008.pm | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index b5b04a511..48df38916 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. + - change: 52_I2C_HDC1008: improve and restructure I2C error handling - bugfix: 57_Calendar: fix count for series with exceptions - bugfix: 72_FB_CALLMONITOR: fix control code entries in phonebook and reverse search text file diff --git a/fhem/FHEM/52_I2C_HDC1008.pm b/fhem/FHEM/52_I2C_HDC1008.pm index 2d618c760..db95067d5 100644 --- a/fhem/FHEM/52_I2C_HDC1008.pm +++ b/fhem/FHEM/52_I2C_HDC1008.pm @@ -163,12 +163,24 @@ sub I2C_HDC1008_I2CRec ($$) { my $upper_k = uc $k; $hash->{$upper_k} = $v if $k =~ /^$pname/ ; } - if ($clientmsg->{direction} && $clientmsg->{type} && $clientmsg->{$pname . "_SENDSTAT"} && $clientmsg->{$pname . "_SENDSTAT"} eq "Ok") { - if ( $clientmsg->{direction} eq "i2cread" && defined($clientmsg->{received}) ) - { - Log3 $hash, 5, "[$name] I2C_HDC1008_I2CRec received: $clientmsg->{type} $clientmsg->{received}"; - I2C_HDC1008_GetTemp ($hash, $clientmsg->{received}) if $clientmsg->{type} eq "temp" && $clientmsg->{nbyte} == 2; - I2C_HDC1008_GetHum ($hash, $clientmsg->{received}) if $clientmsg->{type} eq "hum" && $clientmsg->{nbyte} == 2; + if ($clientmsg->{direction} && $clientmsg->{$pname . "_SENDSTAT"}) { + my $sendstat = $clientmsg->{$pname . "_SENDSTAT"}; + Log3 $hash, 5, "[$name] I2C_HDC1008_I2CRec $clientmsg->{direction} $sendstat "; + if ( $clientmsg->{$pname . "_SENDSTAT"} eq "Ok") { + if ( $clientmsg->{direction} eq "i2cwrite" ) { + if ($hash->{DEVICE_STATE} eq 'READY') { + $hash->{DEVICE_STATE} = 'CONFIGURING'; + } elsif($hash->{DEVICE_STATE} eq 'CONFIGURING') { + $hash->{DEVICE_STATE} = 'MEASURING'; + } + } + + if ( $clientmsg->{direction} eq "i2cread" && defined($clientmsg->{received}) ) + { + Log3 $hash, 5, "[$name] I2C_HDC1008_I2CRec received: $clientmsg->{type} $clientmsg->{received}"; + I2C_HDC1008_GetTemp ($hash, $clientmsg->{received}) if $clientmsg->{nbyte} == 4; + I2C_HDC1008_GetHum ($hash, $clientmsg->{received}) if $clientmsg->{nbyte} == 4; + } } } } @@ -315,7 +327,6 @@ sub I2C_HDC1008_UpdateValues($) reg => 2, data => $high_byte. " ".$low_byte # Leider fehlt es hier an Doku. Laut Quellcode (00_RPII2C.pm, ab Zeile 369), werden die dezimale Zahlen durch Leerzeichen getrennt, binär gewandelt und zum I2C-Bus geschickt }); - $hash->{DEVICE_STATE} = 'CONFIGURING'; return 15.0/1000; # Sensor braucht bis 15 ms bis er bereit ist } elsif($hash->{DEVICE_STATE} eq 'CONFIGURING') @@ -329,8 +340,6 @@ sub I2C_HDC1008_UpdateValues($) data => (0) }); - $hash->{DEVICE_STATE} = 'MEASURING'; - my $tempWait = $I2C_HDC1008_tempParams{$resTempIndex}{delay} + $I2C_HDC1008_humParams{$resTempIndex}{delay}; # in ns return $tempWait/1000000.0;