09_BS.pm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #
  2. #
  3. # 09_BS.pm
  4. # written by Dr. Boris Neubert 2009-06-20
  5. # e-mail: omega at online dot de
  6. #
  7. ##############################################
  8. # $Id: 09_BS.pm 16375 2018-03-10 15:40:02Z neubert $
  9. package main;
  10. use strict;
  11. use warnings;
  12. #############################
  13. sub
  14. BS_Initialize($)
  15. {
  16. my ($hash) = @_;
  17. $hash->{Match} = "^81..(04|0c)..0101a001a5cf";
  18. $hash->{DefFn} = "BS_Define";
  19. $hash->{UndefFn} = "BS_Undef";
  20. $hash->{ParseFn} = "BS_Parse";
  21. $hash->{AttrList} = "do_not_notify:1,0 showtime:0,1 ".
  22. "ignore:1,0 model:BS " . $readingFnAttributes;
  23. }
  24. #############################
  25. sub
  26. BS_Define($$)
  27. {
  28. my ($hash, $def) = @_;
  29. my @a = split("[ \t][ \t]*", $def);
  30. my $u= "wrong syntax: define <name> BS <sensor> [[RExt] luxOffset]";
  31. return $u if((int(@a)< 3) || (int(@a)>5));
  32. my $name = $a[0];
  33. my $sensor = $a[2];
  34. if($sensor !~ /[123456789]/) {
  35. return "erroneous sensor specification $sensor, use one of 1..9";
  36. }
  37. $sensor= "0$sensor";
  38. my $RExt = 50000; # default is 50kOhm
  39. $RExt= $a[3] if(int(@a)>=4);
  40. my $luxOffset= 0; # default is no offset
  41. $luxOffset= $a[4] if(int(@a)>=5);
  42. $hash->{SENSOR}= "$sensor";
  43. $hash->{RExt}= $RExt;
  44. $hash->{luxOffset}= $luxOffset;
  45. my $dev= "a5cf $sensor";
  46. $hash->{DEF}= $dev;
  47. $modules{BS}{defptr}{$dev} = $hash;
  48. AssignIoPort($hash);
  49. }
  50. #############################
  51. sub
  52. BS_Undef($$)
  53. {
  54. my ($hash, $name) = @_;
  55. delete($modules{BS}{defptr}{$hash->{DEF}});
  56. return undef;
  57. }
  58. #############################
  59. sub
  60. BS_Parse($$)
  61. {
  62. my ($hash, $msg) = @_; # hash points to the FHZ, not to the BS
  63. # Msg format:
  64. # 01 23 45 67 8901 2345 6789 01 23 45 67
  65. # 81 0c 04 .. 0101 a001 a5cf xx 00 zz zz
  66. my $sensor= substr($msg, 20, 2);
  67. my $dev= "a5cf $sensor";
  68. my $def= $modules{BS}{defptr}{$dev};
  69. if(!defined($def)) {
  70. $sensor =~ s/^0//;
  71. Log3 $hash, 3, "BS Unknown device $sensor, please define it";
  72. return "UNDEFINED BS_$sensor BS $sensor";
  73. }
  74. my $name= $def->{NAME};
  75. return "" if(IsIgnored($name));
  76. my $t= TimeNow();
  77. my $flags= hex(substr($msg, 24, 1)) & 0xdc;
  78. my $value= hex(substr($msg, 25, 3)) & 0x3ff;
  79. my $RExt= $def->{RExt};
  80. my $luxOffset= $def->{luxOffset};
  81. my $brightness= $value/10.24; # Vout in percent of reference voltage 1.1V
  82. # brightness in lux= 100lux*(VOut/RExt/1.8muA)^2;
  83. my $VOut= $value*1.1/1024.0;
  84. my $temp= $VOut/$RExt/1.8E-6;
  85. my $lux= 100.0*$temp*$temp;
  86. $lux+= $luxOffset; # add lux offset
  87. my $state= sprintf("brightness: %.2f lux: %.0f flags: %d",
  88. $brightness, $lux, $flags);
  89. readingsBeginUpdate($def);
  90. readingsBulkUpdate($def, "state", $state);
  91. #Debug "BS $name: $state";
  92. readingsBulkUpdate($def, "brightness", $brightness);
  93. readingsBulkUpdate($def, "lux", $lux);
  94. readingsBulkUpdate($def, "flags", $flags);
  95. readingsEndUpdate($def, 1);
  96. return $name;
  97. }
  98. #############################
  99. 1;
  100. =pod
  101. =item summary BS brightness sensor communicating over FHZ
  102. =item summary_DE BS Helligkeitssenor angebunden &uuml;ber FHZ
  103. =begin html
  104. <a name="BS"></a>
  105. <h3>BS</h3>
  106. <ul>
  107. The module BS allows to collect data from a brightness sensor through a
  108. <a href="#FHZ">FHZ</a> device. For details on the brightness sensor see
  109. <a href="http://www.busware.de/tiki-index.php?page=CPM-BS">busware wiki</a>.
  110. You can have at most nine different brightness sensors in range of your
  111. FHZ.<br>
  112. <br>
  113. The state contains the brightness in % (reading <code>brightness</code>) and
  114. the brightness in lux (reading <code>lux</code>). The <code>flags</code>
  115. reading is always zero. The meaning of these readings is explained in more
  116. detail on the above mentioned wiki page.<br>
  117. <br>
  118. <a name="BSDefine"></a>
  119. <b>Define</b>
  120. <ul>
  121. <code>define &lt;name&gt; BS &lt;sensor#&gt; [&lt;RExt&gt;]</code>
  122. <br><br>
  123. <code>&lt;sensor#&gt;</code> is the number of sensor in the brightness
  124. sensor address system that runs from 1 to 9.<br>
  125. <br>
  126. <code>&lt;RExt&gt;</code> is the value of the resistor on your brightness
  127. sensor in &Omega; (Ohm). The brightness reading in % is proportional to the resistance, the
  128. lux reading is proportional to the resistance squared. The value is
  129. optional. The default resistance is RExt= 50.000&Omega;.<br>
  130. <br>
  131. Example:<br>
  132. <ul>
  133. <code>define bs1 BS 1 40000</code><br>
  134. </ul>
  135. </ul>
  136. <br>
  137. <a name="BSset"></a>
  138. <b>Set </b>
  139. <ul>
  140. N/A
  141. </ul>
  142. <br>
  143. <a name="BSget"></a>
  144. <b>Get</b>
  145. <ul>
  146. N/A
  147. </ul>
  148. <br>
  149. <a name="BSattr"></a>
  150. <b>Attributes</b>
  151. <ul>
  152. <li><a href="#do_not_notify">do_not_notify</a></li>
  153. <li><a href="#showtime">showtime</a></li>
  154. <li><a href="#model">model</a> (bs)</li>
  155. <li><a href="#ignore">ignore</a></li>
  156. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  157. </ul>
  158. <br>
  159. </ul>
  160. =end html
  161. =cut