21_OWMULTI.pm 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. ########################################################################################
  2. #
  3. # OWMULTI.pm
  4. #
  5. # FHEM module to commmunicate with 1-Wire chip DS2438Z - Smart Battery Monitor
  6. #
  7. # Prof. Dr. Peter A. Henning
  8. # Norbert Truchsess
  9. #
  10. # $Id: 21_OWMULTI.pm 16671 2018-04-29 05:06:35Z phenning $
  11. #
  12. ########################################################################################
  13. #
  14. # This programm is free software; you can redistribute it and/or modify
  15. # it under the terms of the GNU General Public License as published by
  16. # the Free Software Foundation; either version 2 of the License, or
  17. # (at your option) any later version.
  18. #
  19. # The GNU General Public License can be found at
  20. # http://www.gnu.org/copyleft/gpl.html.
  21. # A copy is found in the textfile GPL.txt and important notices to the license
  22. # from the author is found in LICENSE.txt distributed with these scripts.
  23. #
  24. # This script is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU General Public License for more details.
  28. #
  29. ########################################################################################
  30. package main;
  31. use vars qw{%attr %defs %modules $readingFnAttributes $init_done};
  32. use strict;
  33. use warnings;
  34. #add FHEM/lib to @INC if it's not already included. Should rather be in fhem.pl than here though...
  35. BEGIN {
  36. if (!grep(/FHEM\/lib$/,@INC)) {
  37. foreach my $inc (grep(/FHEM$/,@INC)) {
  38. push @INC,$inc."/lib";
  39. };
  40. };
  41. };
  42. use ProtoThreads;
  43. no warnings 'deprecated';
  44. sub Log($$);
  45. my $owx_version="7.02";
  46. #-- flexible channel name
  47. my ($owg_channel,$owg_schannel);
  48. my %gets = (
  49. "id" => ":noArg",
  50. "reading" => ":noArg",
  51. "temperature" => ":noArg",
  52. "VDD" => ":noArg",
  53. "raw" => ":noArg",
  54. "version" => ":noArg"
  55. );
  56. my %sets = (
  57. "interval" => "",
  58. );
  59. my %updates = (
  60. "present" => "",
  61. "reading" => "",
  62. );
  63. ########################################################################################
  64. #
  65. # The following subroutines are independent of the bus interface
  66. #
  67. # Prefix = OWMULTI
  68. #
  69. ########################################################################################
  70. #
  71. # OWMULTI_Initialize
  72. #
  73. # Parameter hash = hash of device addressed
  74. #
  75. ########################################################################################
  76. sub OWMULTI_Initialize ($) {
  77. my ($hash) = @_;
  78. $hash->{DefFn} = "OWMULTI_Define";
  79. $hash->{UndefFn} = "OWMULTI_Undef";
  80. $hash->{GetFn} = "OWMULTI_Get";
  81. $hash->{SetFn} = "OWMULTI_Set";
  82. $hash->{NotifyFn}= "OWMULTI_Notify";
  83. $hash->{InitFn} = "OWMULTI_Init";
  84. $hash->{AttrFn} = "OWMULTI_Attr";
  85. #tempOffset = a temperature offset added to the temperature reading for correction
  86. #tempUnit = a unit of measure: C/F/K
  87. $hash->{AttrList}= "IODev do_not_notify:0,1 showtime:0,1 model:DS2438 verbose:0,1,2,3,4,5 ".
  88. "tempOffset tempUnit:Celsius,Fahrenheit,Kelvin ".
  89. "VName VUnit VFunction WName WUnit WFunction ".
  90. "interval ".
  91. $readingFnAttributes;
  92. #-- temperature and voltage globals - always the raw values from the device
  93. $hash->{owg_val}->[0] = undef;
  94. $hash->{owg_val}->[1] = undef;
  95. $hash->{owg_val}->[2] = undef;
  96. $hash->{owg_val}->[3] = undef;
  97. #-- make sure OWX is loaded so OWX_CRC is available if running with OWServer
  98. main::LoadModule("OWX");
  99. }
  100. #######################################################################################
  101. #
  102. # OWMULTI_Attr - Set one attribute value for device
  103. #
  104. # Parameter hash = hash of device addressed
  105. # a = argument array
  106. #
  107. ########################################################################################
  108. sub OWMULTI_Attr(@) {
  109. my ($do,$name,$key,$value) = @_;
  110. my $hash = $defs{$name};
  111. my $ret;
  112. if ( $do eq "set") {
  113. ARGUMENT_HANDLER: {
  114. #-- interval modified at runtime
  115. $key eq "interval" and do {
  116. #-- check value
  117. return "OWMULTI: set $name interval must be >= 0" if(int($value) < 0);
  118. #-- update timer
  119. $hash->{INTERVAL} = int($value);
  120. if ($init_done) {
  121. RemoveInternalTimer($hash);
  122. InternalTimer(gettimeofday()+$hash->{INTERVAL}, "OWMULTI_GetValues", $hash, 0);
  123. }
  124. last;
  125. };
  126. $key eq "IODev" and do {
  127. AssignIoPort($hash,$value);
  128. if( defined($hash->{IODev}) ) {
  129. $hash->{ASYNC} = $hash->{IODev}->{TYPE} eq "OWX_ASYNC" ? 1 : 0;
  130. if ($init_done) {
  131. OWMULTI_Init($hash);
  132. }
  133. }
  134. last;
  135. }
  136. }
  137. }
  138. return $ret;
  139. }
  140. ########################################################################################
  141. #
  142. # OWMULTI_Define - Implements DefFn function
  143. #
  144. # Parameter hash = hash of device addressed, def = definition string
  145. #
  146. ########################################################################################
  147. sub OWMULTI_Define ($$) {
  148. my ($hash, $def) = @_;
  149. # define <name> OWMULTI [<model>] <id> [interval]
  150. # e.g.: define flow OWMULTI 525715020000 300
  151. my @a = split("[ \t][ \t]*", $def);
  152. my ($name,$model,$fam,$id,$crc,$interval,$ret);
  153. my $tn = TimeNow();
  154. #-- default
  155. $name = $a[0];
  156. $interval = 300;
  157. $ret = "";
  158. #-- check syntax
  159. return "OWMULTI: Wrong syntax, must be define <name> OWMULTI [<model>] <id> [interval]"
  160. if(int(@a) < 2 || int(@a) > 6);
  161. #-- different types of definition allowed
  162. my $a2 = $a[2];
  163. my $a3 = defined($a[3]) ? $a[3] : "";
  164. #-- no model, 12 characters
  165. if( $a2 =~ m/^[0-9|a-f|A-F]{12}$/ ) {
  166. $model = "DS2438";
  167. $fam = "26";
  168. $id = $a[2];
  169. if(int(@a)>=4) { $interval = $a[3]; }
  170. CommandAttr (undef,"$name model DS2438");
  171. #-- no model, 2+12 characters
  172. } elsif( $a2 =~ m/^[0-9|a-f|A-F]{2}\.[0-9|a-f|A-F]{12}$/ ) {
  173. $fam = substr($a[2],0,2);
  174. $id = substr($a[2],3);
  175. if(int(@a)>=4) { $interval = $a[3]; }
  176. if( $fam eq "26" ){
  177. $model = "DS2438";
  178. CommandAttr (undef,"$name model DS2438");
  179. }elsif( $fam eq "A6" ){
  180. $model = "DS2438a";
  181. CommandAttr (undef,"$name model DS2438a");
  182. }else{
  183. return "OWMULTI: Wrong 1-Wire device family $fam";
  184. }
  185. #-- model, 12 characters
  186. } elsif( $a3 =~ m/^[0-9|a-f|A-F]{12}$/ ) {
  187. $model = $a[2];
  188. $id = $a[3];
  189. if(int(@a)>=5) { $interval = $a[4]; }
  190. if( $model eq "DS2438" ){
  191. $fam = "26";
  192. CommandAttr (undef,"$name model DS2438");
  193. }elsif( $model eq "DS2438a" ){
  194. $fam = "A6";
  195. CommandAttr (undef,"$name model DS2438a");
  196. }else{
  197. return "OWMULTI: Wrong 1-Wire device model $model";
  198. }
  199. } else {
  200. return "OWMULTI: $a[0] ID $a[2] invalid, specify a 12 or 2.12 digit value";
  201. }
  202. #-- determine CRC Code - only if this is a direct interface
  203. $crc = sprintf("%02x",OWX_CRC($fam.".".$id."00"));
  204. #-- define device internals
  205. $hash->{OW_ID} = $id;
  206. $hash->{OW_FAMILY} = $fam;
  207. $hash->{PRESENT} = 0;
  208. $hash->{ERRCOUNT} = 0;
  209. $hash->{ROM_ID} = "$fam.$id.$crc";
  210. $hash->{INTERVAL} = $interval;
  211. #-- Couple to I/O device
  212. AssignIoPort($hash);
  213. if( !defined($hash->{IODev}) or !defined($hash->{IODev}->{NAME}) ){
  214. return "OWMULTI: Warning, no 1-Wire I/O device found for $name.";
  215. } else {
  216. $hash->{ASYNC} = $hash->{IODev}->{TYPE} eq "OWX_ASYNC" ? 1 : 0; #-- false for now
  217. }
  218. $main::modules{OWMULTI}{defptr}{$id} = $hash;
  219. #--
  220. readingsSingleUpdate($hash,"state","defined",1);
  221. Log 3, "OWMULTI: Device $name defined.";
  222. $hash->{NOTIFYDEV} = "global";
  223. if ($init_done) {
  224. OWMULTI_Init($hash);
  225. }
  226. return undef;
  227. }
  228. ########################################################################################
  229. #
  230. # OWMULTI_Notify - implements the Notify Function
  231. #
  232. # Parameter hash = hash of device addressed
  233. #
  234. ########################################################################################
  235. sub OWMULTI_Notify ($$) {
  236. my ($hash,$dev) = @_;
  237. if( grep(m/^(INITIALIZED|REREADCFG)$/, @{$dev->{CHANGED}}) ) {
  238. OWMULTI_Init($hash);
  239. } elsif( grep(m/^SAVE$/, @{$dev->{CHANGED}}) ) {
  240. }
  241. }
  242. ########################################################################################
  243. #
  244. # OWMULTI_Init - implements the Init function
  245. #
  246. # Parameter hash = hash of device addressed
  247. #
  248. ########################################################################################
  249. sub OWMULTI_Init ($) {
  250. my ($hash)=@_;
  251. #-- Start timer for updates
  252. RemoveInternalTimer($hash);
  253. InternalTimer(gettimeofday()+10, "OWMULTI_GetValues", $hash, 0);
  254. return undef;
  255. }
  256. ########################################################################################
  257. #
  258. # OWMULTI_ChannelNames - find the real channel names
  259. #
  260. # Parameter hash = hash of device addressed
  261. #
  262. ########################################################################################
  263. sub OWMULTI_ChannelNames($) {
  264. my ($hash) = @_;
  265. my $name = $hash->{NAME};
  266. my $state = $hash->{READINGS}{"state"}{VAL};
  267. my ($cname,@cnama,$unit);
  268. my ($tunit,$toffset,$tfactor,$tabbr,$vfunc,$wfunc);
  269. #-- Set channel name, channel unit for voltage channel
  270. $cname = defined($attr{$name}{"VName"}) ? $attr{$name}{"VName"} : "voltage|vad";
  271. @cnama = split(/\|/,$cname);
  272. if( int(@cnama)!=2){
  273. push(@cnama,$cnama[0]);
  274. }
  275. #-- unit
  276. $unit = defined($attr{$name}{"VUnit"}) ? $attr{$name}{"VUnit"} : "V";
  277. $unit = ""
  278. if($unit eq "none");
  279. #-- put into readings
  280. $owg_channel = $cnama[0];
  281. $hash->{READINGS}{$owg_channel}{VAL} = " ";
  282. $hash->{READINGS}{$owg_channel}{ABBR} = $cnama[1];
  283. $hash->{READINGS}{$owg_channel}{UNIT} = " ".$unit;
  284. #-- Set channel name, channel unit for sense channel
  285. $cname = defined($attr{$name}{"WName"}) ? $attr{$name}{"WName"} : "sense|s";
  286. @cnama = split(/\|/,$cname);
  287. if( int(@cnama)!=2){
  288. push(@cnama,$cnama[0]);
  289. }
  290. #-- unit
  291. $unit = defined($attr{$name}{"WUnit"}) ? $attr{$name}{"WUnit"} : "V";
  292. if($unit eq "none"){
  293. $unit = ""
  294. }else{
  295. $unit = " ".$unit
  296. }
  297. #-- put into readings
  298. $owg_schannel = $cnama[0];
  299. $hash->{READINGS}{$owg_schannel}{VAL} = " ";
  300. $hash->{READINGS}{$owg_schannel}{ABBR} = $cnama[1];
  301. $hash->{READINGS}{$owg_schannel}{UNIT} = $unit;
  302. #-- temperature scale
  303. $hash->{READINGS}{"temperature"}{UNIT} = defined($attr{$name}{"tempUnit"}) ? $attr{$name}{"tempUnit"} : "Celsius";
  304. $tunit = defined($attr{$name}{"tempUnit"}) ? $attr{$name}{"tempUnit"} : $hash->{READINGS}{"temperature"}{UNIT};
  305. $toffset = defined($attr{$name}{"tempOffset"}) ? $attr{$name}{"tempOffset"} : 0.0 ;
  306. $tfactor = 1.0;
  307. if( $tunit eq "none" ){
  308. $tabbr = "";
  309. }elsif( $tunit eq "Celsius" ){
  310. $tabbr = " °C";
  311. } elsif ($tunit eq "Kelvin" ){
  312. $tabbr = " K";
  313. $toffset += "273.16"
  314. } elsif ($tunit eq "Fahrenheit" ){
  315. $tabbr = " °F";
  316. $toffset = ($toffset+32)/1.8;
  317. $tfactor = 1.8;
  318. } else {
  319. $tabbr="?";
  320. Log 1, "OWMULTI_ChannelNames: unknown unit $tunit";
  321. }
  322. #-- these values are rather complex to obtain, therefore save them in the hash
  323. $hash->{READINGS}{"temperature"}{ABBR} = "T";
  324. $hash->{READINGS}{"temperature"}{UNIT} = $tabbr;
  325. $hash->{tempf}{offset} = $toffset;
  326. $hash->{tempf}{factor} = $tfactor;
  327. }
  328. ########################################################################################
  329. #
  330. # OWMULTI_FormatValues - put together various format strings
  331. #
  332. # Parameter hash = hash of device addressed, fs = format string
  333. #
  334. ########################################################################################
  335. sub OWMULTI_FormatValues($) {
  336. my ($hash) = @_;
  337. my $name = $hash->{NAME};
  338. my ($toffset,$tfactor,$tval,$vfunc,$wfunc,$vval,$wval);
  339. my $svalue = "";
  340. #-- no change in any value if invalid reading
  341. return if( ($hash->{owg_val}->[0] eq "") || ($hash->{owg_val}->[1] eq "") || ($hash->{owg_val}->[2] eq "") || ($hash->{owg_val}->[3] eq ""));
  342. #-- obtain channel names
  343. OWMULTI_ChannelNames($hash);
  344. #-- correct values for proper offset, factor
  345. $toffset = $hash->{tempf}{offset};
  346. $tfactor = $hash->{tempf}{factor};
  347. $tval = int(10*($hash->{owg_val}->[0] + $toffset)*$tfactor+0.5)/10;
  348. #-- attribute V/WFunction defined ?
  349. $vfunc = defined($attr{$name}{"VFunction"}) ? $attr{$name}{"VFunction"} : "V";
  350. $wfunc = defined($attr{$name}{"WFunction"}) ? $attr{$name}{"WFunction"} : "W";
  351. #-- replace by proper values
  352. $vfunc =~ s/VDD/\$hash->{owg_val}->[1]/g;
  353. $vfunc =~ s/V/\$hash->{owg_val}->[2]/g;
  354. $vfunc =~ s/W/\$hash->{owg_val}->[3]/g;
  355. $vfunc =~ s/T/\$tval/g;
  356. $wfunc =~ s/VDD/\$hash->{owg_val}->[1]/g;
  357. $wfunc =~ s/V/\$hash->{owg_val}->[2]/g;
  358. $wfunc =~ s/W/\$hash->{owg_val}->[3]/g;
  359. $wfunc =~ s/T/\$tval/g;
  360. #-- determine the measured value from the function
  361. $vfunc = "\$hash->{owg_val}->[1] = $hash->{owg_val}->[1]; \$hash->{owg_val}->[2] = $hash->{owg_val}->[2]; \$hash->{owg_val}->[3] = $hash->{owg_val}->[3]; \$tval = $tval; ".$vfunc;
  362. #Log 1, "vfunc= ".$vfunc;
  363. $vfunc = eval($vfunc);
  364. if( !$vfunc ){
  365. $vval = 0.0;
  366. } elsif( $vfunc ne "" ){
  367. $vval = int( $vfunc*100+0.5)/100;
  368. } else {
  369. $vval = "???";
  370. }
  371. $wfunc = "\$hash->{owg_val}->[1] = $hash->{owg_val}->[1]; \$hash->{owg_val}->[2] = $hash->{owg_val}->[2]; \$hash->{owg_val}->[3] = $hash->{owg_val}->[3]; \$tval = $tval; ".$wfunc;
  372. #Log 1, "wfunc= ".$wfunc;
  373. $wfunc = eval($wfunc);
  374. if( !$wfunc ){
  375. $wval = 0.0;
  376. } elsif( $wfunc ne "" ){
  377. $wval = int( $wfunc*100+0.5)/100;
  378. } else {
  379. $wval = "???";
  380. }
  381. #-- string buildup for return value, STATE
  382. $svalue .= sprintf( "%s: %5.2f%s (T: %5.1f%s %s: %5.2f%s)",
  383. $hash->{READINGS}{$owg_channel}{ABBR}, $vval,$hash->{READINGS}{$owg_channel}{UNIT},
  384. $tval,$hash->{READINGS}{"temperature"}{UNIT}, $hash->{READINGS}{$owg_schannel}{ABBR}, $wval,$hash->{READINGS}{$owg_schannel}{UNIT});
  385. #-- put into READINGS
  386. readingsBeginUpdate($hash);
  387. readingsBulkUpdate($hash,$owg_channel,$vval);
  388. readingsBulkUpdate($hash,$owg_schannel,$wval);
  389. readingsBulkUpdate($hash,"VDD",sprintf("%4.2f",$hash->{owg_val}->[1]));
  390. readingsBulkUpdate($hash,"temperature",$tval);
  391. #-- STATE
  392. readingsBulkUpdate($hash,"state",$svalue);
  393. readingsEndUpdate($hash,1);
  394. return $svalue;
  395. }
  396. ########################################################################################
  397. #
  398. # OWMULTI_Get - Implements GetFn function
  399. #
  400. # Parameter hash = hash of device addressed, a = argument array
  401. #
  402. ########################################################################################
  403. sub OWMULTI_Get($@) {
  404. my ($hash, @a) = @_;
  405. my $reading = $a[1];
  406. my $name = $hash->{NAME};
  407. my $model = $hash->{OW_MODEL};
  408. my $value = undef;
  409. my $ret = "";
  410. #-- check syntax
  411. return "OWMULTI: Get argument is missing @a"
  412. if(int(@a) != 2);
  413. #-- check argument
  414. my $msg = "OWMULTI: Get with unknown argument $a[1], choose one of ";
  415. $msg .= "$_$gets{$_} " foreach (keys%gets);
  416. return $msg
  417. if(!defined($gets{$a[1]}));
  418. #-- get id
  419. if($a[1] eq "id") {
  420. $value = $hash->{ROM_ID};
  421. return "$name.id => $value";
  422. }
  423. #-- hash of the busmaster
  424. my $master = $hash->{IODev};
  425. #-- Get other values according to interface type
  426. my $interface= $hash->{IODev}->{TYPE};
  427. #-- get version
  428. if( $a[1] eq "version") {
  429. return "$name.version => $owx_version";
  430. }
  431. #-- reset current ERRSTATE
  432. $hash->{ERRSTATE} = 0;
  433. #-- for the other readings we need a new reading
  434. #-- OWX interface
  435. if( $interface eq "OWX" ){
  436. #-- not different from getting all values ..
  437. $ret = OWXMULTI_GetValues($hash);
  438. }elsif( $interface eq "OWX_ASYNC"){
  439. eval {
  440. $ret = OWX_ASYNC_RunToCompletion($hash,OWXMULTI_PT_GetValues($hash));
  441. };
  442. $ret = GP_Catch($@) if $@;
  443. #-- OWFS interface
  444. }elsif( $interface eq "OWServer" ){
  445. $ret = OWFSMULTI_GetValues($hash);
  446. #-- Unknown interface
  447. }else{
  448. return "OWMULTI: Get with wrong IODev type $interface";
  449. }
  450. #-- process result
  451. if( $master->{ASYNCHRONOUS} ){
  452. #return "OWSMULTI: $name getting readings, please wait for completion";
  453. return undef;
  454. }else{
  455. if( defined($ret) ){
  456. return "OWMULTI: Could not get values from device $name, reason $ret";
  457. }
  458. #-- return the special reading
  459. if ($reading eq "reading") {
  460. return "OWMULTI: $name.reading => ".$hash->{READINGS}{"state"}{VAL};
  461. }
  462. if ($reading eq "temperature") {
  463. return "OWMULTI: $name.temperature => ".
  464. $hash->{READINGS}{"temperature"}{VAL};
  465. }
  466. if ($reading eq "VDD") {
  467. return "OWMULTI: $name.VDD => ".
  468. $hash->{owg_val}->[1];
  469. }
  470. if ( $reading eq "raw") {
  471. return "OWMULTI: $name.raw => ".
  472. $hash->{owg_val}->[2]." V ".$hash->{owg_val}->[3]." V";
  473. }
  474. }
  475. return undef;
  476. }
  477. #######################################################################################
  478. #
  479. # OWMULTI_GetValues - Updates the readings from device
  480. #
  481. # Parameter hash = hash of device addressed
  482. #
  483. ########################################################################################
  484. sub OWMULTI_GetValues($) {
  485. my $hash = shift;
  486. my $name = $hash->{NAME};
  487. my $value = "";
  488. my $ret = "";
  489. #-- check if device needs to be initialized
  490. OWMULTI_InitializeDevice($hash)
  491. if( $hash->{READINGS}{"state"}{VAL} eq "defined");
  492. RemoveInternalTimer($hash);
  493. #-- auto-update for device disabled;
  494. return undef
  495. if( $hash->{INTERVAL} == 0 );
  496. #-- restart timer for updates
  497. InternalTimer(time()+$hash->{INTERVAL}, "OWMULTI_GetValues", $hash, 0);
  498. #-- reset current ERRSTATE
  499. $hash->{ERRSTATE} = 0;
  500. #-- Get values according to interface type
  501. my $interface= $hash->{IODev}->{TYPE};
  502. if( $interface eq "OWX" ){
  503. $ret = OWXMULTI_GetValues($hash);
  504. }elsif( $interface eq "OWX_ASYNC" ){
  505. eval {
  506. OWX_ASYNC_Schedule( $hash, OWXMULTI_PT_GetValues($hash) );
  507. };
  508. $ret = GP_Catch($@) if $@;
  509. }elsif( $interface eq "OWServer" ){
  510. $ret = OWFSMULTI_GetValues($hash);
  511. }else{
  512. return "OWMULTI: GetValues with wrong IODev type $interface";
  513. }
  514. #-- process results
  515. if( defined($ret) ){
  516. return "OWMULTI: Could not get values from device $name, reason $ret";
  517. }
  518. return undef;
  519. }
  520. ########################################################################################
  521. #
  522. # OWMULTI_InitializeDevice - delayed setting of initial readings and channel names
  523. #
  524. # Parameter hash = hash of device addressed
  525. #
  526. ########################################################################################
  527. sub OWMULTI_InitializeDevice($) {
  528. my ($hash) = @_;
  529. my $name = $hash->{NAME};
  530. #-- Initial readings
  531. $hash->{owg_val}->[0] = "";
  532. $hash->{owg_val}->[1] = "";
  533. $hash->{owg_val}->[2] = "";
  534. $hash->{owg_val}->[3] = "";
  535. #-- Set state to initialized
  536. readingsSingleUpdate($hash,"state","initialized",1);
  537. }
  538. #######################################################################################
  539. #
  540. # OWMULTI_Set - Set one value for device
  541. #
  542. # Parameter hash = hash of device addressed
  543. # a = argument string
  544. #
  545. ########################################################################################
  546. sub OWMULTI_Set($@) {
  547. my ($hash, @a) = @_;
  548. #-- for the selector: which values are possible
  549. return join(" ", sort keys %sets) if(@a == 2);
  550. #-- check syntax
  551. return "OWMULTI: Set needs one parameter"
  552. if(int(@a) != 3);
  553. #-- check argument
  554. return "OWMULTI: Set with unknown argument $a[1], choose one of ".join(",", sort keys %sets)
  555. if(!defined($sets{$a[1]}));
  556. #-- define vars
  557. my $key = $a[1];
  558. my $value = $a[2];
  559. my $ret = undef;
  560. my $name = $hash->{NAME};
  561. my $model = $hash->{OW_MODEL};
  562. #-- set new timer interval
  563. if($key eq "interval") {
  564. # check value
  565. return "OWMULTI: set $name interval must be >= 0"
  566. if(int($value) < 0);
  567. # update timer
  568. $hash->{INTERVAL} = int($value);
  569. RemoveInternalTimer($hash);
  570. InternalTimer(gettimeofday()+$hash->{INTERVAL}, "OWMULTI_GetValues", $hash, 0);
  571. return undef;
  572. }
  573. #-- set other values depending on interface type
  574. my $interface = $hash->{IODev}->{TYPE};
  575. my $offset = $hash->{tempf}{offset};
  576. my $factor = $hash->{tempf}{factor};
  577. #-- find upper and lower boundaries for given offset/factor
  578. my $mmin = (-55+$offset)*$factor;
  579. my $mmax = (125+$offset)*$factor;
  580. return sprintf("OWMULTI: Set with wrong value $value for $key, range is [%3.1f,%3.1f]",$mmin,$mmax)
  581. if($value < $mmin || $value > $mmax);
  582. #-- seems to be ok, put into the device
  583. $a[2] = int($value/$factor-$offset);
  584. #-- OWX interface
  585. if( $interface eq "OWX" ){
  586. $ret = OWXMULTI_SetValues($hash,@a);
  587. }elsif( $interface eq "OWX_ASYNC" ){
  588. eval {
  589. OWX_ASYNC_Schedule( $hash, OWXMULTI_PT_SetValues($hash,@a) );
  590. };
  591. $ret = GP_Catch($@) if $@;
  592. #-- OWFS interface
  593. }elsif( $interface eq "OWServer" ){
  594. $ret = OWFSMULTI_SetValues($hash,@a);
  595. } else {
  596. return "OWMULTI: Set with wrong IODev type $interface";
  597. }
  598. #-- process results
  599. if( defined($ret) ){
  600. return "OWMULTI: Could not set device $name, reason: ".$ret;
  601. }
  602. #-- process results - we have to reread the device
  603. OWMULTI_GetValues($hash);
  604. Log 4, "OWMULTI: Set $hash->{NAME} $key $value";
  605. return undef;
  606. }
  607. ########################################################################################
  608. #
  609. # OWMULTI_Undef - Implements UndefFn function
  610. #
  611. # Parameter hash = hash of device addressed
  612. #
  613. ########################################################################################
  614. sub OWMULTI_Undef ($) {
  615. my ($hash) = @_;
  616. delete($main::modules{OWMULTI}{defptr}{$hash->{OW_ID}});
  617. RemoveInternalTimer($hash);
  618. return undef;
  619. }
  620. ########################################################################################
  621. #
  622. # The following subroutines in alphabetical order are only for a 1-Wire bus connected
  623. # via OWFS
  624. #
  625. # Prefix = OWFSMULTI
  626. #
  627. ########################################################################################
  628. #
  629. # OWFSMULTI_GetValues - Get reading from one device
  630. #
  631. # Parameter hash = hash of device addressed
  632. #
  633. ########################################################################################
  634. sub OWFSMULTI_GetValues($) {
  635. my ($hash) = @_;
  636. #-- ID of the device
  637. my $owx_add = substr($hash->{ROM_ID},0,15);
  638. #-- hash of the busmaster
  639. my $master = $hash->{IODev};
  640. my $name = $hash->{NAME};
  641. #-- reset presence
  642. $hash->{PRESENT} = 0;
  643. #-- get values - or should we rather get the uncached ones ?
  644. $hash->{owg_val}->[0] = OWServer_Read($master,"/$owx_add/temperature");
  645. $hash->{owg_val}->[1] = OWServer_Read($master,"/$owx_add/VDD");
  646. $hash->{owg_val}->[2] = OWServer_Read($master,"/$owx_add/VAD");
  647. $hash->{owg_val}->[3] = OWServer_Read($master,"/$owx_add/vis");
  648. return "no return from OWServer"
  649. if( (!defined($hash->{owg_val}->[0])) || (!defined($hash->{owg_val}->[1])) || (!defined($hash->{owg_val}->[2])) || (!defined($hash->{owg_val}->[3])) );
  650. return "empty return from OWServer"
  651. if( ($hash->{owg_val}->[0] eq "") || ($hash->{owg_val}->[1] eq "") || ($hash->{owg_val}->[2] eq "") || ($hash->{owg_val}->[3] eq "") );
  652. #-- and now from raw to formatted values
  653. $hash->{PRESENT} = 1;
  654. my $value = OWMULTI_FormatValues($hash);
  655. return undef;
  656. }
  657. #######################################################################################
  658. #
  659. # OWFSMULTI_SetValues - Set values in device
  660. #
  661. # Parameter hash = hash of device addressed
  662. # a = argument array
  663. #
  664. ########################################################################################
  665. sub OWFSMULTI_SetValues($@) {
  666. my ($hash, @a) = @_;
  667. }
  668. ########################################################################################
  669. #
  670. # The following subroutines in alphabetical order are only for a 1-Wire bus connected
  671. # directly to the FHEM server
  672. #
  673. # Prefix = OWXMULTI
  674. #
  675. ########################################################################################
  676. #
  677. # OWXMULTI_BinValues - Process reading from one device - translate binary into raw
  678. #
  679. # Parameter hash = hash of device addressed
  680. # context = mode for evaluating the binary data
  681. # proc = processing instruction, also passed to OWX_Read.
  682. # bitwise interpretation !!
  683. # if 0, nothing special
  684. # if 1 = bit 0, a reset will be performed not only before, but also after
  685. # the last operation in OWX_Read
  686. # if 2 = bit 1, the initial reset of the bus will be suppressed
  687. # if 8 = bit 3, the fillup of the data with 0xff will be suppressed
  688. # if 16= bit 4, the insertion will be at the top of the queue
  689. # owx_dev = ROM ID of slave device
  690. # crcpart = part of the data that needs to be part of the CRC check
  691. # numread = number of bytes to receive
  692. # res = result string
  693. #
  694. #
  695. ########################################################################################
  696. sub OWXMULTI_BinValues($$$$$$$) {
  697. my ($hash, $context, $proc, $owx_dev, $crcpart, $numread, $res) = @_;
  698. #-- hash of the busmaster
  699. my $master = $hash->{IODev};
  700. my $name = $hash->{NAME};
  701. #-- inherit previous error
  702. my $error = $hash->{ERRSTATE};
  703. my @data = [];
  704. my ($value,$lsb,$msb,$sign);
  705. my $msg;
  706. OWX_WDBGL($name,4,"OWXMULTI_BinValues: called for device $name in context $context with data ",$res);
  707. #-- always check for success, unused are reset, numread
  708. return unless ($context =~ /^ds2438.getv[ad]d$/);
  709. #-- we have to get rid of the first 11 bytes
  710. if( length($res) == 20 ){
  711. $res=substr($res,11);
  712. }
  713. @data=split(//,$res);
  714. #-- process results
  715. if ((ord($data[0]) & 112)!=0) {
  716. $msg = "$name: conversion not complete or data invalid in context $context ";
  717. $error = 1;
  718. }elsif (OWX_CRC8(substr($res,0,8),$data[8]) eq "\0x00") {
  719. $msg = "$name: invalid CRC ";
  720. $error = 1;
  721. }else{
  722. $msg = "$name: no error, ";
  723. }
  724. OWX_WDBGL($name,5-4*$error,"OWXMULTI_BinValues: ".$msg,$res);
  725. #-- this must be different for the different device types
  726. # family = 26 => DS2438
  727. # family = A6 => DS2438a
  728. #-- transform binary rep of VDD
  729. if( $context eq "ds2438.getvdd") {
  730. #-- possible addtional check: $data[0] must be 08
  731. #-- temperature
  732. $lsb = ord($data[1]);
  733. $msb = ord($data[2]) & 127;
  734. $sign = ord($data[2]) & 128;
  735. #-- test with -55 degrees
  736. #$lsb = 0;
  737. #$sign = 1;
  738. #$msb = 73;
  739. #-- 2's complement form = signed bytes
  740. $hash->{owg_val}->[0] = $msb+ $lsb/256.;
  741. if( $sign !=0 ){
  742. $hash->{owg_val}->[0] = -128+$hash->{owg_val}->[0];
  743. }
  744. #-- voltage
  745. $lsb = ord($data[3]);
  746. $msb = ord($data[4]) & 3;
  747. #-- test with 5V
  748. #$lsb = 244;
  749. #$msb = 1;
  750. #-- supply voltage
  751. $hash->{owg_val}->[1] = ($msb*256+ $lsb)/100.;
  752. #-- transform binary rep of VAD
  753. }elsif( $context eq "ds2438.getvad") {
  754. #-- possible addtional check: $data[0] must be 08
  755. #-- voltage
  756. $lsb = ord($data[3]);
  757. $msb = ord($data[4]) & 3;
  758. #-- test with 7.2 V
  759. #$lsb = 208;
  760. #$msb = 2;
  761. #-- external voltage
  762. $hash->{owg_val}->[2] = ($msb*256+ $lsb)/100.;
  763. #-- current
  764. $lsb = ord($data[5]);
  765. $msb = ord($data[6]) & 3;
  766. #-- external current
  767. $hash->{owg_val}->[3] = ($msb*256.+ $lsb)/4096;
  768. #-- and now from raw to formatted values
  769. if( $error ){
  770. $hash->{ERRCOUNT}++;
  771. $hash->{ERRSTATE} = 1;
  772. }else{
  773. $hash->{PRESENT} = 1;
  774. OWMULTI_FormatValues($hash);
  775. }
  776. }
  777. return undef;
  778. }
  779. ########################################################################################
  780. #
  781. # OWXMULTI_GetValues - Get reading from one device
  782. #
  783. # Parameter hash = hash of device addressed
  784. # final= 1 if FormatValues is to be called
  785. #
  786. ########################################################################################
  787. sub OWXMULTI_GetValues($) {
  788. my ($hash) = @_;
  789. my ($res,$ret);
  790. #-- ID of the device
  791. my $owx_dev = $hash->{ROM_ID};
  792. #-- hash of the busmaster
  793. my $master = $hash->{IODev};
  794. #-- reset presence
  795. $hash->{PRESENT} = 0;
  796. #------------------------------------------------------------------------------------
  797. #-- switch the device to current measurement off, VDD only
  798. #-- issue the match ROM command \x55 and the write scratchpad command
  799. #-- OLD OWX interface
  800. if( !$master->{ASYNCHRONOUS} ){
  801. OWX_Reset($master);
  802. #if( OWX_Complex($master,$owx_dev,"\x4E\x00\x08",0) eq 0 ){
  803. if( OWX_Complex($master,$owx_dev,"\x4E\x00\x09",0) eq 0 ){
  804. return "$owx_dev write status failed";
  805. }
  806. #-- copy scratchpad to register
  807. #-- issue the match ROM command \x55 and the copy scratchpad command
  808. OWX_Reset($master);
  809. if( OWX_Complex($master,$owx_dev,"\x48\x00",0) eq 0){
  810. return "$owx_dev copy scratchpad failed";
  811. }
  812. #-- initiate temperature conversion
  813. #-- conversion needs some 12 ms !
  814. #-- issue the match ROM command \x55 and the start conversion command
  815. OWX_Reset($master);
  816. if( OWX_Complex($master,$owx_dev,"\x44",0) eq 0 ){
  817. return "$owx_dev temperature conversion failed";
  818. }
  819. select(undef,undef,undef,0.012);
  820. #-- initiate voltage conversion
  821. #-- conversion needs some 6 ms !
  822. #-- issue the match ROM command \x55 and the start conversion command
  823. OWX_Reset($master);
  824. if( OWX_Complex($master,$owx_dev,"\xB4",0.01) eq 0 ){
  825. return "$owx_dev voltage conversion failed";
  826. }
  827. select(undef,undef,undef,0.006);
  828. #-- from memory to scratchpad
  829. #-- copy needs some 12 ms !
  830. #-- issue the match ROM command \x55 and the recall memory command
  831. OWX_Reset($master);
  832. if( OWX_Complex($master,$owx_dev,"\xB8\x00",0.02) eq 0 ){
  833. return "$owx_dev recall memory failed";
  834. }
  835. select(undef,undef,undef,0.012);
  836. #-- NOW ask the specific device
  837. #-- issue the match ROM command \x55 and the read scratchpad command \xBE
  838. #-- reading 9 + 2 + 9 data bytes = 20 bytes
  839. OWX_Reset($master);
  840. $res=OWX_Complex($master,$owx_dev,"\xBE\x00",9);
  841. #Log 1,"OWXMULTI: data length from reading device is ".length($res)." bytes";
  842. return "$owx_dev not accessible in 2nd step"
  843. if( $res eq 0 );
  844. return "$owx_dev has returned invalid data"
  845. if( length($res)!=20);
  846. $ret = OWXMULTI_BinValues($hash,"ds2438.getvdd",undef,$owx_dev,undef,undef,substr($res,11));
  847. return $ret if (defined $ret);
  848. #------------------------------------------------------------------------------------
  849. #-- switch the device to current measurement off, V external only
  850. #-- issue the match ROM command \x55 and the write scratchpad command
  851. OWX_Reset($master);
  852. #if( OWX_Complex($master,$owx_dev,"\x4E\x00\x00",0) eq 0 ){
  853. if( OWX_Complex($master,$owx_dev,"\x4E\x00\x01",0) eq 0 ){
  854. return "$owx_dev write status failed";
  855. }
  856. #-- copy scratchpad to register
  857. #-- issue the match ROM command \x55 and the copy scratchpad command
  858. OWX_Reset($master);
  859. if( OWX_Complex($master,$owx_dev,"\x48\x00",0) eq 0){
  860. return "$owx_dev copy scratchpad failed";
  861. }
  862. #-- initiate voltage conversion
  863. #-- conversion needs some 6 ms !
  864. #-- issue the match ROM command \x55 and the start conversion command
  865. OWX_Reset($master);
  866. if( OWX_Complex($master,$owx_dev,"\xB4",0.01) eq 0 ){
  867. return "$owx_dev voltage conversion failed";
  868. }
  869. select(undef,undef,undef,0.006);
  870. #-- from memory to scratchpad
  871. #-- copy needs some 12 ms !
  872. #-- issue the match ROM command \x55 and the recall memory command
  873. OWX_Reset($master);
  874. if( OWX_Complex($master,$owx_dev,"\xB8\x00",0.02) eq 0 ){
  875. return "$owx_dev recall memory failed";
  876. }
  877. select(undef,undef,undef,0.012);
  878. #-- NOW ask the specific device
  879. #-- issue the match ROM command \x55 and the read scratchpad command \xBE
  880. #-- reading 9 + 2 + 9 data bytes = 20 bytes
  881. my $context = "ds2438.getvad";
  882. OWX_Reset($master);
  883. $res=OWX_Complex($master,$owx_dev,"\xBE\x00",9);
  884. #-- process results
  885. return "$owx_dev not accessible in 2nd step"
  886. if( $res eq 0 );
  887. return "$owx_dev has returned invalid data"
  888. if( length($res)!=20);
  889. return OWXMULTI_BinValues($hash,$context,undef,$owx_dev,undef,undef,substr($res,11));
  890. #-- NEW OWX interface
  891. }else{
  892. #-- switch the device to current measurement off, VDD only
  893. #-- issue the match ROM command \x55 and the write scratchpad command
  894. #### master slave context proc owx_dev data crcpart numread startread callback delay
  895. #OWX_Qomplex($master, $hash, "write SP", 0, $owx_dev, "\x4E\x00\x08", 0, 0, 0, undef, undef);
  896. #-- switch the device to current measurement on, VDD only
  897. #-- issue the match ROM command \x55 and the write scratchpad command
  898. #### master slave context proc owx_dev data crcpart numread startread callback delay
  899. OWX_Qomplex($master, $hash, "write SP", 0, $owx_dev, "\x4E\x00\x09", 0, 2, 0, undef, 0.015);
  900. #-- copy scratchpad to register
  901. #-- issue the match ROM command \x55 and the copy scratchpad command
  902. #### master slave context proc owx_dev data crcpart numread startread callback delay
  903. OWX_Qomplex($master, $hash, "copy SP", 0, $owx_dev, "\x48\x00", 0, 1, 0, undef, 0.015);
  904. #-- initiate temperature conversion
  905. #-- conversion needs some 12 ms !
  906. #-- issue the match ROM command \x55 and the start conversion command
  907. #### master slave context proc owx_dev data crcpart numread startread callback delay
  908. OWX_Qomplex($master, $hash, "T conversion", 0, $owx_dev, "\x44", 0, 0, 0, undef, 0.015);
  909. #-- initiate voltage conversion
  910. #-- conversion needs some 6 ms !
  911. #-- issue the match ROM command \x55 and the start conversion command
  912. #### master slave context proc owx_dev data crcpart numread startread callback delay
  913. OWX_Qomplex($master, $hash, "V conversion", 0, $owx_dev, "\xB4", 0, 0, 0, undef, 0.015);
  914. #-- from memory to scratchpad
  915. #-- copy needs some 12 ms !
  916. #-- issue the match ROM command \x55 and the recall memory command
  917. #### master slave context proc owx_dev data crcpart numread startread callback delay
  918. OWX_Qomplex($master, $hash, "recall", 0, $owx_dev, "\xB8\x00", 0, 2, 0, undef, 0.015);
  919. #-- NOW ask the specific device
  920. #-- issue the match ROM command \x55 and the read scratchpad command \xBE
  921. #-- reading 9 + 2 + 9 data bytes = 20 bytes
  922. #### master slave context proc owx_dev data crcpart numread startread callback delay
  923. # 1 provides additional reset after last operation
  924. OWX_Qomplex($master, $hash, "ds2438.getvdd", 1, $owx_dev, "\xBE\x00", 0, 20, 0, \&OWXMULTI_BinValues, 0.015);
  925. #-- switch the device to current measurement off, V external only
  926. #-- issue the match ROM command \x55 and the write scratchpad command
  927. #### master slave context proc owx_dev data crcpart numread startread callback delay
  928. #OWX_Qomplex($master, $hash, "write SP", 0, $owx_dev, "\x4E\x00\x00", 0, 0, 0, undef, undef);
  929. #-- switch the device to current measurement on, V external only
  930. #-- issue the match ROM command \x55 and the write scratchpad command
  931. #### master slave context proc owx_dev data crcpart numread startread callback delay
  932. OWX_Qomplex($master, $hash, "write SP", 0, $owx_dev, "\x4E\x00\x01", 0, 1, 0, undef, 0.015);
  933. #-- copy scratchpad to register
  934. #-- issue the match ROM command \x55 and the copy scratchpad command
  935. #### master slave context proc owx_dev data crcpart numread startread callback delay
  936. OWX_Qomplex($master, $hash, "copy SP", 0, $owx_dev, "\x48\x00", 0, 1, 0, undef, 0.015);
  937. #-- initiate voltage conversion
  938. #-- conversion needs some 6 ms !
  939. #-- issue the match ROM command \x55 and the start conversion command
  940. #### master slave context proc owx_dev data crcpart numread startread callback delay
  941. OWX_Qomplex($master, $hash, "V conversion", 0, $owx_dev, "\xB4", 0, 0, 0, undef, 0.015);
  942. #-- from memory to scratchpad
  943. #-- copy needs some 12 ms !
  944. #-- issue the match ROM command \x55 and the recall memory command
  945. #### master slave context proc owx_dev data crcpart numread startread callback delay
  946. OWX_Qomplex($master, $hash, "recall", 0, $owx_dev, "\xB8\x00", 0, 1, 0, undef, 0.015);
  947. #-- NOW ask the specific device
  948. #-- issue the match ROM command \x55 and the read scratchpad command \xBE
  949. #-- reading 9 + 2 + 9 data bytes = 20 bytes
  950. #### master slave context proc owx_dev data crcpart numread startread callback delay
  951. # 1 provides additional reset after last operation
  952. OWX_Qomplex($master, $hash, "ds2438.getvad", 1, $owx_dev, "\xBE\x00", 0, 20, 0, \&OWXMULTI_BinValues, 0.015);
  953. return undef;
  954. }
  955. }
  956. #######################################################################################
  957. #
  958. # OWXMULTI_SetValues - Set values in device
  959. #
  960. # Parameter hash = hash of device addressed
  961. # a = argument array
  962. #
  963. ########################################################################################
  964. sub OWXMULTI_SetValues($@) {
  965. my ($hash, @a) = @_;
  966. my ($i,$j,$k);
  967. my $name = $hash->{NAME};
  968. #-- ID of the device
  969. my $owx_dev = $hash->{ROM_ID};
  970. #-- hash of the busmaster
  971. my $master = $hash->{IODev};
  972. #-- define vars
  973. my $key = $a[1];
  974. my $value = $a[2];
  975. #-- issue the match ROM command \x55 and the write scratchpad command \x4E,
  976. # followed by the write EEPROM command \x48
  977. #
  978. # so far writing the EEPROM does not work properly.
  979. # 1. \x48 directly appended to the write scratchpad command => command ok, no effect on EEPROM
  980. # 2. \x48 appended to match ROM => command not ok.
  981. # 3. \x48 sent by WriteBytePower after match ROM => command ok, no effect on EEPROM
  982. my $select=sprintf("\x4E%c%c\x48",0,0);
  983. OWX_Reset($master);
  984. my $res=OWX_Complex($master,$owx_dev,$select,0);
  985. if( $res eq 0 ){
  986. return "OWXMULTI: Device $owx_dev not accessible";
  987. }
  988. return undef;
  989. }
  990. ########################################################################################
  991. #
  992. # OWXMULTI_PT_GetValues - Get reading from one device async
  993. #
  994. # Parameter hash = hash of device addressed
  995. # final= 1 if FormatValues is to be called
  996. #
  997. ########################################################################################
  998. sub OWXMULTI_PT_GetValues($) {
  999. my ($hash) = @_;
  1000. return PT_THREAD(sub {
  1001. my ($thread) = @_;
  1002. my ($i,$j,$k,$res,$ret,$response);
  1003. #-- ID of the device
  1004. my $owx_dev = $hash->{ROM_ID};
  1005. #-- hash of the busmaster
  1006. my $master = $hash->{IODev};
  1007. PT_BEGIN($thread);
  1008. #------------------------------------------------------------------------------------
  1009. #-- switch the device to current measurement off, VDD only
  1010. #-- issue the match ROM command \x55 and the write scratchpad command
  1011. #"ds2438.writestatusvdd"
  1012. $thread->{pt_execute} = OWX_ASYNC_PT_Execute($master,1,$owx_dev,"\x4E\x00\x08",0);
  1013. PT_WAIT_THREAD($thread->{pt_execute});
  1014. die $thread->{pt_execute}->PT_CAUSE() if ($thread->{pt_execute}->PT_STATE() == PT_ERROR);
  1015. #-- copy scratchpad to register
  1016. #-- issue the match ROM command \x55 and the copy scratchpad command
  1017. #"ds2438.copyscratchpadvdd"
  1018. $thread->{pt_execute} = OWX_ASYNC_PT_Execute($master,1,$owx_dev,"\x48\x00",0);
  1019. PT_WAIT_THREAD($thread->{pt_execute});
  1020. die $thread->{pt_execute}->PT_CAUSE() if ($thread->{pt_execute}->PT_STATE() == PT_ERROR);
  1021. #-- initiate temperature conversion
  1022. #-- conversion needs some 12 ms !
  1023. #-- issue the match ROM command \x55 and the start conversion command
  1024. #"ds2438.temperaturconversionvdd"
  1025. $thread->{pt_execute} = OWX_ASYNC_PT_Execute($master,1,$owx_dev,"\x44",0);
  1026. $thread->{ExecuteTime} = gettimeofday() + 0.03; # was 0.012
  1027. PT_WAIT_THREAD($thread->{pt_execute});
  1028. die $thread->{pt_execute}->PT_CAUSE() if ($thread->{pt_execute}->PT_STATE() == PT_ERROR);
  1029. PT_YIELD_UNTIL(gettimeofday() >= $thread->{ExecuteTime});
  1030. delete $thread->{ExecuteTime};
  1031. #-- initiate voltage conversion
  1032. #-- conversion needs some 6 ms !
  1033. #-- issue the match ROM command \x55 and the start conversion command
  1034. #"ds2438.voltageconversionvdd"
  1035. $thread->{pt_execute} = OWX_ASYNC_PT_Execute($master,1,$owx_dev,"\xB4",0);
  1036. $thread->{ExecuteTime} = gettimeofday() + 0.02; # was 0.006
  1037. PT_WAIT_THREAD($thread->{pt_execute});
  1038. die $thread->{pt_execute}->PT_CAUSE() if ($thread->{pt_execute}->PT_STATE() == PT_ERROR);
  1039. PT_YIELD_UNTIL(gettimeofday() >= $thread->{ExecuteTime});
  1040. delete $thread->{ExecuteTime};
  1041. #-- from memory to scratchpad
  1042. #-- copy needs some 12 ms !
  1043. #-- issue the match ROM command \x55 and the recall memory command
  1044. #"ds2438.recallmemoryvdd"
  1045. $thread->{pt_execute} = OWX_ASYNC_PT_Execute($master,1,$owx_dev,"\xB8\x00",0);
  1046. $thread->{ExecuteTime} = gettimeofday() + 0.03; # was 0.012
  1047. PT_WAIT_THREAD($thread->{pt_execute});
  1048. die $thread->{pt_execute}->PT_CAUSE() if ($thread->{pt_execute}->PT_STATE() == PT_ERROR);
  1049. PT_YIELD_UNTIL(gettimeofday() >= $thread->{ExecuteTime});
  1050. delete $thread->{ExecuteTime};
  1051. #-- NOW ask the specific device
  1052. #-- issue the match ROM command \x55 and the read scratchpad command \xBE
  1053. #-- reading 9 + 2 + 9 data bytes = 20 bytes
  1054. #"ds2438.getvdd"
  1055. $thread->{pt_execute} = OWX_ASYNC_PT_Execute($master,1,$owx_dev,"\xBE\x00",9);
  1056. PT_WAIT_THREAD($thread->{pt_execute});
  1057. die $thread->{pt_execute}->PT_CAUSE() if ($thread->{pt_execute}->PT_STATE() == PT_ERROR);
  1058. $res = $thread->{pt_execute}->PT_RETVAL();
  1059. unless (defined $res and length($res)==9) {
  1060. PT_EXIT("$owx_dev has returned invalid data");
  1061. }
  1062. $ret = OWXMULTI_BinValues($hash,"ds2438.getvdd",undef,$owx_dev,undef,undef,$res);
  1063. if ($ret) {
  1064. die $ret;
  1065. }
  1066. #------------------------------------------------------------------------------------
  1067. #-- switch the device to current measurement off, V external only
  1068. #-- issue the match ROM command \x55 and the write scratchpad command
  1069. #"ds2438.writestatusvad"
  1070. $thread->{pt_execute} = OWX_ASYNC_PT_Execute($master,1,$owx_dev,"\x4E\x00\x00",0);
  1071. PT_WAIT_THREAD($thread->{pt_execute});
  1072. die $thread->{pt_execute}->PT_CAUSE() if ($thread->{pt_execute}->PT_STATE() == PT_ERROR);
  1073. #-- copy scratchpad to register
  1074. #-- issue the match ROM command \x55 and the copy scratchpad command
  1075. #"ds2438.copyscratchpadvad"
  1076. $thread->{pt_execute} = OWX_ASYNC_PT_Execute($master,1,$owx_dev,"\x48\x00",0);
  1077. PT_WAIT_THREAD($thread->{pt_execute});
  1078. die $thread->{pt_execute}->PT_CAUSE() if ($thread->{pt_execute}->PT_STATE() == PT_ERROR);
  1079. #-- initiate voltage conversion
  1080. #-- conversion needs some 6 ms !
  1081. #-- issue the match ROM command \x55 and the start conversion command
  1082. #"ds2438.voltageconversionvad"
  1083. $thread->{pt_execute} = OWX_ASYNC_PT_Execute($master,1,$owx_dev,"\xB4",0);
  1084. $thread->{ExecuteTime} = gettimeofday() + 0.02; # was 0.006
  1085. PT_WAIT_THREAD($thread->{pt_execute});
  1086. die $thread->{pt_execute}->PT_CAUSE() if ($thread->{pt_execute}->PT_STATE() == PT_ERROR);
  1087. PT_YIELD_UNTIL(gettimeofday() >= $thread->{ExecuteTime});
  1088. delete $thread->{ExecuteTime};
  1089. #-- from memory to scratchpad
  1090. #-- copy needs some 12 ms !
  1091. #-- issue the match ROM command \x55 and the recall memory command
  1092. #"ds2438.recallmemoryvad"
  1093. $thread->{pt_execute} = OWX_ASYNC_PT_Execute($master,1,$owx_dev,"\xB8\x00",0);
  1094. $thread->{ExecuteTime} = gettimeofday() + 0.03; # was 0.012
  1095. PT_WAIT_THREAD($thread->{pt_execute});
  1096. die $thread->{pt_execute}->PT_CAUSE() if ($thread->{pt_execute}->PT_STATE() == PT_ERROR);
  1097. PT_YIELD_UNTIL(gettimeofday() >= $thread->{ExecuteTime});
  1098. delete $thread->{ExecuteTime};
  1099. #-- NOW ask the specific device
  1100. #-- issue the match ROM command \x55 and the read scratchpad command \xBE
  1101. #-- reading 9 + 2 + 9 data bytes = 20 bytes
  1102. #"ds2438.getvad"
  1103. $thread->{pt_execute} = OWX_ASYNC_PT_Execute($master,1,$owx_dev,"\xBE\x00", 9);
  1104. PT_WAIT_THREAD($thread->{pt_execute});
  1105. die $thread->{pt_execute}->PT_CAUSE() if ($thread->{pt_execute}->PT_STATE() == PT_ERROR);
  1106. #-- process results
  1107. $res = $thread->{pt_execute}->PT_RETVAL();
  1108. unless (defined $res and length($res)==9) {
  1109. PT_EXIT("$owx_dev has returned invalid data");
  1110. }
  1111. $ret = OWXMULTI_BinValues($hash,"ds2438.getvad",undef,$owx_dev,undef,undef,$res);
  1112. if ($ret) {
  1113. die $ret;
  1114. }
  1115. PT_END;
  1116. });
  1117. }
  1118. #######################################################################################
  1119. #
  1120. # OWXMULTI_PT_SetValues - Set values in device async
  1121. #
  1122. # Parameter hash = hash of device addressed
  1123. # a = argument array
  1124. #
  1125. ########################################################################################
  1126. sub OWXMULTI_PT_SetValues($@) {
  1127. my ($hash, @a) = @_;
  1128. return PT_THREAD(sub {
  1129. my ($thread) = @_;
  1130. my ($i,$j,$k);
  1131. my $name = $hash->{NAME};
  1132. #-- ID of the device
  1133. my $owx_dev = $hash->{ROM_ID};
  1134. #-- hash of the busmaster
  1135. my $master = $hash->{IODev};
  1136. PT_BEGIN($thread);
  1137. #-- define vars
  1138. my $key = $a[1];
  1139. my $value = $a[2];
  1140. #-- issue the match ROM command \x55 and the write scratchpad command \x4E,
  1141. # followed by the write EEPROM command \x48
  1142. #
  1143. # so far writing the EEPROM does not work properly.
  1144. # 1. \x48 directly appended to the write scratchpad command => command ok, no effect on EEPROM
  1145. # 2. \x48 appended to match ROM => command not ok.
  1146. # 3. \x48 sent by WriteBytePower after match ROM => command ok, no effect on EEPROM
  1147. my $select=sprintf("\x4E%c%c\x48",0,0);
  1148. #"setvalues"
  1149. $thread->{pt_execute} = OWX_ASYNC_PT_Execute($master,1,$owx_dev,$select, 0);
  1150. PT_WAIT_THREAD($thread->{pt_execute});
  1151. die $thread->{pt_execute}->PT_CAUSE() if ($thread->{pt_execute}->PT_STATE() == PT_ERROR);
  1152. PT_END;
  1153. });
  1154. }
  1155. 1;
  1156. =pod
  1157. =item device
  1158. =item summary to control 1-Wire chip DS2438Z - Smart Battery Monitor
  1159. =begin html
  1160. <a name="OWMULTI"></a>
  1161. <h3>OWMULTI</h3>
  1162. <ul>
  1163. <p>FHEM module to commmunicate with 1-Wire multi-sensors, currently the DS2438 smart battery
  1164. monitor<br /> <br />This 1-Wire module works with the OWX interface module or with the OWServer interface module
  1165. (prerequisite: Add this module's name to the list of clients in OWServer).
  1166. Please define an <a href="#OWX">OWX</a> device or <a href="#OWServer">OWServer</a> device first.</p>
  1167. <h4>Example</h4>
  1168. <p>
  1169. <code>define OWX_M OWMULTI 7C5034010000 45</code>
  1170. <br />
  1171. <code>attr OWX_M VName humidity|rH</code>
  1172. <br />
  1173. <code>attr OWX_M VUnit %</code>
  1174. <br />
  1175. <code>attr OWX_M VFunction (161.29 * V / VDD - 25.8065)/(1.0546 - 0.00216 * T)</code>
  1176. </p>
  1177. <a name="OWMULTIdefine"></a>
  1178. <h4>Define</h4>
  1179. <p>
  1180. <code>define &lt;name&gt; OWMULTI [&lt;model&gt;] &lt;id&gt; [&lt;interval&gt;]</code> or <br/>
  1181. <code>define &lt;name&gt; OWMULTI &lt;fam&gt;.&lt;id&gt; [&lt;interval&gt;]</code>
  1182. <br /><br /> Define a 1-Wire multi-sensor</p>
  1183. <ul>
  1184. <li>
  1185. <code>[&lt;model&gt;]</code><br /> Defines the sensor model (and thus 1-Wire family
  1186. id), currently the following values are permitted: <ul>
  1187. <li>model DS2438 with family id 26 (default if the model parameter is omitted).
  1188. Measured is a temperature value, an external voltage and the current supply
  1189. voltage</li>
  1190. <li>model DS2438a with family id A6.
  1191. Measured is a temperature value, an external voltage and the current supply
  1192. voltage</li>
  1193. </ul>
  1194. </li>
  1195. <li>
  1196. <code>&lt;fam&gt;</code>
  1197. <br />2-character unique family id, see above
  1198. </li>
  1199. <li>
  1200. <code>&lt;id&gt;</code>
  1201. <br />12-character unique ROM id of the converter device without family id and CRC
  1202. code </li>
  1203. <li>
  1204. <code>&lt;interval&gt;</code>
  1205. <br />Measurement interval in seconds. The default is 300 seconds, a value of 0 disables the automatic update. </li>
  1206. </ul>
  1207. <a name="OWMULTIset"></a>
  1208. <h4>Set</h4>
  1209. <ul>
  1210. <li><a name="owmulti_interval">
  1211. <code>set &lt;name&gt; interval &lt;int&gt;</code></a><br /> Measurement
  1212. interval in seconds. The default is 300 seconds, a value of 0 disables the automatic update. </li>
  1213. </ul>
  1214. <a name="OWMULTIget"></a>
  1215. <h4>Get</h4>
  1216. <ul>
  1217. <li><a name="owmulti_id">
  1218. <code>get &lt;name&gt; id</code></a>
  1219. <br /> Returns the full 1-Wire device id OW_FAMILY.ROM_ID.CRC </li>
  1220. <li><a name="owmulti_reading">
  1221. <code>get &lt;name&gt; reading</code></a><br />Obtain all three measurement values. </li>
  1222. <li><a name="owmulti_temperature">
  1223. <code>get &lt;name&gt; temperature</code></a><br />Obtain the temperature value. </li>
  1224. <li><a name="owmulti_vdd">
  1225. <code>get &lt;name&gt; VDD</code></a><br />Obtain the current supply voltage. </li>
  1226. <li><a name="owmulti_raw">
  1227. <code>get &lt;name&gt; raw</code></a><br />Obtain the raw readings for V and W.</li>
  1228. </ul>
  1229. <a name="OWMULTIattr"></a>
  1230. <h4>Attributes</h4>
  1231. <ul><li><a name="owtherm_interval2">
  1232. <code>attr &lt;name&gt; interval &lt;int&gt;</code></a><br /> Measurement
  1233. interval in seconds. The default is 300 seconds, a value of 0 disables the automatic update.</li>
  1234. <li><a name="owmulti_vname"><code>attr &lt;name&gt; VName
  1235. &lt;string&gt;[|&lt;string&gt;]</code></a>
  1236. <br />name for the voltage channel [|short name used in state reading]. </li>
  1237. <li><a name="owmulti_vunit"><code>attr &lt;name&gt; VUnit
  1238. &lt;string&gt;</code></a>
  1239. <br />unit of measurement for the voltage channel used in state reading (default "V", set to "none" for empty).</li>
  1240. <li><a name="owmulti_vfunction"><code>attr &lt;name&gt; VFunction
  1241. &lt;string&gt;</code></a>
  1242. <br />arbitrary functional expression involving the values VDD, V, W and T. Example see
  1243. above. <ul>
  1244. <li>VDD is replaced by the measured supply voltage in Volt,</li>
  1245. <li>V by the measured external voltage channel,</li>
  1246. <li>W by the measured external sense channel,</li>
  1247. <li>T by the measured and corrected temperature in its unit</li>
  1248. </ul></li>
  1249. <li><a name="owmulti_wname"><code>attr &lt;name&gt; WName
  1250. &lt;string&gt;[|&lt;string&gt;]</code></a>
  1251. <br />name for the sense channel [|short name used in state reading]. </li>
  1252. <li><a name="owmulti_wunit"><code>attr &lt;name&gt; WUnit
  1253. &lt;string&gt;</code></a>
  1254. <br />unit of measurement for the sense channel used in state reading (default “V", set to "none" for empty).</li>
  1255. <li><a name="owmulti_wfunction"><code>attr &lt;name&gt; WFunction
  1256. &lt;string&gt;</code></a>
  1257. <br />arbitrary functional expression involving the values VDD, V, W and T. Example and usage see
  1258. above.</li>
  1259. <li><a name="owmulti_tempOffset"><code>attr &lt;name&gt; tempOffset &lt;float&gt;</code>
  1260. </a>
  1261. <br />temperature offset in &deg;C added to the raw temperature reading. </li>
  1262. <li><a name="owmulti_tempUnit"><code>attr &lt;name&gt; tempUnit
  1263. Celsius|Kelvin|Fahrenheit</code>
  1264. </a>
  1265. <br />unit of measurement (temperature scale), default is Celsius = &deg;C </li>
  1266. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  1267. </ul>
  1268. </ul>
  1269. =end html
  1270. =cut