10_FRM.pm 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. ##############################################
  2. # $Id: 10_FRM.pm 11728 2016-07-01 22:20:30Z klauswitt $
  3. ##############################################
  4. package main;
  5. use vars qw{%attr %defs};
  6. use strict;
  7. use warnings;
  8. use GPUtils qw(:all);
  9. #add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
  10. BEGIN {
  11. if (!grep(/FHEM\/lib$/,@INC)) {
  12. foreach my $inc (grep(/FHEM$/,@INC)) {
  13. push @INC,$inc."/lib";
  14. };
  15. };
  16. };
  17. use Device::Firmata::Constants qw/ :all /;
  18. use Device::Firmata::Protocol;
  19. use Device::Firmata::Platform;
  20. sub FRM_Set($@);
  21. sub FRM_Attr(@);
  22. my %sets = (
  23. "reset" => "",
  24. "reinit" => ""
  25. );
  26. my %gets = (
  27. "firmware" => "",
  28. "version" => ""
  29. );
  30. my @clients = qw(
  31. FRM_IN
  32. FRM_OUT
  33. FRM_AD
  34. FRM_PWM
  35. FRM_I2C
  36. FRM_SERVO
  37. FRM_RGB
  38. FRM_ROTENC
  39. FRM_STEPPER
  40. OWX
  41. OWX_ASYNC
  42. I2C_LCD
  43. I2C_DS1307
  44. I2C_PC.*
  45. I2C_MCP23.*
  46. I2C_SHT.*
  47. I2C_BME280
  48. I2C_BMP180
  49. I2C_BH1750
  50. I2C_TSL2561
  51. FRM_LCD
  52. I2C_K30
  53. );
  54. #####################################
  55. sub FRM_Initialize($) {
  56. my $hash = shift @_;
  57. require "$main::attr{global}{modpath}/FHEM/DevIo.pm";
  58. # Provider
  59. $hash->{Clients} = join (':',@clients);
  60. $hash->{ReadyFn} = "FRM_Ready";
  61. $hash->{ReadFn} = "FRM_Read";
  62. $hash->{I2CWrtFn} = "FRM_I2C_Write";
  63. # Consumer
  64. $hash->{DefFn} = "FRM_Define";
  65. $hash->{UndefFn} = "FRM_Undef";
  66. $hash->{GetFn} = "FRM_Get";
  67. $hash->{SetFn} = "FRM_Set";
  68. $hash->{AttrFn} = "FRM_Attr";
  69. $hash->{NotifyFn} = "FRM_Notify";
  70. $hash->{AttrList} = "model:nano dummy:1,0 sampling-interval i2c-config $main::readingFnAttributes";
  71. }
  72. #####################################
  73. sub FRM_Define($$) {
  74. my ( $hash, $def ) = @_;
  75. my ($name, $type, $dev, $global) = split("[ \t]+", $def);
  76. $hash->{DeviceName} = $dev;
  77. $hash->{NOTIFYDEV} = "global";
  78. if ( $dev eq "none" ) {
  79. Log3 $name,3,"device is none, commands will be echoed only";
  80. $main::attr{$name}{dummy} = 1;
  81. }
  82. if ($main::init_done) {
  83. return FRM_Start($hash);
  84. }
  85. return undef;
  86. }
  87. #####################################
  88. sub FRM_Undef($) {
  89. my $hash = shift;
  90. FRM_forall_clients($hash,\&FRM_Client_Unassign,undef);
  91. if (defined $hash->{DeviceName}) {
  92. DevIo_Disconnected($hash);
  93. };
  94. TcpServer_Close($hash);
  95. foreach my $d ( sort keys %main::defs ) { # close and dispose open tcp-connection (if any) to free open filedescriptors
  96. if ( defined( my $dev = $main::defs{$d} )) {
  97. if ( defined( $main::defs{$d}{SNAME} )
  98. && $main::defs{$d}{SNAME} eq $hash->{NAME}) {
  99. FRM_Tcp_Connection_Close($main::defs{$d});
  100. }
  101. }
  102. }
  103. FRM_FirmataDevice_Close($hash);
  104. return undef;
  105. }
  106. sub FRM_Start {
  107. my ($hash) = @_;
  108. my ($dev, $global) = split("[ \t]+", $hash->{DEF});
  109. $hash->{DeviceName} = $dev;
  110. my $isServer = 1 if($dev && $dev =~ m/^(IPV6:)?\d+$/);
  111. # my $isClient = 1 if($dev && $dev =~ m/^(IPV6:)?.*:\d+$/);
  112. # return "Usage: define <name> FRM {<device>[@<baudrate>] | [IPV6:]<tcp-portnr> [global]}"
  113. # if(!($isServer || $isClient) ||
  114. # ($isClient && $global) ||
  115. # ($global && $global ne "global"));
  116. # Make sure that fhem only runs once
  117. if($isServer) {
  118. my $ret = TcpServer_Open($hash, $dev, $global);
  119. if (!$ret) {
  120. $hash->{STATE}="listening";
  121. }
  122. return $ret;
  123. }
  124. DevIo_CloseDev($hash);
  125. my $ret = DevIo_OpenDev($hash, 0, "FRM_DoInit");
  126. return $ret;
  127. }
  128. sub FRM_Notify {
  129. my ($hash,$dev) = @_;
  130. my $name = $hash->{NAME};
  131. my $type = $hash->{TYPE};
  132. if( grep(m/^(INITIALIZED|REREADCFG)$/, @{$dev->{CHANGED}}) ) {
  133. FRM_Start($hash);
  134. } elsif( grep(m/^SAVE$/, @{$dev->{CHANGED}}) ) {
  135. }
  136. }
  137. #####################################
  138. sub FRM_is_firmata_connected {
  139. my ($hash) = @_;
  140. return defined($hash->{FirmataDevice}) && defined ($hash->{FirmataDevice}->{io});
  141. }
  142. #####################################
  143. sub FRM_Set($@) {
  144. my ($hash, @a) = @_;
  145. return "Need at least one parameters" if(@a < 2);
  146. return "Unknown argument $a[1], choose one of " . join(" ", sort keys %sets)
  147. if(!defined($sets{$a[1]}));
  148. my $command = $a[1];
  149. my $value = $a[2];
  150. COMMAND_HANDLER: {
  151. $command eq "reset" and do {
  152. return $hash->{NAME}." is not connected" unless (FRM_is_firmata_connected($hash) && (defined $hash->{FD} or ($^O=~/Win/ and defined $hash->{USBDev})));
  153. $hash->{FirmataDevice}->system_reset();
  154. if (defined $hash->{SERVERSOCKET}) {
  155. # dispose preexisting connections
  156. foreach my $e ( sort keys %main::defs ) {
  157. if ( defined( my $dev = $main::defs{$e} )) {
  158. if ( defined( $dev->{SNAME} ) && ( $dev->{SNAME} eq $hash->{NAME} )) {
  159. FRM_Tcp_Connection_Close($dev);
  160. }
  161. }
  162. }
  163. FRM_FirmataDevice_Close($hash);
  164. last;
  165. } else {
  166. DevIo_CloseDev($hash);
  167. FRM_FirmataDevice_Close($hash);
  168. return DevIo_OpenDev($hash, 0, "FRM_DoInit");
  169. }
  170. };
  171. $command eq "reinit" and do {
  172. FRM_forall_clients($hash,\&FRM_Init_Client,undef);
  173. last;
  174. };
  175. }
  176. return undef;
  177. }
  178. #####################################
  179. sub FRM_Get($@) {
  180. my ($hash, @a) = @_;
  181. return "Need at least one parameters" if(@a < 2);
  182. return "Unknown argument $a[1], choose one of " . join(" ", sort keys %gets)
  183. if(!defined($gets{$a[1]}));
  184. my $name = shift @a;
  185. my $cmd = shift @a;
  186. ARGUMENT_HANDLER: {
  187. $cmd eq "firmware" and do {
  188. if (FRM_is_firmata_connected($hash)) {
  189. return $hash->{FirmataDevice}->{metadata}->{firmware};
  190. } else {
  191. return "not connected to FirmataDevice";
  192. }
  193. };
  194. $cmd eq "version" and do {
  195. if (FRM_is_firmata_connected($hash)) {
  196. return $hash->{FirmataDevice}->{metadata}->{firmware_version};
  197. } else {
  198. return "not connected to FirmataDevice";
  199. }
  200. };
  201. }
  202. }
  203. #####################################
  204. # called from the global loop, when the select for hash->{FD} reports data
  205. sub FRM_Read($) {
  206. my ( $hash ) = @_;
  207. if($hash->{SERVERSOCKET}) { # Accept and create a child
  208. my $chash = TcpServer_Accept($hash, "FRM");
  209. return if(!$chash);
  210. $chash->{DeviceName}=$hash->{PORT}; # required for DevIo_CloseDev and FRM_Ready
  211. $chash->{TCPDev}=$chash->{CD};
  212. # dispose preexisting connections
  213. foreach my $e ( sort keys %main::defs ) {
  214. if ( defined( my $dev = $main::defs{$e} )) {
  215. if ( $dev != $chash && defined( $dev->{SNAME} ) && ( $dev->{SNAME} eq $chash->{SNAME} )) {
  216. FRM_Tcp_Connection_Close($dev);
  217. }
  218. }
  219. }
  220. FRM_FirmataDevice_Close($hash);
  221. FRM_DoInit($chash);
  222. return;
  223. }
  224. my $device = $hash->{FirmataDevice} or return;
  225. $device->poll();
  226. }
  227. sub FRM_Ready($) {
  228. my ($hash) = @_;
  229. my $name = $hash->{NAME};
  230. if ($name=~/^^FRM:.+:\d+$/) { # this is a closed tcp-connection, remove it
  231. FRM_Tcp_Connection_Close($hash);
  232. FRM_FirmataDevice_Close($hash);
  233. }
  234. return DevIo_OpenDev($hash, 1, "FRM_DoInit") if($hash->{STATE} eq "disconnected");
  235. # This is relevant for windows/USB only
  236. my $po = $hash->{USBDev};
  237. my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags);
  238. if($po) {
  239. ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags) = $po->status;
  240. }
  241. return ($InBytes && $InBytes>0);
  242. }
  243. sub FRM_Tcp_Connection_Close($) {
  244. my $hash = shift;
  245. TcpServer_Close($hash);
  246. if ($hash->{SNAME}) {
  247. my $shash = $main::defs{$hash->{SNAME}};
  248. if (defined $shash) {
  249. $shash->{STATE}="listening";
  250. delete $shash->{SocketDevice} if (defined $shash->{SocketDevice});
  251. }
  252. }
  253. my $dev = $hash->{DeviceName};
  254. my $name = $hash->{NAME};
  255. if (defined $name) {
  256. delete $main::readyfnlist{"$name.$dev"} if (defined $dev);
  257. delete $main::attr{$name};
  258. delete $main::defs{$name};
  259. }
  260. return undef;
  261. }
  262. sub FRM_FirmataDevice_Close($) {
  263. my $hash = shift;
  264. my $device = $hash->{FirmataDevice};
  265. if (defined $device) {
  266. if (defined $device->{io}) {
  267. delete $hash->{FirmataDevice}->{io}->{handle} if defined $hash->{FirmataDevice}->{io}->{handle};
  268. delete $hash->{FirmataDevice}->{io};
  269. }
  270. delete $device->{protocol} if defined $device->{protocol};
  271. delete $hash->{FirmataDevice};
  272. }
  273. }
  274. sub FRM_Attr(@) {
  275. my ($command,$name,$attribute,$value) = @_;
  276. if ($command eq "set") {
  277. $main::attr{$name}{$attribute}=$value;
  278. if ($attribute eq "sampling-interval"
  279. or $attribute eq "i2c-config") {
  280. FRM_apply_attribute($main::defs{$name},$attribute);
  281. }
  282. }
  283. }
  284. sub FRM_apply_attribute {
  285. my ($hash,$attribute) = @_;
  286. my $firmata = $hash->{FirmataDevice};
  287. my $name = $hash->{NAME};
  288. if (defined $firmata) {
  289. if ($attribute eq "sampling-interval") {
  290. $firmata->sampling_interval(AttrVal($name,$attribute,"1000"));
  291. } elsif ($attribute eq "i2c-config") {
  292. my $i2cattr = AttrVal($name,$attribute,undef);
  293. if (defined $i2cattr) {
  294. my @a = split(" ", $i2cattr);
  295. my $i2cpins = $firmata->{metadata}{i2c_pins};
  296. my $err;
  297. if (defined $i2cpins and scalar @$i2cpins) {
  298. eval {
  299. foreach my $i2cpin (@$i2cpins) {
  300. $firmata->pin_mode($i2cpin,PIN_I2C);
  301. }
  302. $firmata->i2c_config(@a);
  303. $firmata->observe_i2c(\&FRM_i2c_observer,$hash);
  304. };
  305. $err = $@ if ($@);
  306. } else {
  307. $err = "Error, arduino doesn't support I2C";
  308. }
  309. Log3 $name,2,$err if ($err);
  310. }
  311. }
  312. }
  313. }
  314. sub FRM_DoInit($) {
  315. my ($hash) = @_;
  316. my $sname = $hash->{SNAME}; #is this a serversocket-connection?
  317. my $shash = defined $sname ? $main::defs{$sname} : $hash;
  318. my $name = $shash->{NAME};
  319. my $firmata_io = Firmata_IO->new($hash,$name);
  320. my $device = Device::Firmata::Platform->attach($firmata_io) or return 1;
  321. $shash->{FirmataDevice} = $device;
  322. if (defined $sname) {
  323. $shash->{SocketDevice} = $hash;
  324. #as FRM_Read gets the connected socket hash, but calls firmatadevice->poll():
  325. $hash->{FirmataDevice} = $device;
  326. }
  327. $device->observe_string(\&FRM_string_observer,$shash);
  328. my $found; # we cannot call $device->probe() here, as it doesn't select bevore read, so it would likely cause IODev to close the connection on the first attempt to read from empty stream
  329. my $endTicks = time+5;
  330. my $queryTicks = time+2;
  331. $device->system_reset();
  332. do {
  333. FRM_poll($shash);
  334. if ($device->{metadata}{firmware} && $device->{metadata}{firmware_version}) {
  335. $device->{protocol}->{protocol_version} = $device->{metadata}{firmware_version};
  336. $main::defs{$name}{firmware} = $device->{metadata}{firmware};
  337. $main::defs{$name}{firmware_version} = $device->{metadata}{firmware_version};
  338. Log3 $name,3,"Firmata Firmware Version: ".$device->{metadata}{firmware}." ".$device->{metadata}{firmware_version};
  339. $device->analog_mapping_query();
  340. $device->capability_query();
  341. do {
  342. FRM_poll($shash);
  343. if ($device->{metadata}{analog_mappings} and $device->{metadata}{capabilities}) {
  344. my $inputpins = $device->{metadata}{input_pins};
  345. $main::defs{$name}{input_pins} = join(",", sort{$a<=>$b}(@$inputpins)) if (defined $inputpins and scalar @$inputpins);
  346. my $outputpins = $device->{metadata}{output_pins};
  347. $main::defs{$name}{output_pins} = join(",", sort{$a<=>$b}(@$outputpins)) if (defined $outputpins and scalar @$outputpins);
  348. my $analogpins = $device->{metadata}{analog_pins};
  349. $main::defs{$name}{analog_pins} = join(",", sort{$a<=>$b}(@$analogpins)) if (defined $analogpins and scalar @$analogpins);
  350. my $pwmpins = $device->{metadata}{pwm_pins};
  351. $main::defs{$name}{pwm_pins} = join(",", sort{$a<=>$b}(@$pwmpins)) if (defined $pwmpins and scalar @$pwmpins);
  352. my $servopins = $device->{metadata}{servo_pins};
  353. $main::defs{$name}{servo_pins} = join(",", sort{$a<=>$b}(@$servopins)) if (defined $servopins and scalar @$servopins);
  354. my $i2cpins = $device->{metadata}{i2c_pins};
  355. $main::defs{$name}{i2c_pins} = join(",", sort{$a<=>$b}(@$i2cpins)) if (defined $i2cpins and scalar @$i2cpins);
  356. my $onewirepins = $device->{metadata}{onewire_pins};
  357. $main::defs{$name}{onewire_pins} = join(",", sort{$a<=>$b}(@$onewirepins)) if (defined $onewirepins and scalar @$onewirepins);
  358. my $encoderpins = $device->{metadata}{encoder_pins};
  359. $main::defs{$name}{encoder_pins} = join(",", sort{$a<=>$b}(@$encoderpins)) if (defined $encoderpins and scalar @$encoderpins);
  360. my $stepperpins = $device->{metadata}{stepper_pins};
  361. $main::defs{$name}{stepper_pins} = join(",", sort{$a<=>$b}(@$stepperpins)) if (defined $stepperpins and scalar @$stepperpins);
  362. if (defined $device->{metadata}{analog_resolutions}) {
  363. my @analog_resolutions;
  364. foreach my $pin (sort{$a<=>$b}(keys %{$device->{metadata}{analog_resolutions}})) {
  365. push @analog_resolutions,$pin.":".$device->{metadata}{analog_resolutions}{$pin};
  366. }
  367. $main::defs{$name}{analog_resolutions} = join(",",@analog_resolutions) if (scalar @analog_resolutions);
  368. }
  369. if (defined $device->{metadata}{pwm_resolutions}) {
  370. my @pwm_resolutions;
  371. foreach my $pin (sort{$a<=>$b}(keys %{$device->{metadata}{pwm_resolutions}})) {
  372. push @pwm_resolutions,$pin.":".$device->{metadata}{pwm_resolutions}{$pin};
  373. }
  374. $main::defs{$name}{pwm_resolutions} = join(",",@pwm_resolutions) if (scalar @pwm_resolutions);
  375. }
  376. if (defined $device->{metadata}{servo_resolutions}) {
  377. my @servo_resolutions;
  378. foreach my $pin (sort{$a<=>$b}(keys %{$device->{metadata}{servo_resolutions}})) {
  379. push @servo_resolutions,$pin.":".$device->{metadata}{servo_resolutions}{$pin};
  380. }
  381. $main::defs{$name}{servo_resolutions} = join(",",@servo_resolutions) if (scalar @servo_resolutions);
  382. }
  383. if (defined $device->{metadata}{encoder_resolutions}) {
  384. my @encoder_resolutions;
  385. foreach my $pin (sort{$a<=>$b}(keys %{$device->{metadata}{encoder_resolutions}})) {
  386. push @encoder_resolutions,$pin.":".$device->{metadata}{encoder_resolutions}{$pin};
  387. }
  388. $main::defs{$name}{encoder_resolutions} = join(",",@encoder_resolutions) if (scalar @encoder_resolutions);
  389. }
  390. if (defined $device->{metadata}{stepper_resolutions}) {
  391. my @stepper_resolutions;
  392. foreach my $pin (sort{$a<=>$b}(keys %{$device->{metadata}{stepper_resolutions}})) {
  393. push @stepper_resolutions,$pin.":".$device->{metadata}{stepper_resolutions}{$pin};
  394. }
  395. $main::defs{$name}{stepper_resolutions} = join(",",@stepper_resolutions) if (scalar @stepper_resolutions);
  396. }
  397. $found = 1;
  398. } else {
  399. select (undef,undef,undef,0.01);
  400. }
  401. } while (time < $endTicks and !$found);
  402. $found = 1;
  403. } else {
  404. select (undef,undef,undef,0.01);
  405. if (time > $queryTicks) {
  406. Log3 $name,3,"querying Firmata Firmware Version";
  407. $device->firmware_version_query();
  408. $queryTicks++;
  409. }
  410. }
  411. } while (time < $endTicks and !$found);
  412. if ($found) {
  413. FRM_apply_attribute($shash,"sampling-interval");
  414. FRM_apply_attribute($shash,"i2c-config");
  415. FRM_forall_clients($shash,\&FRM_Init_Client,undef);
  416. $shash->{STATE}="Initialized";
  417. return undef;
  418. }
  419. Log3 $name,3,"no response from Firmata, closing DevIO";
  420. DevIo_Disconnected($shash);
  421. delete $shash->{FirmataDevice};
  422. delete $shash->{SocketDevice};
  423. return "FirmataDevice not responding";
  424. }
  425. sub
  426. FRM_forall_clients($$$)
  427. {
  428. my ($hash,$fn,$args) = @_;
  429. foreach my $d ( sort keys %main::defs ) {
  430. if ( defined( $main::defs{$d} )
  431. && defined( $main::defs{$d}{IODev} )
  432. && $main::defs{$d}{IODev} == $hash ) {
  433. &$fn($main::defs{$d},$args);
  434. }
  435. }
  436. return undef;
  437. }
  438. sub
  439. FRM_Init_Client($@) {
  440. my ($hash,$args) = @_;
  441. if (!defined $args and defined $hash->{DEF}) {
  442. my @a = split("[ \t][ \t]*", $hash->{DEF});
  443. $args = \@a;
  444. }
  445. my $name = $hash->{NAME};
  446. my $ret = CallFn($name,"InitFn",$hash,$args);
  447. if ($ret) {
  448. Log3 $name,2,"error initializing '".$hash->{NAME}."': ".$ret;
  449. }
  450. }
  451. sub
  452. FRM_Init_Pin_Client($$$) {
  453. my ($hash,$args,$mode) = @_;
  454. my $u = "wrong syntax: define <name> FRM_XXX pin";
  455. return $u unless defined $args and int(@$args) > 0;
  456. my $pin = @$args[0];
  457. $hash->{PIN} = $pin;
  458. eval {
  459. FRM_Client_AssignIOPort($hash);
  460. FRM_Client_FirmataDevice($hash)->pin_mode($pin,$mode);
  461. };
  462. if ($@) {
  463. $@ =~ /^(.*)( at.*FHEM.*)$/;
  464. $hash->{STATE} = "error initializing: ".$1;
  465. return $1;
  466. }
  467. return undef;
  468. }
  469. sub
  470. FRM_Client_Define($$)
  471. {
  472. my ($hash, $def) = @_;
  473. my @a = split("[ \t][ \t]*", $def);
  474. $hash->{STATE}="defined";
  475. if ($main::init_done) {
  476. eval {
  477. FRM_Init_Client($hash,[@a[2..scalar(@a)-1]]);
  478. };
  479. if ($@) {
  480. $@ =~ /^(.*)( at.*FHEM.*)$/;
  481. return $1;
  482. }
  483. }
  484. return undef;
  485. }
  486. sub
  487. FRM_Client_Undef($$)
  488. {
  489. my ($hash, $name) = @_;
  490. my $pin = $hash->{PIN};
  491. eval {
  492. my $firmata = FRM_Client_FirmataDevice($hash);
  493. $firmata->pin_mode($pin,PIN_ANALOG);
  494. };
  495. if ($@) {
  496. eval {
  497. my $firmata = FRM_Client_FirmataDevice($hash);
  498. $firmata->pin_mode($pin,PIN_INPUT);
  499. $firmata->digital_write($pin,0);
  500. };
  501. }
  502. return undef;
  503. }
  504. sub
  505. FRM_Client_Unassign($)
  506. {
  507. my ($dev) = @_;
  508. delete $dev->{IODev} if defined $dev->{IODev};
  509. $dev->{STATE}="defined";
  510. }
  511. sub
  512. FRM_Client_AssignIOPort($@)
  513. {
  514. my ($hash,$iodev) = @_;
  515. my $name = $hash->{NAME};
  516. AssignIoPort($hash,defined $iodev ? $iodev : AttrVal($hash->{NAME},"IODev",undef));
  517. die "unable to assign IODev to '$name'" unless defined ($hash->{IODev});
  518. if (defined($hash->{IODev}->{SNAME})) {
  519. $hash->{IODev} = $main::defs{$hash->{IODev}->{SNAME}};
  520. $attr{$name}{IODev} = $hash->{IODev}{NAME};
  521. }
  522. foreach my $d ( sort keys %main::defs ) {
  523. if ( defined( my $dev = $main::defs{$d} )) {
  524. if ( $dev != $hash
  525. && defined( $dev->{IODev} )
  526. && defined( $dev->{PIN} )
  527. && $dev->{IODev} == $hash->{IODev}
  528. && defined( $hash->{PIN})
  529. && grep {$_ == $hash->{PIN}} split(" ",$dev->{PIN}) ) {
  530. delete $hash->{IODev};
  531. delete $attr{$name}{IODev};
  532. die "Device '$main::defs{$d}{NAME}' allready defined for pin $hash->{PIN}";
  533. }
  534. }
  535. }
  536. }
  537. sub FRM_Client_FirmataDevice($) {
  538. my $hash = shift;
  539. my $iodev = $hash->{IODev};
  540. die $hash->{NAME}." no IODev assigned" unless defined $iodev;
  541. die $hash->{NAME}.", ".$iodev->{NAME}." is not connected" unless (defined $iodev->{FirmataDevice} and (defined $iodev->{FD} or ($^O=~/Win/ and defined $iodev->{USBDev})));
  542. return $iodev->{FirmataDevice};
  543. }
  544. sub FRM_Catch($) {
  545. my $exception = shift;
  546. if ($exception) {
  547. $exception =~ /^(.*)( at.*FHEM.*)$/;
  548. return $1;
  549. }
  550. return undef;
  551. }
  552. package Firmata_IO;
  553. sub new($$) {
  554. my ($class,$hash,$name) = @_;
  555. return bless {
  556. hash => $hash,
  557. name => $name,
  558. }, $class;
  559. }
  560. sub data_write {
  561. my ( $self, $buf ) = @_;
  562. my $hash = $self->{hash};
  563. main::Log3 $self->{name},5,"FRM:>".unpack "H*",$buf;
  564. main::DevIo_SimpleWrite($hash,$buf,undef);
  565. }
  566. sub data_read {
  567. my ( $self, $bytes ) = @_;
  568. my $hash = $self->{hash};
  569. my $string = main::DevIo_SimpleRead($hash);
  570. if (defined $string ) {
  571. main::Log3 $self->{name},5,"FRM:<".unpack "H*",$string;
  572. }
  573. return $string;
  574. }
  575. package main;
  576. # im master muss eine I2CWrtFn definiert werden, diese wird vom client mit
  577. # CallFn(<mastername>, "I2CWrtFn", <masterhash>, \%sendpackage);
  578. # aufgerufen.
  579. # Der Master muss mit AssignIoPort() dem Client zugeordnet werden;
  580. # %sendpackage muss folgende keys enthalten:
  581. #
  582. # i2caddress => <xx>
  583. # direction => <i2cwrite|i2cread>
  584. # data => <xx [xx ...] (kann für read leer bleiben)>
  585. #
  586. # der Master fügt zu %sendpackage noch folgende keys hinzu:
  587. #
  588. # received (durch leerzeichen getrennte 1byte hexwerte)
  589. # mastername_* (alle mit mastername_ beginnenden keys können als internal im client angelegt weden)
  590. # unter anderem: mastername_SENDSTAT (enthält "Ok" wenn Übertragung erfolgreich)
  591. #
  592. # danach ruft er über:
  593. # CallFn(<clientname>, "I2CRecFn", <clienthash>, $sendpackage);
  594. # die I2CRecFn im client auf. Dort werden die Daten verarbeitet und
  595. # im Master wird der Hash sendpackage gelöscht.
  596. #
  597. # $package->{i2caddress}; # single byte value
  598. # $package->{direction}; # i2cread|i2cwrite
  599. # $package->{data}; # space separated list of values
  600. # $package->{reg}; # register
  601. # $package->{nbyte}; # number of bytes to read
  602. #
  603. # $firmata->i2c_read($address,$register,$bytestoread);
  604. # $firmata->i2c_write($address,@data);
  605. sub FRM_I2C_Write
  606. {
  607. my ($hash,$package) = @_;
  608. if (FRM_is_firmata_connected($hash)) {
  609. my $firmata = $hash->{FirmataDevice};
  610. COMMANDHANDLER: {
  611. $package->{direction} eq "i2cwrite" and do {
  612. if (defined $package->{reg}) {
  613. $firmata->i2c_write($package->{i2caddress},$package->{reg},split(" ",$package->{data}));
  614. } else {
  615. $firmata->i2c_write($package->{i2caddress},split(" ",$package->{data}));
  616. }
  617. last;
  618. };
  619. $package->{direction} eq "i2cread" and do {
  620. if (defined $package->{reg}) {
  621. $firmata->i2c_readonce($package->{i2caddress},$package->{reg},defined $package->{nbyte} ? $package->{nbyte} : 1);
  622. } else {
  623. $firmata->i2c_readonce($package->{i2caddress},defined $package->{nbyte} ? $package->{nbyte} : 1);
  624. }
  625. last;
  626. };
  627. }
  628. }
  629. }
  630. sub
  631. FRM_i2c_observer
  632. {
  633. my ($data,$hash) = @_;
  634. Log3 $hash->{NAME},5,"onI2CMessage address: '".$data->{address}."', register: '".$data->{register}."' data: [".(join(',',@{$data->{data}}))."]";
  635. FRM_forall_clients($hash,\&FRM_i2c_update_device,$data);
  636. }
  637. sub FRM_i2c_update_device
  638. {
  639. my ($hash,$data) = @_;
  640. if (defined $hash->{I2C_Address} and $hash->{I2C_Address} eq $data->{address}) {
  641. CallFn($hash->{NAME}, "I2CRecFn", $hash, {
  642. i2caddress => $data->{address},
  643. direction => "i2cread",
  644. reg => $data->{register},
  645. nbyte => scalar(@{$data->{data}}),
  646. received => join (' ',@{$data->{data}}),
  647. $hash->{IODev}->{NAME}."_SENDSTAT" => "Ok",
  648. });
  649. } elsif (defined $hash->{"i2c-address"} && $hash->{"i2c-address"}==$data->{address}) {
  650. my $replydata = $data->{data};
  651. my @values = split(" ",ReadingsVal($hash->{NAME},"values",""));
  652. splice(@values,$data->{register},@$replydata, @$replydata);
  653. readingsBeginUpdate($hash);
  654. $hash->{STATE}="active";
  655. readingsBulkUpdate($hash,"values",join (" ",@values),1);
  656. readingsEndUpdate($hash,1);
  657. }
  658. }
  659. sub FRM_string_observer
  660. {
  661. my ($string,$hash) = @_;
  662. Log3 $hash->{NAME},3,"received String_data: ".$string;
  663. readingsSingleUpdate($hash,"error",$string,1);
  664. }
  665. sub FRM_poll
  666. {
  667. my ($hash) = @_;
  668. if (defined $hash->{SocketDevice} and defined $hash->{SocketDevice}->{FD}) {
  669. my ($rout, $rin) = ('', '');
  670. vec($rin, $hash->{SocketDevice}->{FD}, 1) = 1;
  671. my $nfound = select($rout=$rin, undef, undef, 0.1);
  672. my $mfound = vec($rout, $hash->{SocketDevice}->{FD}, 1);
  673. if($mfound && FRM_is_firmata_connected($hash)) {
  674. $hash->{FirmataDevice}->poll();
  675. }
  676. return $mfound;
  677. } elsif (defined $hash->{FD}) {
  678. my ($rout, $rin) = ('', '');
  679. vec($rin, $hash->{FD}, 1) = 1;
  680. my $nfound = select($rout=$rin, undef, undef, 0.1);
  681. my $mfound = vec($rout, $hash->{FD}, 1);
  682. if($mfound && FRM_is_firmata_connected($hash)) {
  683. $hash->{FirmataDevice}->poll();
  684. }
  685. return $mfound;
  686. } else {
  687. # This is relevant for windows/USB only
  688. my $po = $hash->{USBDev};
  689. my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags);
  690. if($po) {
  691. ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags) = $po->status;
  692. }
  693. if ($InBytes && $InBytes>0 && FRM_is_firmata_connected($hash)) {
  694. $hash->{FirmataDevice}->poll();
  695. }
  696. }
  697. }
  698. ######### following is code to be called from OWX: ##########
  699. sub
  700. FRM_OWX_Init($$)
  701. {
  702. my ($hash,$args) = @_;
  703. my $ret = FRM_Init_Pin_Client($hash,$args,PIN_ONEWIRE);
  704. return $ret if (defined $ret);
  705. eval {
  706. my $firmata = FRM_Client_FirmataDevice($hash);
  707. my $pin = $hash->{PIN};
  708. $hash->{FRM_OWX_CORRELATIONID} = 0;
  709. $firmata->observe_onewire($pin,\&FRM_OWX_observer,$hash);
  710. $hash->{FRM_OWX_REPLIES} = {};
  711. $hash->{DEVS} = [];
  712. if ( AttrVal($hash->{NAME},"buspower","") eq "parasitic" ) {
  713. $firmata->onewire_config($pin,1);
  714. }
  715. };
  716. return GP_Catch($@) if ($@);
  717. $hash->{STATE}="Initialized";
  718. InternalTimer(gettimeofday()+10, "OWX_Discover", $hash,0);
  719. return undef;
  720. }
  721. sub FRM_OWX_observer
  722. {
  723. my ( $data,$hash ) = @_;
  724. my $command = $data->{command};
  725. COMMAND_HANDLER: {
  726. $command eq "READ_REPLY" and do {
  727. my $id = $data->{id};
  728. my $request = (defined $id) ? $hash->{FRM_OWX_REQUESTS}->{$id} : undef;
  729. unless (defined $request) {
  730. return unless (defined $data->{device});
  731. my $owx_device = FRM_OWX_firmata_to_device($data->{device});
  732. my %requests = %{$hash->{FRM_OWX_REQUESTS}};
  733. foreach my $key (keys %requests) {
  734. if ($requests{$key}->{device} eq $owx_device) {
  735. $request = $requests{$key};
  736. $id = $key;
  737. last;
  738. };
  739. };
  740. };
  741. return unless (defined $request);
  742. my $owx_data = pack "C*",@{$data->{data}};
  743. my $owx_device = $request->{device};
  744. $hash->{FRM_OWX_REPLIES}->{$owx_device} = $owx_data;
  745. delete $hash->{FRM_OWX_REQUESTS}->{$id};
  746. last;
  747. };
  748. ($command eq "SEARCH_REPLY" or $command eq "SEARCH_ALARMS_REPLY") and do {
  749. my @owx_devices = ();
  750. foreach my $device (@{$data->{devices}}) {
  751. push @owx_devices, FRM_OWX_firmata_to_device($device);
  752. }
  753. if ($command eq "SEARCH_REPLY") {
  754. $hash->{DEVS} = \@owx_devices;
  755. #$main::attr{$hash->{NAME}}{"ow-devices"} = join " ",@owx_devices;
  756. } else {
  757. $hash->{ALARMDEVS} = \@owx_devices;
  758. }
  759. last;
  760. };
  761. }
  762. }
  763. ########### functions implementing interface to OWX ##########
  764. sub FRM_OWX_device_to_firmata
  765. {
  766. my @device;
  767. foreach my $hbyte (unpack "A2xA2A2A2A2A2A2xA2", shift) {
  768. push @device, hex $hbyte;
  769. }
  770. return {
  771. family => shift @device,
  772. crc => pop @device,
  773. identity => \@device,
  774. }
  775. }
  776. sub FRM_OWX_firmata_to_device
  777. {
  778. my $device = shift;
  779. return sprintf ("%02X.%02X%02X%02X%02X%02X%02X.%02X",$device->{family},@{$device->{identity}},$device->{crc});
  780. }
  781. sub FRM_OWX_Verify {
  782. my ($hash,$dev) = @_;
  783. foreach my $found (@{$hash->{DEVS}}) {
  784. if ($dev eq $found) {
  785. return 1;
  786. }
  787. }
  788. return 0;
  789. }
  790. sub FRM_OWX_Alarms {
  791. my ($hash) = @_;
  792. my $ret = eval {
  793. my $firmata = FRM_Client_FirmataDevice($hash);
  794. my $pin = $hash->{PIN};
  795. return 0 unless ( defined $firmata and defined $pin );
  796. $hash->{ALARMDEVS} = undef;
  797. $firmata->onewire_search_alarms($hash->{PIN});
  798. my $times = AttrVal($hash,"ow-read-timeout",1000) / 50; #timeout in ms, defaults to 1 sec
  799. for (my $i=0;$i<$times;$i++) {
  800. if (FRM_poll($hash->{IODev})) {
  801. if (defined $hash->{ALARMDEVS}) {
  802. return 1;
  803. }
  804. } else {
  805. select (undef,undef,undef,0.05);
  806. }
  807. }
  808. $hash->{ALARMDEVS} = [];
  809. return 1;
  810. };
  811. if ($@) {
  812. Log3 $hash->{NAME},4,"FRM_OWX_Alarms: ".GP_Catch($@);
  813. return 0;
  814. }
  815. return $ret;
  816. }
  817. sub FRM_OWX_Reset {
  818. my ($hash) = @_;
  819. my $ret = eval {
  820. my $firmata = FRM_Client_FirmataDevice($hash);
  821. my $pin = $hash->{PIN};
  822. return undef unless ( defined $firmata and defined $pin );
  823. $firmata->onewire_reset($pin);
  824. return 1;
  825. };
  826. if ($@) {
  827. Log3 $hash->{NAME},4,"FRM_OWX_Alarms: ".GP_Catch($@);
  828. return 0;
  829. }
  830. return $ret;
  831. }
  832. sub FRM_OWX_Complex ($$$$) {
  833. my ( $hash, $owx_dev, $data, $numread ) = @_;
  834. my $res = "";
  835. my $ret = eval {
  836. my $firmata = FRM_Client_FirmataDevice($hash);
  837. my $pin = $hash->{PIN};
  838. return 0 unless ( defined $firmata and defined $pin );
  839. my $ow_command = {};
  840. #-- has match ROM part
  841. if ($owx_dev) {
  842. $ow_command->{"select"} = FRM_OWX_device_to_firmata($owx_dev);
  843. #-- padding first 9 bytes into result string, since we have this
  844. # in the serial interfaces as well
  845. $res .= "000000000";
  846. }
  847. #-- has data part
  848. if ($data) {
  849. my @data = unpack "C*", $data;
  850. $ow_command->{"write"} = \@data;
  851. $res.=$data;
  852. }
  853. #-- has receive part
  854. if ( $numread > 0 ) {
  855. $ow_command->{"read"} = $numread;
  856. #Firmata sends 0-address on read after skip
  857. $owx_dev = '00.000000000000.00' unless defined $owx_dev;
  858. my $id = $hash->{FRM_OWX_CORRELATIONID};
  859. $ow_command->{"id"} = $hash->{FRM_OWX_CORRELATIONID};
  860. $hash->{FRM_OWX_REQUESTS}->{$id} = {
  861. command => $ow_command,
  862. device => $owx_dev
  863. };
  864. delete $hash->{FRM_OWX_REPLIES}->{$owx_dev};
  865. $hash->{FRM_OWX_CORRELATIONID} = ($id + 1) & 0xFFFF;
  866. }
  867. $firmata->onewire_command_series( $pin, $ow_command );
  868. if ($numread) {
  869. my $times = AttrVal($hash,"ow-read-timeout",1000) / 50; #timeout in ms, defaults to 1 sec
  870. for (my $i=0;$i<$times;$i++) {
  871. if (FRM_poll($hash->{IODev})) {
  872. if (defined $hash->{FRM_OWX_REPLIES}->{$owx_dev}) {
  873. $res .= $hash->{FRM_OWX_REPLIES}->{$owx_dev};
  874. return $res;
  875. }
  876. } else {
  877. select (undef,undef,undef,0.05);
  878. }
  879. }
  880. }
  881. return $res;
  882. };
  883. if ($@) {
  884. Log3 $hash->{NAME},4,"FRM_OWX_Alarms: ".GP_Catch($@);
  885. return 0;
  886. }
  887. return $ret;
  888. }
  889. ########################################################################################
  890. #
  891. # OWX_Discover_FRM - Discover devices on the 1-Wire bus via internal firmware
  892. #
  893. # Parameter hash = hash of bus master
  894. #
  895. # Return 0 : error
  896. # 1 : OK
  897. #
  898. ########################################################################################
  899. sub FRM_OWX_Discover ($) {
  900. my ($hash) = @_;
  901. my $ret = eval {
  902. my $firmata = FRM_Client_FirmataDevice($hash);
  903. my $pin = $hash->{PIN};
  904. return 0 unless ( defined $firmata and defined $pin );
  905. my $old_devices = $hash->{DEVS};
  906. $hash->{DEVS} = undef;
  907. $firmata->onewire_search($hash->{PIN});
  908. my $times = AttrVal($hash,"ow-read-timeout",1000) / 50; #timeout in ms, defaults to 1 sec
  909. for (my $i=0;$i<$times;$i++) {
  910. if (FRM_poll($hash->{IODev})) {
  911. if (defined $hash->{DEVS}) {
  912. return 1;
  913. }
  914. } else {
  915. select (undef,undef,undef,0.05);
  916. }
  917. }
  918. $hash->{DEVS} = $old_devices;
  919. return 1;
  920. };
  921. if ($@) {
  922. Log3 $hash->{NAME},4,"FRM_OWX_Alarms: ".GP_Catch($@);
  923. return 0;
  924. }
  925. return $ret;
  926. }
  927. 1;
  928. =pod
  929. CHANGES
  930. 18.12.2015 jensb
  931. o added sub FRM_is_firmata_connected
  932. - extended connection check including {FirmataDevice}->{io} (gets
  933. deleted by FRM_FirmataDevice_Close on TCP disconnect while FHEM
  934. has still a valid reference to {FirmataDevice} when calling
  935. I2CWrtFn)
  936. o modified sub FRM_Set, FRM_Get, FRM_I2C_Write, FRM_poll:
  937. - use sub FRM_is_firmata_connected to check if Firmata is still
  938. connected before performing IO operations (to prevent FHEM crash)
  939. o modified sub FRM_Tcp_Connection_Close:
  940. - set STATE to listening and delete SocketDevice (to present same
  941. idle state as FRM_Start)
  942. o help updated
  943. =cut
  944. =pod
  945. =begin html
  946. <a name="FRM"></a>
  947. <h3>FRM</h3>
  948. <ul>
  949. connects fhem to <a href="http://www.arduino.cc">Arduino</a> using
  950. the <a href="http://www.firmata.org">Firmata</a> protocol.
  951. <br><br>
  952. A single FRM device can serve multiple FRM-clients.<br><br>
  953. Clients of FRM are:<br><br>
  954. <a href="#FRM_IN">FRM_IN</a> for digital input<br>
  955. <a href="#FRM_OUT">FRM_OUT</a> for digital out<br>
  956. <a href="#FRM_AD">FRM_AD</a> for analog input<br>
  957. <a href="#FRM_PWM">FRM_PWM</a> for analog output (pulse_width_modulated)<br>
  958. <a href="#FRM_RGB">FRM_RGB</a> control multichannel/RGB-LEDs by pwm<br>
  959. <a href="#FRM_SERVO">FRM_SERVO</a> for pwm-controled servos as being used in modelmaking<br>
  960. <a href="#FRM_LCD">FRM_LCD</a> output text to LCD attached via I2C<br>
  961. <a href="#FRM_I2C">FRM_I2C</a> to read data from integrated circutes attached
  962. to Arduino supporting the <a href="http://en.wikipedia.org/wiki/I%C2%B2C">
  963. i2c-protocol</a>.<br>
  964. <a href="#OWX">OWX</a> to read/write sensors and actors on 1-Wire bus.<br><br>
  965. Each client stands for a Pin of the Arduino configured for a specific use
  966. (digital/analog in/out) or an integrated circuit connected to Arduino by i2c.<br><br>
  967. Note: this module is based on <a href="https://github.com/ntruchsess/perl-firmata">Device::Firmata</a> module (perl-firmata).
  968. perl-firmata is included in FHEM-distributions lib-directory. You can download the latest version <a href="https://github.com/amimoto/perl-firmata/archive/master.zip">as a single zip</a> file from github.<br><br>
  969. Note: this module may require the Device::SerialPort or Win32::SerialPort
  970. module if you attach the device via USB and the OS sets strange default
  971. parameters for serial devices.<br><br>
  972. <a name="FRMdefine"></a>
  973. <b>Define</b><br>
  974. <ul><br>
  975. <code>define &lt;name&gt; FRM {&lt;device&gt; | &lt;port&gt; [global]}</code> <br>
  976. Specifies the FRM device.<br>
  977. <br>
  978. <li>USB-connected devices:<br><br>
  979. <code>&lt;device&gt;</code> specifies the serial port to communicate with the Arduino.
  980. The name of the serial-device depends on your distribution, under
  981. linux the cdc_acm kernel module is responsible, and usually a
  982. /dev/ttyACM0 device will be created. If your distribution does not have a
  983. cdc_acm module, you can force usbserial to handle the Arduino by the
  984. following command:<br>
  985. <code>modprobe usbserial vendor=0x03eb product=0x204b</code></br>
  986. In this case the device is most probably /dev/ttyUSB0.<br><br>
  987. You can also specify a baudrate if the device name contains the @
  988. character, e.g.: /dev/ttyACM0@38400<br><br>
  989. If the baudrate is "directio" (e.g.: /dev/ttyACM0@directio), then the
  990. perl module Device::SerialPort is not needed, and fhem opens the device
  991. with simple file io. This might work if the operating system uses sane
  992. defaults for the serial parameters, e.g. some Linux distributions and
  993. OSX. <br><br>
  994. The Arduino has to run either 'StandardFirmata' or 'ConfigurableFirmata'.
  995. StandardFirmata supports Digital and Analog-I/O, Servo and I2C. In addition
  996. to that ConfigurableFirmata supports 1-Wire and Stepper-motors.<br><br>
  997. You can find StandardFirmata in the Arduino-IDE under 'Examples->Firmata->StandardFirmata<br><br>
  998. ConfigurableFirmata has to be installed manualy. See <a href="https://github.com/firmata/arduino/tree/configurable/examples/ConfigurableFirmata">
  999. ConfigurableFirmata</a> on GitHub or <a href="http://www.fhemwiki.de/wiki/Arduino_Firmata#Installation_ConfigurableFirmata">FHEM-Wiki</a><br>
  1000. </li>
  1001. <br>
  1002. <li>Network-connected devices:<br><br>
  1003. <code>&lt;port&gt;</code> specifies the port the FRM device listens on. If <code>global</code> is
  1004. specified the socket is bound to all local IP addresses, otherwise to localhost
  1005. only.<br><br>
  1006. The Arduino has to run either 'StandardFirmataEthernet' or 'ConfigurableFirmata'.
  1007. StandardFirmataEthernet supports Digital and Analog-I/O, Servo and I2C. In addition
  1008. to that ConfigurableFirmata supports 1-Wire and Stepper-motors.<br><br>
  1009. The connection is initiated by the Arduino in client-mode. Therefore the IP address and port
  1010. of the fhem-server has to be configured in the Arduino, so it knows where to connect to.<br>
  1011. As of now only a single Arduino per FRM-device configured is supported. Multiple
  1012. Arduinos may connect to different FRM-devices configured for different ports.<br><br>
  1013. You can find StandardFirmataEthernet in the Arduino-IDE under 'Examples->Firmata->StandardFirmataEthernet<br><br>
  1014. ConfigurableFirmata has to be installed manually. See <a href="https://github.com/firmata/arduino/tree/configurable/examples/ConfigurableFirmata">
  1015. ConfigurableFirmata</a> on GitHub or <a href="http://www.fhemwiki.de/wiki/Arduino_Firmata#Installation_ConfigurableFirmata">FHEM-Wiki</a><br>
  1016. </li>
  1017. <br>
  1018. <li>
  1019. If the device is called none, then no device will be opened, so you
  1020. can experiment without hardware attached.<br>
  1021. </li>
  1022. </ul>
  1023. <br>
  1024. <a name="FRMset"></a>
  1025. <b>Set</b>
  1026. <ul>
  1027. <li>
  1028. <code>set &lt;name&gt; init</code><br>
  1029. reinitializes the FRM-Client-devices configured for this Arduino
  1030. </li><br>
  1031. <li>
  1032. <code>set &lt;name&gt; reset</code><br>
  1033. does a complete reset of FRM by disconnecting from, reconnecting to and reinitializing the Arduino and FRM internals and all attached FRM-client-devices
  1034. </li>
  1035. </ul>
  1036. <br><br>
  1037. <a name="FRMattr"></a>
  1038. <b>Attributes</b><br>
  1039. <ul>
  1040. <li>i2c-config<br>
  1041. Configure the Arduino for ic2 communication. This will enable i2c on the
  1042. i2c_pins received by the capability-query issued during initialization of FRM.<br>
  1043. As of Firmata 2.3 you can set a delay-time (in microseconds, max. 65535, default 0) that will be
  1044. inserted into i2c protocol when switching from write to read. This may be necessary because Firmata
  1045. i2c write does not block on the fhem side so consecutive i2c write/read operations get queued and
  1046. will be executed on the Firmata device in a different time sequence. Use the maximum operation
  1047. time required by the connected i2c devices (e.g. 30000 for the BMP180 with triple oversampling,
  1048. see i2c device manufacturer documentation for details). <br>
  1049. See: <a href="http://www.firmata.org/wiki/Protocol#I2C">Firmata Protocol details about I2C</a><br>
  1050. </li><br>
  1051. <li>sampling-interval<br>
  1052. Configure the interval Firmata reports analog data to FRM (in milliseconds, max. 65535). <br>
  1053. See: <a href="http://www.firmata.org/wiki/Protocol#Sampling_Interval">Firmata Protocol details about Sampling Interval</a></br>
  1054. </li>
  1055. </ul>
  1056. </ul>
  1057. <br>
  1058. =end html
  1059. =cut