2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-13 05:06:35 +00:00

Bugfix: negative temp and internal re-design

git-svn-id: https://svn.fhem.de/fhem/trunk@2400 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
fladdy 2013-01-01 10:04:10 +00:00
parent a4040bf0b2
commit a9261a9866

View File

@ -1,18 +1,27 @@
#############################################################################
# GPIO4.pm written by Peter J. Flathmann #
# Version 0.3, 2012-12-16 #
# #
# define RPi GPIO4 BUSMASTER #
# #
# All devices should be automatically created with model information: #
# =================================================================== #
# define mysSensor GPIO4 28-000004715a10 #
# attr mySensor model DS18B20 #
# #
# Optional attributes: #
# ==================== #
# attr mySensor pollingInterval 60 (default: 60s) #
# #
#
# $Id$
#
# Copyright notice
#
# (c) 2012 Copyright: Peter J. Flathmann (peter dot flathmann at web dot de)
# All rights reserved
#
# This script 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.
#
# The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html.
# A copy is found in the textfile GPL.txt and important notices to the license
# from the author is found in LICENSE.txt distributed with these scripts.
#
# This script 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.
#
#############################################################################
package main;
@ -27,10 +36,20 @@ sub GPIO4_Initialize($) {
$hash->{DefFn} = "GPIO4_Define";
$hash->{UndefFn} = "GPIO4_Undef";
$hash->{NotifyFn} = "GPIO4_Notify";
$hash->{GetFn} = "GPIO4_Get";
$hash->{AttrList} = "tempOffset pollingInterval model loglevel:0,1,2,3,4,5,6";
}
sub GPIO4_Notify($$) {
my ($hash, $dev) = @_;
return if($hash->{DEF} ne "BUSMASTER" || $dev->{NAME} ne "global" || !grep(m/^INITIALIZED$/, @{$dev->{CHANGED}}));
# check bus for new devices not before fhem.cfg completely loaded to avoid duplicates
GPIO4_GetSlaves($hash);
delete $modules{GPIO4}{NotifyFn};
return;
}
sub GPIO4_Define($$) {
my ($hash, $def) = @_;
@ -39,23 +58,18 @@ sub GPIO4_Define($$) {
my @a = split("[ \t][ \t]*", $def);
return "syntax: define <name> GPIO4 <id>|BUSMASTER" if (int(@a) != 3);
$hash->{NAME} = $a[0];
$hash->{TYPE} = $a[1];
$hash->{TYPE} = 'GPIO4';
if ($a[2] eq "BUSMASTER") {
$hash->{STATE} = "Initialized";
# check connected devices after fhem.cfg completely loaded to avoid duplicates (5s)
InternalTimer(gettimeofday()+5, "GPIO4_GetSlaves", $hash, 0);
}
else {
my ($family, $id) = split('-',$a[2]);
if ($family eq "28" || $family eq "10") {
# reset failures counter
setReadingsVal($hash,'failures',0,TimeNow());
# start polling device after fhem.cfg completely loaded to ensure pollingInterval attribute is assigned (5s)
InternalTimer(gettimeofday()+5, "GPIO4_DeviceUpdateLoop", $hash, 0);
$hash->{fhem}{interfaces} = "temperature";
GPIO4_DeviceUpdateLoop($hash);
}
else {
return "GPIO4: device family $family not supported";
@ -88,35 +102,17 @@ sub GPIO_GetSlave($$) {
return if (exists $defs{$devicename}{DEF} && $defs{$devicename}{DEF} eq $slave);
}
# device does not exist, create it
Log 1, "GPIO4: create $slave";
CommandDefine(undef,"gpio4_$id GPIO4 $slave");
# device does not exist, let autocreate create it
my $model;
if ($family eq "28") {
$attr{"gpio4_$id"}{model} = "DS18B20";
$model = "DS18B20";
}
if ($family eq "10") {
$attr{"gpio4_$id"}{model} = "DS1820";
elsif ($family eq "10") {
$model = "DS1820";
}
$attr{"gpio4_$id"}{room} = "GPIO4";
$defs{"gpio4_$id"}{MASTER} = $hash->{NAME};
DoTrigger("global", "UNDEFINED GPIO4_${model}_${id} GPIO4 $slave");
$attr{"GPIO4_${model}_${id}"}{model} = $model;
# create logfile temp4:Temp
my @logfile = split('/',$attr{autocreate}{filelog});
pop(@logfile);
my $logdir = join('/',@logfile);
CommandDefine(undef,"FileLog_gpio4_$id FileLog $logdir/gpio4_$id-%Y.log gpio4_$id:T:.*");
$attr{"FileLog_gpio4_$id"}{room} = "GPIO4";
$attr{"FileLog_gpio4_$id"}{logtype} = "temp4:Temp,text";
# create plot
if ($attr{autocreate}{weblink}) {
CommandDefine(undef,"weblink_gpio4_$id weblink fileplot FileLog_gpio4_$id:temp4:CURRENT");
$attr{"weblink_gpio4_$id"}{label} = '"'."gpio4_$id: ".'Min $data{min1}, Max $data{max1}, Last $data{currval1}"';
$attr{"weblink_gpio4_$id"}{room} = $attr{autocreate}{weblink_room} || "GPIO4";
}
# save fhem.cfg depending on autocreate autosave
CommandSave(undef, undef) if($attr{autocreate}{autosave});
return;
}
@ -134,7 +130,7 @@ sub GPIO4_Get($) {
Log 6, "GPIO4: GPIO4_Get($hash->{NAME})";
open DATA, "/sys/bus/w1/devices/$hash->{DEF}/w1_slave";
if (<DATA> =~ /YES/) {
<DATA> =~ /t=(\d+)/;
<DATA> =~ /t=(-?\d+)/;
my $temp = $1/1000.0;
if ($attr{$hash->{NAME}}{tempOffset}) {
$temp+=$attr{$hash->{NAME}}{tempOffset};
@ -160,3 +156,74 @@ sub GPIO4_Undef($) {
}
1;
=pod
=begin html
<a name="GPIO4"></a>
<h3>GPIO4</h3>
<ul>
1-wire temperature sensors connected to Raspberry Pi's GPIO port 4.
<br><br>
<a name="GPIO4define"></a>
<b>Define</b>
<ul>
<code>define &lt;name&gt; GPIO4 ('BUSMASTER'|&lt;id&gt;)</code>
<br><br>
Defines the BUSMASTER or one of the slave devices. The BUSMASTER is necessary
to get the slaves automatically created by autocreate.pm.
<br><br>
Examples:
<ul>
<code>
define RPi GPIO4 BUSMASTER<br>
define mysSensor GPIO4 28-000004715a10<br>
</code>
</ul>
</ul>
<br>
<a name="GPIO4attr"></a>
<b>Attributes</b>
<ul>
<li><a href="#pollingInterval">pollingInterval</a></li>
<li><a href="#tempOffest">tempOffset</a></li>
</ul>
<br>
</ul>
=end html
=begin html_DE
<a name="GPIO4"></a>
<h3>GPIO4</h3>
<ul>
1-wire Temperatursensoren an GPIO Port 4 des Raspberry Pi.
<br><br>
<a name="GPIO4define"></a>
<b>Define</b>
<ul>
<code>define &lt;name&gt; GPIO4 ('BUSMASTER'|&lt;id&gt;)</code>
<br><br>
Definiert den BUSMASTER oder die Slave-Devices. Der BUSMASTER ist notwendig,
um die Slave-Devices automatisch via autocreate.pm zu erzeugen.
<br><br>
Examples:
<ul>
<code>
define RPi GPIO4 BUSMASTER<br>
define mysSensor GPIO4 28-000004715a10<br>
</code>
</ul>
</ul>
<br>
<a name="GPIO4attr"></a>
<b>Attributes</b>
<ul>
<li><a href="#pollingInterval">pollingInterval</a></li>
<li><a href="#tempOffest">tempOffset</a></li>
</ul>
<br>
</ul>
=end html_DE
=cut