20_FRM_AD.pm 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ########################################################################################
  2. #
  3. # $Id: 20_FRM_AD.pm 15932 2018-01-19 21:19:00Z jensb $
  4. #
  5. # FHEM module for one Firmata analog input pin
  6. #
  7. ########################################################################################
  8. #
  9. # LICENSE AND COPYRIGHT
  10. #
  11. # Copyright (C) 2013 ntruchess
  12. # Copyright (C) 2016 jensb
  13. #
  14. # All rights reserved
  15. #
  16. # This script is free software; you can redistribute it and/or modify
  17. # it under the terms of the GNU General Public License as published by
  18. # the Free Software Foundation; either version 2 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # The GNU General Public License can be found at
  22. # http://www.gnu.org/copyleft/gpl.html.
  23. # A copy is found in the textfile GPL.txt and important notices to the license
  24. # from the author is found in LICENSE.txt distributed with these scripts.
  25. #
  26. # This script is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU General Public License for more details.
  30. #
  31. # This copyright notice MUST APPEAR in all copies of the script!
  32. #
  33. ########################################################################################
  34. package main;
  35. use strict;
  36. use warnings;
  37. #add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
  38. BEGIN {
  39. if (!grep(/FHEM\/lib$/,@INC)) {
  40. foreach my $inc (grep(/FHEM$/,@INC)) {
  41. push @INC,$inc."/lib";
  42. };
  43. };
  44. };
  45. use Device::Firmata::Constants qw/ :all /;
  46. #####################################
  47. my %gets = (
  48. "reading" => "",
  49. "state" => "",
  50. "alarm-upper-threshold" => "off",
  51. "alarm-lower-threshold" => "off",
  52. );
  53. sub
  54. FRM_AD_Initialize($)
  55. {
  56. my ($hash) = @_;
  57. $hash->{AttrFn} = "FRM_AD_Attr";
  58. $hash->{GetFn} = "FRM_AD_Get";
  59. $hash->{DefFn} = "FRM_Client_Define";
  60. $hash->{InitFn} = "FRM_AD_Init";
  61. $hash->{AttrList} = "IODev upper-threshold lower-threshold $main::readingFnAttributes";
  62. main::LoadModule("FRM");
  63. }
  64. sub
  65. FRM_AD_Init($$)
  66. {
  67. my ($hash,$args) = @_;
  68. my $ret = FRM_Init_Pin_Client($hash,$args,PIN_ANALOG);
  69. return $ret if (defined $ret);
  70. my $firmata = $hash->{IODev}->{FirmataDevice};
  71. my $name = $hash->{NAME};
  72. my $resolution = 10;
  73. if (defined $firmata->{metadata}{analog_resolutions}) {
  74. $resolution = $firmata->{metadata}{analog_resolutions}{$hash->{PIN}}
  75. }
  76. $hash->{resolution} = $resolution;
  77. $hash->{".max"} = defined $resolution ? (1<<$resolution)-1 : 1024;
  78. eval {
  79. $firmata->observe_analog($hash->{PIN},\&FRM_AD_observer,$hash);
  80. };
  81. return FRM_Catch($@) if $@;
  82. if (! (defined AttrVal($name,"stateFormat",undef))) {
  83. $main::attr{$name}{"stateFormat"} = "reading";
  84. }
  85. if (! (defined AttrVal($name,"event-min-interval",undef))) {
  86. $main::attr{$name}{"event-min-interval"} = 5;
  87. }
  88. main::readingsSingleUpdate($hash,"state","Initialized",1);
  89. return undef;
  90. }
  91. sub
  92. FRM_AD_observer
  93. {
  94. my ($pin,$old,$new,$hash) = @_;
  95. my $name = $hash->{NAME};
  96. Log3 $name,5,"onAnalogMessage for pin ".$pin.", old: ".(defined $old ? $old : "--").", new: ".(defined $new ? $new : "--");
  97. main::readingsBeginUpdate($hash);
  98. main::readingsBulkUpdate($hash,"reading",$new,1);
  99. my $upperthresholdalarm = ReadingsVal($name,"alarm-upper-threshold","off");
  100. if ( $new < AttrVal($name,"upper-threshold",$hash->{".max"}) ) {
  101. if ( $upperthresholdalarm eq "on" ) {
  102. main::readingsBulkUpdate($hash,"alarm-upper-threshold","off",1);
  103. }
  104. my $lowerthresholdalarm = ReadingsVal($name,"alarm-lower-threshold","off");
  105. if ( $new > AttrVal($name,"lower-threshold",-1) ) {
  106. if ( $lowerthresholdalarm eq "on" ) {
  107. main::readingsBulkUpdate($hash,"alarm-lower-threshold","off",1);
  108. }
  109. } else {
  110. if ( $lowerthresholdalarm eq "off" ) {
  111. main::readingsBulkUpdate($hash,"alarm-lower-threshold","on",1);
  112. }
  113. }
  114. } else {
  115. if ( $upperthresholdalarm eq "off" ) {
  116. main::readingsBulkUpdate($hash,"alarm-upper-threshold","on",1);
  117. }
  118. };
  119. main::readingsEndUpdate($hash,1);
  120. }
  121. sub
  122. FRM_AD_Get($)
  123. {
  124. my ($hash,@a) = @_;
  125. my $name = shift @a;
  126. my $cmd = shift @a;
  127. my $ret;
  128. ARGUMENT_HANDLER: {
  129. $cmd eq "reading" and do {
  130. eval {
  131. return FRM_Client_FirmataDevice($hash)->analog_read($hash->{PIN});
  132. };
  133. return $@;
  134. };
  135. ( $cmd eq "alarm-upper-threshold" or $cmd eq "alarm-lower-threshold" or $cmd eq "state" ) and do {
  136. return main::ReadingsVal($name,"count",$gets{$cmd});
  137. };
  138. }
  139. return undef;
  140. }
  141. sub
  142. FRM_AD_Attr($$$$) {
  143. my ($command,$name,$attribute,$value) = @_;
  144. my $hash = $main::defs{$name};
  145. eval {
  146. if ($command eq "set") {
  147. ARGUMENT_HANDLER: {
  148. $attribute eq "IODev" and do {
  149. if ($main::init_done and (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value)) {
  150. FRM_Client_AssignIOPort($hash,$value);
  151. FRM_Init_Client($hash) if (defined ($hash->{IODev}));
  152. }
  153. last;
  154. };
  155. }
  156. }
  157. };
  158. if ($@) {
  159. $@ =~ /^(.*)( at.*FHEM.*)$/;
  160. $hash->{STATE} = "error setting $attribute to $value: ".$1;
  161. return "cannot $command attribute $attribute to $value for $name: ".$1;
  162. }
  163. }
  164. 1;
  165. =pod
  166. CHANGES
  167. 2016 jensb
  168. o modified sub FRM_AD_Init to catch exceptions and return error message
  169. 19.01.2018 jensb
  170. o support analog resolution depending on device capability
  171. =cut
  172. =pod
  173. =item device
  174. =item summary Firmata: analog input
  175. =item summary_DE Firmata: analog Eingang
  176. =begin html
  177. <a name="FRM_AD"></a>
  178. <h3>FRM_AD</h3>
  179. <ul>
  180. This module represents a pin of a <a href="http://www.firmata.org">Firmata device</a>
  181. that should be configured as an analog input.<br><br>
  182. Requires a defined <a href="#FRM">FRM</a> device to work. The pin must be listed in the internal reading "<a href="#FRMinternals">analog_pins</a>"<br>
  183. of the FRM device (after connecting to the Firmata device) to be used as analog input.<br><br>
  184. <a name="FRM_ADdefine"></a>
  185. <b>Define</b>
  186. <ul>
  187. <code>define &lt;name&gt; FRM_AD &lt;pin&gt;</code><br><br>
  188. Defines the FRM_AD device. &lt;pin&gt; is the arduino-pin to use.
  189. </ul><br>
  190. <a name="FRM_ADset"></a>
  191. <b>Set</b><br>
  192. <ul>
  193. N/A<br>
  194. </ul><br>
  195. <a name="FRM_ADget"></a>
  196. <b>Get</b><br>
  197. <ul>
  198. <li>reading<br>
  199. returns the voltage-level equivalent at the arduino-pin. The min value is zero and the max value<br>
  200. depends on the Firmata device (see internal reading "<a href="#FRMinternals">analog_resolutions</a>" of the FRM device.<br>
  201. For 10 bits resolution the range is 0 to 1023 (also see <a href="http://arduino.cc/en/Reference/AnalogRead">analogRead()</a> for details)<br></li>
  202. <li>alarm-upper-threshold<br>
  203. returns the current state of 'alarm-upper-threshold'. Values are 'on' and 'off' (Defaults to 'off')<br>
  204. 'alarm-upper-threshold' turns 'on' whenever the 'reading' is higher than the attribute 'upper-threshold'<br>
  205. it turns 'off' again as soon 'reading' falls below 'alarm-upper-threshold'</li>
  206. <li>alarm-lower-threshold<br>
  207. returns the current state of 'alarm-lower-threshold'. Values are 'on' and 'off' (Defaults to 'off')<br>
  208. 'alarm-lower-threshold' turns 'on' whenever the 'reading' is lower than the attribute 'lower-threshold'<br>
  209. it turns 'off' again as soon 'reading rises above 'alarm-lower-threshold'</li>
  210. <li>state<br>
  211. returns the 'state' reading</li>
  212. </ul><br>
  213. <a name="FRM_ADattr"></a>
  214. <b>Attributes</b><br>
  215. <ul>
  216. <li>upper-threshold<br>
  217. sets the 'upper-threshold'. Whenever the 'reading' exceeds this value 'alarm-upper-threshold' is set to 'on'<br>
  218. As soon 'reading' falls below the 'upper-threshold' 'alarm-upper-threshold' turns 'off' again<br>
  219. Defaults to the max pin resolution plus one.</li>
  220. <li>lower-threshold<br>
  221. sets the 'lower-threshold'. Whenever the 'reading' falls below this value 'alarm-lower-threshold' is set to 'on'<br>
  222. As soon 'reading' rises above the 'lower-threshold' 'alarm-lower-threshold' turns 'off' again<br>
  223. Defaults to -1.</li>
  224. <li><a href="#IODev">IODev</a><br>
  225. Specify which <a href="#FRM">FRM</a> to use. (Optional, only required if there is more
  226. than one FRM-device defined.)
  227. </li>
  228. <li><a href="#eventMap">eventMap</a><br></li>
  229. <li><a href="#readingFnAttributes">readingFnAttributes</a><br></li>
  230. </ul><br>
  231. <a name="FRM_ADnotes"></a>
  232. <b>Notes</b><br>
  233. <ul>
  234. <li>attribute <i>stateFormat</i><br>
  235. In most cases it is a good idea to assign "reading" to the attribute <i>stateFormat</i>. This will show the
  236. current value of the pin in the web interface.
  237. </li>
  238. </ul>
  239. </ul><br>
  240. =end html
  241. =cut