2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-10 01:55:38 +00:00

git-svn-id: https://svn.fhem.de/fhem/trunk@1740 2b470e98-0d58-463d-a4d8-8e2adae1ed80

This commit is contained in:
pahenning 2012-07-19 11:36:08 +00:00
parent 814beddc1d
commit f40c030c63
7 changed files with 163 additions and 79 deletions

View File

@ -9,7 +9,7 @@
# Internally these interfaces are vastly different, read the corresponding Wiki pages
# http://fhemwiki.de/wiki/Interfaces_f%C3%BCr_1-Wire
#
# Version 2.11 - July, 2012
# Version 2.13 - July, 2012
#
# Prof. Dr. Peter A. Henning, 2012
#
@ -595,8 +595,11 @@ sub OWX_Discover ($) {
my $name = sprintf "OWX_%s_%s",$owx_f,$owx_rnf;
#-- Family 10 = Temperature sensor, assume DS1820 as default
if( $owx_f eq "10" ){
CommandDefine(undef,"$name OWTHERM DS1820 $owx_rnf");
#-- Family 1D = Counter/RAM, assume DS2423 as default
CommandDefine(undef,"$name OWTHERM DS1820 $owx_rnf");
#-- Family 12 = Switch, assume DS2406 as default
}elsif( $owx_f eq "12" ){
CommandDefine(undef,"$name OWSWITCH DS2406 $owx_rnf");
#-- Family 1D = Counter/RAM, assume DS2423 as default
}elsif( $owx_f eq "1D" ){
CommandDefine(undef,"$name OWCOUNT DS2423 $owx_rnf");
#-- Family 20 = A/D converter, assume DS2450 as default
@ -608,7 +611,7 @@ sub OWX_Discover ($) {
#-- Family 28 = Temperature sensor, assume DS18B20 as default
}elsif( $owx_f eq "28" ){
CommandDefine(undef,"$name OWTHERM DS18B20 $owx_rnf");
#-- Family 3A = Switch, assume DS2413 as default
#-- Family 3A = Switch, assume DS2413 as default
}elsif( $owx_f eq "3A" ){
CommandDefine(undef,"$name OWSWITCH DS2413 $owx_rnf");
#-- Family FF = LCD display

View File

@ -14,7 +14,7 @@
#
# Prof. Dr. Peter A. Henning, 2012
#
# Version 2.11 - July, 2012
# Version 2.13 - July, 2012
#
# Setup bus device in fhem.cfg as
#

View File

@ -14,7 +14,7 @@
#
# Prof. Dr. Peter A. Henning, 2012
#
# Version 2.11 - July, 2012
# Version 2.13 - July, 2012
#
# Setup bus device in fhem.cfg as
#

View File

@ -12,7 +12,7 @@
#
# Prof. Dr. Peter A. Henning, 2012
#
# Version 2.11 - July, 2012
# Version 2.13 - July, 2012
#
# Setup bus device in fhem.cfg as
#

View File

@ -12,7 +12,7 @@
#
# Prof. Dr. Peter A. Henning, 2012
#
# Version 2.1 - July, 2012
# Version 2.13 - July, 2012
#
# Setup bus device in fhem.cfg as
#

View File

@ -2,7 +2,7 @@
#
# OWSWITCH.pm
#
# FHEM module to commmunicate with 1-Wire adressable switches DS2413
# FHEM module to commmunicate with 1-Wire adressable switches DS2413, DS206
#
# Attention: This module may communicate with the OWX module,
# but currently not with the 1-Wire File System OWFS
@ -17,7 +17,7 @@
#
# Prof. Dr. Peter A. Henning, 2012
#
# Version 2.11 - July, 2012
# Version 2.13 - July, 2012
#
# Setup bus device in fhem.cfg as
#
@ -26,7 +26,7 @@
# where <name> may be replaced by any name string
#
# <model> is a 1-Wire device type. If omitted, we assume this to be an
# DS2413
# DS2413. Allowed values are DS2413, DS2406
# <ROM_ID> is a 12 character (6 byte) 1-Wire ROM ID
# without Family ID, e.g. A2D90D000800
# [interval] is an optional query interval in seconds
@ -35,10 +35,15 @@
# get <name> present => 1 if device present, 0 if not
# get <name> interval => query interval
# get <name> input <channel-name> => state for channel (name A, B or defined channel name)
# note: this value reflects the measured value, not necessarily the one set as
# output state, because the output transistors are open collector switches. A measured
# state of 1 = OFF therefore corresponds to an output state of 1 = OFF, but a measured
# state of 0 = ON can also be due to an external shortening of th eoutput.
# get <name> gpio => values for channels
#
# set <name> interval => set period for measurement
# set <name> output <channel-name> ON|OFF => set value for channel (name A, B or defined channel name)
# note: 1 = OFF, 0 = ON in normal usage. See also th enote above
# set <name> gpio value => set values for channels (3 = both OFF, 1 = B ON 2 = A ON 0 = both ON)
#
# Additional attributes are defined in fhem.cfg, in some cases per channel, where <channel>=A,B
@ -46,6 +51,7 @@
#
# attr <name> <channel>Name <string>|<string> = name for the channel | a type description for the measured value
# attr <name> <channel>Unit <string>|<string> = values to display in state variable for on|off condition
# attr <name> <channel>stateS <string> = character string denoting external shortening condition
#
########################################################################################
#
@ -76,8 +82,9 @@ sub Log($$);
#-- channel name - fixed is the first array, variable the second
my @owg_fixed = ("A","B");
my @owg_channel;
#-- channel values - always the raw values from the device
#-- channel values - always the raw input resp. output values from the device
my @owg_val;
my @owg_vax;
my %gets = (
"id" => "",
@ -124,11 +131,12 @@ sub OWSWITCH_Initialize ($) {
#Offset = an offset added to the reading
#Factor = a factor multiplied with (reading+offset)
#Unit = a unit of measure
my $attlist = "IODev do_not_notify:0,1 showtime:0,1 model:DS2413 loglevel:0,1,2,3,4,5 ";
my $attlist = "IODev do_not_notify:0,1 showtime:0,1 model:DS2413,DS2406 loglevel:0,1,2,3,4,5 ";
for( my $i=0;$i<int(@owg_fixed);$i++ ){
$attlist .= " ".$owg_fixed[$i]."Name";
$attlist .= " ".$owg_fixed[$i]."Unit";
$attlist .= " ".$owg_fixed[$i]."stateS";
}
$hash->{AttrList} = $attlist;
}
@ -169,21 +177,30 @@ sub OWSWITCH_Define ($$) {
if(int(@a)>=4) { $interval = $a[3]; }
} elsif( $a3 =~ m/^[0-9|a-f|A-F]{12}$/ ) {
$model = $a[2];
return "OWSWITCH: Wrong 1-Wire device model $model"
if( $model ne "DS2413");
$id = $a[3];
if(int(@a)>=5) { $interval = $a[4]; }
} else {
return "OWSWITCH: $a[0] ID $a[2] invalid, specify a 12 digit value";
}
#-- 1-Wire ROM identifier in the form "FF.XXXXXXXXXXXX.YY"
# FF = family id follows from the model
# YY must be determined from id
if( $model eq "DS2413" ){
$fam = "3A";
}elsif( $model eq "DS2406" ){
$fam = "12";
}else{
return "OWSWITCH: Wrong 1-Wire device model $model";
}
#-- 1-Wire ROM identifier in the form "FF.XXXXXXXXXXXX.YY"
# determine CRC Code - only if this is a direct interface
$crc = defined($hash->{IODev}->{INTERFACE}) ? sprintf("%02x",OWX_CRC("3A.".$id."00")) : "00";
$crc = defined($hash->{IODev}->{INTERFACE}) ? sprintf("%02x",OWX_CRC($fam.".".$id."00")) : "00";
#-- Define device internals
$hash->{ROM_ID} = "3A.".$id.$crc;
$hash->{ROM_ID} = $fam.".".$id.$crc;
$hash->{OW_ID} = $id;
$hash->{OW_FAMILY} = "3A";
$hash->{OW_FAMILY} = $fam;
$hash->{PRESENT} = 0;
$hash->{INTERVAL} = $interval;
@ -219,12 +236,12 @@ sub OWSWITCH_InitializeDevice($) {
my ($hash) = @_;
my $name = $hash->{NAME};
#-- Initial readings
@owg_val = (0.0,0.0,0.0,0.0);
#-- Set channel names, channel units
for( my $i=0;$i<int(@owg_fixed);$i++) {
#-- Initial readings OFF
$owg_val[$i] = 1;
$owg_vax[$i] = 1;
#-- name
my $cname = defined($attr{$name}{$owg_fixed[$i]."Name"}) ? $attr{$name}{$owg_fixed[$i]."Name"} : $owg_fixed[$i]."|onoff";
my @cnama = split(/\|/,$cname);
@ -279,28 +296,35 @@ sub OWSWITCH_FormatValues($) {
my ($hash) = @_;
my $name = $hash->{NAME};
my ($offset,$factor,$vval);
my ($offset,$factor,$vval,$vvax,$vstr,$cname,@cnama,@unarr);
my ($value1,$value2,$value3) = ("","","");
my $tn = TimeNow();
#-- formats for output
for (my $i=0;$i<int(@owg_fixed);$i++){
my $cname = defined($attr{$name}{$owg_fixed[$i]."Name"}) ? $attr{$name}{$owg_fixed[$i]."Name"} : $owg_fixed[$i];
my @cnama = split(/\|/,$cname);
$cname = defined($attr{$name}{$owg_fixed[$i]."Name"}) ? $attr{$name}{$owg_fixed[$i]."Name"} : $owg_fixed[$i];
@cnama = split(/\|/,$cname);
$owg_channel[$i]=$cnama[0];
#-- result is zero or one
#-- input state is 0 = ON or 1 = OFF
$vval = $owg_val[$i];
#-- output state is 0 = ON or 1 = OFF
$vvax = $owg_vax[$i];
#-- string buildup for return value and STATE
my @unarr=split(/\|/,$hash->{READINGS}{"$owg_channel[$i]"}{UNIT});
$value1 .= sprintf( "%s: %s", $owg_channel[$i], $unarr[$vval]);
$value2 .= sprintf( "%s: %s ", $owg_channel[$i], $unarr[$vval]);
@unarr= split(/\|/,$hash->{READINGS}{"$owg_channel[$i]"}{UNIT});
$cname = defined($attr{$name}{$owg_fixed[$i]."stateS"}) ? $attr{$name}{$owg_fixed[$i]."stateS"} : "<span style=\"color:red\">&#x2607;</span>";
$vstr = $unarr[$vval];
$vstr .= $cname if( ($vval == 0) && ($vvax == 1) );
$vstr = "ERR" if( ($vval == 1) && ($vvax == 0) );
$value1 .= sprintf( "%s: %s", $owg_channel[$i], $vstr);
$value2 .= sprintf( "%s: %s ", $owg_channel[$i], $vstr);
$value3 .= sprintf( "%s: " , $owg_channel[$i]);
#-- put into READINGS
$hash->{READINGS}{"$owg_channel[$i]"}{VAL} = $unarr[$vval];
$hash->{READINGS}{"$owg_channel[$i]"}{VAL} = $vstr;
$hash->{READINGS}{"$owg_channel[$i]"}{TIME} = $tn;
#-- insert comma
@ -457,7 +481,7 @@ sub OWSWITCH_GetValues($) {
if( $interface eq "OWX" ){
$ret = OWXSWITCH_GetState($hash);
#}elsif( $interface eq "OWFS" ){
# $ret = OWFSAD_GetValues($hash);
# $ret = OWFSSWITCH_GetValues($hash);
}else{
return "OWSWITCH: GetValues with wrong IODev type $interface";
}
@ -546,7 +570,7 @@ sub OWSWITCH_Set($@) {
}elsif( lc($a[3]) eq "off" ){
$nval = 1;
}else{
return "OWSWITCH: Wrong data value $a[3]";
return "OWSWITCH: Wrong data value $a[3], must be ON or OFF";
}
#-- OWX interface
@ -645,48 +669,66 @@ sub OWXSWITCH_GetState($) {
my $master = $hash->{IODev};
my ($i,$j,$k);
#-- 8 byte 1-Wire device address
my @owx_ROM_ID =(0,0,0,0 ,0,0,0,0);
#-- from search string to byte id
my $devs=$owx_dev;
$devs=~s/\.//g;
for($i=0;$i<8;$i++){
$owx_ROM_ID[$i]=hex(substr($devs,2*$i,2));
#-- family = 3A => DS2413
if( $hash->{OW_FAMILY} eq "3A" ) {
#=============== get gpio values ===============================
#-- issue the match ROM command \x55 and the read gpio command
# \xF5 plus 2 empty bytes
#-- reset the bus
OWX_Reset($master);
#-- read the data
$res=OWX_Complex($master,$owx_dev,"\xF5",2);
if( $res eq 0 ){
return "OWXSWITCH: Device $owx_dev not accessible in reading";
}
#-- family = 12 => DS2406
}elsif( $hash->{OW_FAMILY} eq "12" ) {
#=============== set gpio values ===============================
#-- issue the match ROM command \x55 and the access channel command
# \xF5 plus the two byte channel control and the value
$select=sprintf("\xF5\xDC\xFF");
#-- reset the bus
OWX_Reset($master);
#-- read the data
$res=OWX_Complex($master,$owx_dev,$select,1);
if( $res eq 0 ){
return "OWXSWITCH: Device $owx_dev not accessible in writing";
}
} else {
return "OWXSWITCH: Unknown device family $hash->{OW_FAMILY}\n";
}
#=============== get gpio values ===============================
#-- issue the match ROM command \x55 and the read gpio command
# \xF5 plus 2 empty bytes
#-- reset the bus
OWX_Reset($master);
#-- read the data
$res=OWX_Complex($master,$owx_dev,"\xF5",2);
if( $res eq 0 ){
return "OWX: Device $owx_dev not accessible in reading";
}
#-- process results
# TODO: could we put in a check if the two bytes are equal
@data=split(//,substr($res,10));
#-- reset the bus
OWX_Reset($master);
#my $ress = "OWXSWITCH_Get: three data bytes ";
# for($i=0;$i<3;$i++){
# for($i=0;$i<int(@data);$i++){
# my $j=int(ord($data[$i])/16);
# my $k=ord($data[$i])%16;
# $ress.=sprintf "0x%1x%1x ",$j,$k;
# }
#Log 1, $ress;
#-- This holds only for DS2413
$owg_val[0] = ord($data[0]) % 2;
$owg_val[1] = (ord($data[0])>>2) % 2;
#Log 1, "Values are ".ord($data[0])." $owg_val[0] and $owg_val[1]";
# note: value 1 corresponds to OFF, 0 to ON normally
# note: we display the sensed output level, not the flipflop setting
#-- family = 3A => DS2413
if( $hash->{OW_FAMILY} eq "3A" ) {
$owg_val[0] = ord($data[0]) & 1;
$owg_vax[0] = (ord($data[0])>>1) & 1;
$owg_val[1] = (ord($data[0])>>2) & 1;
$owg_vax[1] = (ord($data[0])>>3) & 1;
#-- family = 12 => DS2406
}elsif( $hash->{OW_FAMILY} eq "12" ) {
$owg_val[0] = (ord($data[2])>>2) & 1;
$owg_vax[0] = ord($data[2]) & 1;
$owg_val[1] = (ord($data[2])>>3) & 1;
$owg_vax[1] = (ord($data[2])>>1) & 1;
}
return undef
}
@ -716,18 +758,47 @@ sub OWXSWITCH_SetState($$) {
my ($i,$j,$k);
#=============== set gpio values ===============================
#-- issue the match ROM command \x55 and the write gpio command
# \x5A plus the value byte and its complement
$select=sprintf("\x5A%c%c",252+$value,3-$value);
#-- reset the bus
OWX_Reset($master);
#-- read the data
$res=OWX_Complex($master,$owx_dev,$select,1);
if( $res eq 0 ){
return "OWX: Device $owx_dev not accessible in reading";
#-- family = 3A => DS2413
if( $hash->{OW_FAMILY} eq "3A" ) {
#=============== set gpio values ===============================
#-- issue the match ROM command \x55 and the write gpio command
# \x5A plus the value byte and its complement
$select=sprintf("\x5A%c%c",252+$value,3-$value);
#-- reset the bus
OWX_Reset($master);
#-- read the data
$res=OWX_Complex($master,$owx_dev,$select,1);
if( $res eq 0 ){
return "OWXSWITCH: Device $owx_dev not accessible in writing";
}
#-- family = 12 => DS2406
}elsif( $hash->{OW_FAMILY} eq "12" ) {
#=============== set gpio values ===============================
# Wrriting the output state via the access channel command does
# not work contrary to documentation. Using the write status command
#-- issue the match ROM command \x55 and the read status command
# \xAA at address TA1 = \x07 TA2 = \x00
#-- reset the bus
OWX_Reset($master);
#-- read the data
$res = OWX_Complex($master,$owx_dev,"\xAA\x07\x00",1);
my $stat = substr($res,10,1);
my $statneu = ( $stat & 159 ) | (($value<<5) & 96) ;
#-- issue the match ROM command \x55 and the write status command
# \x55 at address TA1 = \x07 TA2 = \x00
#
$select=sprintf("\x55\x07\x00%c",$statneu);
#-- reset the bus
OWX_Reset($master);
#-- read the data
$res=OWX_Complex($master,$owx_dev,$select,2);
if( $res eq 0 ){
return "OWXSWITCH: Device $owx_dev not accessible in writing";
}
} else {
return "OWXSWITCH: Unknown device family $hash->{OW_FAMILY}\n";
}
#-- reset the bus
OWX_Reset($master);
@ -735,23 +806,33 @@ sub OWXSWITCH_SetState($$) {
@data=split(//,substr($res,10));
#my $ress = "OWXSWITCH_Set: three data bytes ";
# for($i=0;$i<3;$i++){
# for($i=0;$i<int(@data);$i++){
# my $j=int(ord($data[$i])/16);
# my $k=ord($data[$i])%16;
# $ress.=sprintf "0x%1x%1x ",$j,$k;
# }
#Log 1, $ress;
if( $data[2] ne "\xAA"){
Log 1, "OWXSWITCH: State could not be set";
return "OWXSWITCH: State could not be set";
}
#-- family = 3A => DS2413
if( $hash->{OW_FAMILY} eq "3A" ) {
if( $data[2] ne "\xAA"){
return "OWXSWITCH: State could not be set for device $owx_dev";
}
#-- family = 12 => DS2406
}elsif( $hash->{OW_FAMILY} eq "12" ) {
#-- very crude check - should be CRC
if( int(@data) != 5){
return "OWXSWITCH: State could not be set for device $owx_dev";
}
}
#-- Put the new values in the system variables
#-- This holds only for DS2413
$owg_val[0] = $value % 2;
$owg_vax[0] = $owg_val[0];
$owg_val[1] = int($value / 2);
$owg_vax[1] = $owg_val[1];
return undef
}

View File

@ -15,7 +15,7 @@
# Prof. Dr. Peter A. Henning, 2012
# Martin Fischer, 2011
#
# Version 2.12 - July, 2012
# Version 2.13 - July, 2012
#
# Setup bus device in fhem.cfg as
#
@ -25,7 +25,7 @@
#
# <model> is a 1-Wire device type. If omitted, we assume this to be an
# DS1820 temperature sensor
# Currently allowed values are DS1820, DS1822
# Currently allowed values are DS1820, DS18B20, DS1822
# <ROM_ID> is a 12 character (6 byte) 1-Wire ROM ID
# without Family ID, e.g. A2D90D000800
# [interval] is an optional query interval in seconds