21_OWMULTI.pm 55 KB

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