38_CO20.pm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. # $Id: 38_CO20.pm 9679 2015-10-25 23:10:29Z markus-m $
  2. # basic idea from http://code.google.com/p/airsensor-linux-usb
  3. package main;
  4. use strict;
  5. use warnings;
  6. use Device::USB;
  7. sub
  8. CO20_Initialize($)
  9. {
  10. my ($hash) = @_;
  11. $hash->{DefFn} = "CO20_Define";
  12. $hash->{NotifyFn} = "CO20_Notify";
  13. $hash->{UndefFn} = "CO20_Undefine";
  14. $hash->{SetFn} = "CO20_Set";
  15. $hash->{GetFn} = "CO20_Get";
  16. $hash->{AttrFn} = "CO20_Attr";
  17. $hash->{AttrList} = "disable:1 ".
  18. "advanced:1 ".
  19. "interval ".
  20. "retries ".
  21. "timeout ".
  22. $readingFnAttributes;
  23. }
  24. #####################################
  25. sub
  26. CO20_Define($$)
  27. {
  28. my ($hash, $def) = @_;
  29. my @a = split("[ \t][ \t]*", $def);
  30. return "Usage: define <name> CO20 [bus:device]" if(@a < 2);
  31. delete $hash->{ID};
  32. my $name = $a[0];
  33. $hash->{tag} = undef;
  34. $hash->{ID} = $a[2] if( defined($a[2]));
  35. $hash->{NAME} = $name;
  36. $hash->{fail} = 0;
  37. $hash->{seq2} = 0x67;
  38. $hash->{seq4} = 0x0001;
  39. $hash->{NOTIFYDEV} = "global";
  40. if( $init_done ) {
  41. CO20_Disconnect($hash);
  42. CO20_Connect($hash);
  43. } elsif( $hash->{STATE} ne "???" ) {
  44. $hash->{STATE} = "Initialized";
  45. }
  46. return undef;
  47. }
  48. sub
  49. CO20_Notify($$)
  50. {
  51. my ($hash,$dev) = @_;
  52. return if($dev->{NAME} ne "global");
  53. return if(!grep(m/^INITIALIZED|REREADCFG$/, @{$dev->{CHANGED}}));
  54. CO20_Connect($hash);
  55. }
  56. my $VENDOR = 0x03eb;
  57. my $PRODUCT = 0x2013;
  58. sub
  59. CO20_SetStickData($$)
  60. {
  61. my ($hash, $data) = @_;
  62. my $name = $hash->{NAME};
  63. my $strlen = length($data);
  64. my $ind = 0;
  65. Log3 $name, 5, "datalen $strlen";
  66. if($strlen == 240) {
  67. $ind = index($data, "warn1")+22;
  68. $hash->{KNOB_CO2_VOC_level_warn1} = ord(substr($data,$ind+1,1))*256 + ord(substr($data,$ind,1));
  69. $ind = index($data, "warn2")+22;
  70. $hash->{KNOB_CO2_VOC_level_warn2} = ord(substr($data,$ind+1,1))*256 + ord(substr($data,$ind,1));
  71. $ind = index($data, "Reg_Set")+20;
  72. $hash->{KNOB_Reg_Set} = ord(substr($data,$ind+1,1))*256 + ord(substr($data,$ind,1));
  73. $ind = index($data, "Reg_P")+19;
  74. $hash->{KNOB_Reg_P} = ord(substr($data,$ind+1,1))*256 + ord(substr($data,$ind,1));
  75. $ind = index($data, "Reg_I")+19;
  76. $hash->{KNOB_Reg_I} = ord(substr($data,$ind+1,1))*256 + ord(substr($data,$ind,1));
  77. $ind = index($data, "Reg_D")+19;
  78. $hash->{KNOB_Reg_D} = ord(substr($data,$ind+1,1))*256 + ord(substr($data,$ind,1));
  79. $ind = index($data, "LogInterval")+27;
  80. $hash->{KNOB_LogInterval} = ord(substr($data,$ind+1,1))*256 + ord(substr($data,$ind,1));
  81. $ind = index($data, "ui16StartupBits")+30;
  82. $hash->{KNOB_ui16StartupBits} = ord(substr($data,$ind+1,1))*256 + ord(substr($data,$ind,1));
  83. } elsif($strlen == 32) {
  84. $ind = index($data, ";");
  85. $hash->{FLAG_WARMUP} = ord(substr($data,$ind+3,1))*256 + ord(substr($data,$ind+2,1));
  86. $hash->{FLAG_BURN_IN} = ord(substr($data,$ind+7,1))*256 + ord(substr($data,$ind+6,1));
  87. $hash->{FLAG_RESET_BASELINE} = ord(substr($data,$ind+11,1))*256 + ord(substr($data,$ind+10,1));
  88. $hash->{FLAG_CALIBRATE_HEATER} = ord(substr($data,$ind+15,1))*256 + ord(substr($data,$ind+14,1));
  89. $hash->{FLAG_LOGGING} = ord(substr($data,$ind+19,1))*256 + ord(substr($data,$ind+18,1));
  90. } elsif($strlen == 1) {
  91. delete( $hash->{KNOB_CO2_VOC_level_warn1} );
  92. delete( $hash->{KNOB_CO2_VOC_level_warn2} );
  93. delete( $hash->{KNOB_Reg_Set} );
  94. delete( $hash->{KNOB_Reg_P} );
  95. delete( $hash->{KNOB_Reg_I} );
  96. delete( $hash->{KNOB_Reg_D} );
  97. delete( $hash->{KNOB_LogInterval} );
  98. delete( $hash->{KNOB_ui16StartupBits} );
  99. delete( $hash->{FLAG_WARMUP} );
  100. delete( $hash->{FLAG_BURN_IN} );
  101. delete( $hash->{FLAG_RESET_BASELINE} );
  102. delete( $hash->{FLAG_CALIBRATE_HEATER} );
  103. delete( $hash->{FLAG_LOGGING} );
  104. }
  105. return undef;
  106. }
  107. sub
  108. CO20_Connect($)
  109. {
  110. my ($hash) = @_;
  111. my $name = $hash->{NAME};
  112. return undef if( AttrVal($name, "disable", 0 ) == 1 );
  113. $hash->{USB} = Device::USB->new() if( !$hash->{USB} );
  114. if( $hash->{ID} && $hash->{ID} =~ m/(\d.*):(\d.*)/ ) {
  115. my $dirname = $1;
  116. my $filename = $2;
  117. delete $hash->{DEV};
  118. foreach my $bus ($hash->{USB}->list_busses()) {
  119. next if( $bus->{dirname} != $dirname );
  120. foreach my $device (@{$bus->{devices}}) {
  121. next if( $device->idVendor() != $VENDOR );
  122. next if( $device->idProduct() != $PRODUCT );
  123. next if( $device->{filename} != $filename );
  124. $hash->{DEV} = $device;
  125. last;
  126. }
  127. last if( $hash->{DEV} );
  128. }
  129. } else {
  130. $hash->{DEV} = $hash->{USB}->find_device( $VENDOR, $PRODUCT );
  131. }
  132. if( $hash->{DEV} ) {
  133. $hash->{STATE} = "found";
  134. Log3 $name, 3, "$name: CO20 device found";
  135. $hash->{DEV}->open();
  136. $hash->{manufacturer} = $hash->{DEV}->manufacturer();
  137. $hash->{product} = $hash->{DEV}->product();
  138. if( $hash->{manufacturer} && $hash->{product} ) {
  139. $hash->{DEV}->detach_kernel_driver_np(0) if( $hash->{DEV}->get_driver_np(0) );
  140. my $ret = $hash->{DEV}->claim_interface( 0 );
  141. if( $ret == -16 ) {
  142. $hash->{STATE} = "waiting";
  143. Log3 $name, 3, "$name: waiting for CO20 device";
  144. return;
  145. } elsif( $ret != 0 ) {
  146. Log3 $name, 3, "$name: failed to claim CO20 device";
  147. CO20_Disconnect($hash);
  148. return;
  149. }
  150. $hash->{STATE} = "opened";
  151. Log3 $name, 3, "$name: CO20 device opened";
  152. $hash->{INTERVAL} = AttrVal($name, "interval", 300);
  153. $hash->{retries} = AttrVal($name,"retries",3);
  154. $hash->{timeout} = AttrVal($name,"timeout",1000);
  155. RemoveInternalTimer($hash);
  156. InternalTimer(gettimeofday()+10, "CO20_poll", $hash, 0);
  157. my $buf;
  158. $hash->{DEV}->interrupt_read(0x00000081, $buf, 0x0000010, 1000);
  159. } else {
  160. Log3 $name, 3, "$name: failed to open CO20 device";
  161. CO20_Disconnect($hash);
  162. }
  163. } else {
  164. Log3 $name, 3, "$name: failed to find CO20 device";
  165. }
  166. }
  167. sub
  168. CO20_Disconnect($)
  169. {
  170. my ($hash) = @_;
  171. my $name = $hash->{NAME};
  172. RemoveInternalTimer($hash);
  173. return if( !$hash->{USB} );
  174. if( $hash->{manufacturer} && $hash->{product} ) {
  175. $hash->{DEV}->release_interface(0);
  176. }
  177. delete( $hash->{USB} );
  178. delete( $hash->{DEV} );
  179. delete( $hash->{manufacturer} );
  180. delete( $hash->{product} );
  181. delete( $hash->{BLOCKED} );
  182. delete $hash->{FIRMWARE};
  183. CO20_SetStickData($hash,"X");
  184. $hash->{STATE} = "disconnected";
  185. Log3 $name, 3, "$name: disconnected";
  186. }
  187. sub
  188. CO20_Undefine($$)
  189. {
  190. my ($hash, $arg) = @_;
  191. CO20_Disconnect($hash);
  192. $hash->{fail} = 0;
  193. return undef;
  194. }
  195. sub
  196. CO20_identify($)
  197. {
  198. my ($hash) = @_;
  199. CO20_dataread($hash,"stickdata");
  200. }
  201. sub
  202. CO20_poll($)
  203. {
  204. my ($hash) = @_;
  205. my $name = $hash->{NAME};
  206. if(!$hash->{LOCAL}) {
  207. RemoveInternalTimer($hash);
  208. InternalTimer(gettimeofday()+$hash->{INTERVAL}, "CO20_poll", $hash, 0);
  209. }
  210. if($hash->{BLOCKED}) {
  211. return undef;
  212. }
  213. if( $hash->{manufacturer} && $hash->{product} ) {
  214. my $buf = "@".sprintf("%c",$hash->{seq2})."TRF?\n@@@@@@@@@";
  215. Log3 $name, 5, "$name: sent $buf / ".ord(substr($buf,0,1));
  216. my $ret = $hash->{DEV}->interrupt_write(0x00000002, $buf, 0x0000010, $hash->{timeout});
  217. if( $ret != 16 ) {
  218. my $ret2 = $hash->{DEV}->interrupt_write(0x00000002, "@@@@@@@@@@@@@@@@", 0x0000010, $hash->{timeout});
  219. $hash->{fail} = $hash->{fail}+1;
  220. Log3 $name, 4, "$name: write error $ret/$ret2 ($hash->{fail})";
  221. RemoveInternalTimer($hash);
  222. InternalTimer(gettimeofday()+30, "CO20_poll", $hash, 1);
  223. if($hash->{fail} >= $hash->{retries}) {
  224. $hash->{fail} = 0;
  225. CO20_Disconnect($hash);
  226. $hash->{RECONNECT} = 1;
  227. CO20_Connect($hash);
  228. }
  229. return undef;
  230. }
  231. if ($hash->{seq2} < 0xFF){ $hash->{seq2}++} else {$hash->{seq2} = 0x67};
  232. my $data="";
  233. for( $a = 1; $a <= 3; $a = $a + 1 ) {
  234. $ret=$hash->{DEV}->interrupt_read(0x00000081, $buf, 0x0000010, $hash->{timeout});
  235. if( $ret != 16 and $ret != 0 ) {
  236. Log3 $name, 4, "$name: read error $ret";
  237. }
  238. $data.=$buf;
  239. }
  240. Log3 $name, 4, "$name got $data / ".length($data)." / ".ord(substr($data,0,1));
  241. if( $ret != 16 and $ret != 0 and length($data) < 16 ) {
  242. $hash->{fail} = $hash->{fail}+1;
  243. RemoveInternalTimer($hash);
  244. InternalTimer(gettimeofday()+30, "CO20_poll", $hash, 1);
  245. Log3 $name, 4, "$name: readloop error $ret ($hash->{fail})";
  246. if($hash->{fail} >= $hash->{retries}) {
  247. $hash->{fail} = 0;
  248. CO20_Disconnect($hash);
  249. $hash->{RECONNECT} = 1;
  250. CO20_Connect($hash);
  251. }
  252. return undef;
  253. }
  254. if( length($data) >= 16 ) {
  255. $data = "@".$data if(ord(substr($data,0,1)) > 64);
  256. $hash->{fail} = 0;
  257. my $voc = ord(substr($data,3,1))*256 + ord(substr($data,2,1));
  258. my $dbg = ord(substr($data,5,1))*256 + ord(substr($data,4,1));
  259. my $pwm = ord(substr($data,7,1))*256 + ord(substr($data,6,1));
  260. my $rh = ord(substr($data,9,1))*256 + ord(substr($data,8,1));
  261. my $rs = ord(substr($data,14,1))*65536 + ord(substr($data,13,1))*256 + ord(substr($data,12,1));
  262. if (ord(substr($data,3,1)) < 128) {
  263. readingsBeginUpdate($hash);
  264. readingsBulkUpdate( $hash, "voc", $voc, 1 );
  265. readingsBulkUpdate( $hash, "debug", $dbg, 1 );
  266. readingsBulkUpdate( $hash, "pwm", $pwm, 1 );
  267. readingsBulkUpdate( $hash, "r_h", $rh/100, 1 );
  268. readingsBulkUpdate( $hash, "r_s", $rs, 1 );
  269. readingsEndUpdate($hash,1);
  270. }
  271. #my $bufdec = ord(substr($buf,0,1))." ".ord(substr($buf,1,1))." ".ord(substr($buf,2,1))." ".ord(substr($buf,3,1))." ".ord(substr($buf,4,1))." ".ord(substr($buf,5,1))." ".ord(substr($buf,6,1))." ".ord(substr($buf,7,1))." ".ord(substr($buf,8,1))." ".ord(substr($buf,9,1))." ".ord(substr($buf,10,1))." ".ord(substr($buf,11,1))." ".ord(substr($buf,12,1))." ".ord(substr($buf,13,1))." ".ord(substr($buf,14,1))." ".ord(substr($buf,15,1))." ".ord(substr($buf,16,1));
  272. # Log3 $name, 5, "$name: read 1 success\n$bufdec";
  273. } else {
  274. $hash->{fail} = $hash->{fail}+1;
  275. Log3 $name, 2, "$name: read failed $ret ($hash->{fail})";
  276. if($hash->{fail} >= $hash->{retries}) {
  277. $hash->{fail} = 0;
  278. CO20_Disconnect($hash);
  279. $hash->{RECONNECT} = 1;
  280. CO20_Connect($hash);
  281. }
  282. }
  283. $hash->{LAST_POLL} = FmtDateTime( gettimeofday() );
  284. } else {
  285. Log3 $name, 2, "$name: no device";
  286. $hash->{fail} = 0;
  287. CO20_Disconnect($hash);
  288. $hash->{RECONNECT} = 1;
  289. CO20_Connect($hash);
  290. }
  291. }
  292. sub
  293. CO20_dataread($$)
  294. {
  295. my ($hash, $readingstype) = @_;
  296. my $name = $hash->{NAME};
  297. my $reqstr = "";
  298. my $retcount = 16;
  299. if($readingstype eq "knobdata") {
  300. $reqstr = "KNOBPRE?";
  301. $retcount = 16;
  302. } elsif ($readingstype eq "flagdata") {
  303. $reqstr = "FLAGGET?";
  304. $retcount = 3;
  305. } elsif ($readingstype eq "stickdata") {
  306. $reqstr = "*IDN?";
  307. $retcount = 8;
  308. } else {
  309. return undef;
  310. }
  311. RemoveInternalTimer($hash);
  312. InternalTimer(gettimeofday()+$hash->{INTERVAL}, "CO20_poll", $hash, 1);
  313. if( $hash->{manufacturer} && $hash->{product} ) {
  314. my $seq = sprintf("%04X",$hash->{seq4});
  315. my $seqstr = sprintf("%c",hex substr($seq,2,2)).sprintf("%c",hex substr($seq,0,2));
  316. $hash->{seq4} = ($hash->{seq4} +1) & 0xFFFF;
  317. my $buf = substr("@".$seq.$reqstr."\n@@@@@@@@@@@@@@@@",0,16);
  318. my $ret = $hash->{DEV}->interrupt_write(0x00000002, $buf, 0x0000010, $hash->{timeout});
  319. Log3 $name, 4, "getdata write $ret" if($ret != 16);
  320. my $data = "";
  321. my $intdata = "";
  322. if($ret == 16) {
  323. for( $a = 1; $a <= $retcount; $a = $a + 1 ){
  324. $hash->{DEV}->interrupt_read(0x00000081, $buf, 0x0000010, $hash->{timeout});
  325. $data.=$buf;
  326. Log3 $name, 4, "getdata read $ret" if($ret != 16);
  327. $intdata = ord(substr($buf,0,1))." ".ord(substr($buf,1,1))." ".ord(substr($buf,2,1))." ".ord(substr($buf,3,1))." ".ord(substr($buf,4,1))." ".ord(substr($buf,5,1))." ".ord(substr($buf,6,1))." ".ord(substr($buf,7,1))." ".ord(substr($buf,8,1))." ".ord(substr($buf,9,1))." ".ord(substr($buf,10,1))." ".ord(substr($buf,11,1))." ".ord(substr($buf,12,1))." ".ord(substr($buf,13,1))." ".ord(substr($buf,14,1))." ".ord(substr($buf,15,1)) if(length($buf) > 15);
  328. Log3 $name, 5, "$intdata\n$buf";
  329. }
  330. Log3 $name, 5, length($data);
  331. }
  332. if($readingstype eq "knobdata") {
  333. CO20_SetStickData($hash,$data);
  334. } elsif ($readingstype eq "flagdata") {
  335. CO20_SetStickData($hash,$data);
  336. } elsif ($readingstype eq "stickdata") {
  337. if ($data =~ /\bStick\b(.*?)\bMCU\b/) {
  338. $hash->{FIRMWARE} = $1;
  339. }
  340. if ($data =~ /\bS\/N:\b(.*?)\b;bI\b/) {
  341. $hash->{SERIALNUMBER} = $1;
  342. }
  343. }
  344. }
  345. }
  346. sub
  347. CO20_flashread($)
  348. {
  349. my ($hash) = @_;
  350. my $name = $hash->{NAME};
  351. return undef;
  352. # 40 30 30 31 31 52 45 43 4F 52 44 53 3F 0A 40 40 @0011RECORDS?.@@
  353. #
  354. # 40 30 30 31 32 4C 42 53 49 5A 45 3F 0A 40 40 40 @0012LBSIZE?.@@@
  355. #
  356. # 40 30 30 31 33 46 4C 53 54 4F 50 0A 40 40 40 40 @0013FLSTOP.@@@@
  357. #
  358. # 40 30 30 31 34 4C 42 53 49 5A 45 3F 0A 40 40 40 @0014LBSIZE?.@@@
  359. #
  360. # 40 30 30 31 35 2A 49 44 4E 3F 0A 40 40 40 40 40 @0015*IDN?.@@@@@
  361. #
  362. # 40 30 30 31 36 4C 42 41 56 47 3B 31 30 30 0A 40 @0016LBAVG;100.@
  363. #
  364. # 40 6A 4C 42 52 0A 40 40 40 40 40 40 40 40 40 40 @jLBR.@@@@@@@@@@ n times ?
  365. #
  366. # 40 30 30 31 37 46 4C 53 54 41 52 54 0A 40 40 40 @0017FLSTART.@@@ n times ?
  367. #
  368. # 2 reads each
  369. }
  370. sub
  371. CO20_dataset($$$)
  372. {
  373. my ($hash, $cmd, $val) = @_;
  374. my $name = $hash->{NAME};
  375. my $reqstr = "";
  376. if($cmd eq "flag_WARMUP") {
  377. $reqstr = "FLAGSET;WARMUP="; # 0000
  378. } elsif($cmd eq "flag_BURN-IN") {
  379. $reqstr = "FLAGSET;BURN-IN="; # 0000
  380. } elsif($cmd eq "flag_RESET_BASELINE") {
  381. $reqstr = "FLAGSET;RESET BASELINE="; # 0000
  382. } elsif($cmd eq "flag_CALIBRATE_HEATER") {
  383. $reqstr = "FLAGSET;CALIBRATE HEATER="; # 0000
  384. } elsif($cmd eq "flag_LOGGING") {
  385. $reqstr = "FLAGSET;LOGGING="; # 0000
  386. } elsif($cmd eq "knob_CO2/VOC_level_warn1") {
  387. $reqstr = "KNOBSET;CO2/VOC level_warn1=";
  388. } elsif($cmd eq "knob_CO2/VOC_level_warn2") {
  389. $reqstr = "KNOBSET;CO2/VOC level_warn2=";
  390. } elsif($cmd eq "knob_Reg_Set") {
  391. $reqstr = "KNOBSET;Reg_Set="; # 9100
  392. } elsif($cmd eq "knob_Reg_P") {
  393. $reqstr = "KNOBSET;Reg_P="; # 0300
  394. } elsif($cmd eq "knob_Reg_I") {
  395. $reqstr = "KNOBSET;Reg_I="; # 0A00
  396. } elsif($cmd eq "knob_Reg_D") {
  397. $reqstr = "KNOBSET;Reg_D="; # 0000
  398. } elsif($cmd eq "knob_LogInterval") {
  399. $reqstr = "KNOBSET;LogInterval="; # 0000
  400. } elsif($cmd eq "knob_ui16StartupBits") {
  401. $reqstr = "KNOBSET;ui16StartupBits="; # 0000
  402. } elsif($cmd eq "recalibrate_heater") {
  403. $reqstr = "FLAGSET;CALIBRATE HEATER="; # 0180
  404. } elsif($cmd eq "reset_baseline") {
  405. $reqstr = "FLAGSET;RESET BASELINE="; # 0180
  406. } elsif($cmd eq "reset_device") {
  407. $reqstr = "*RST";
  408. }
  409. RemoveInternalTimer($hash);
  410. InternalTimer(gettimeofday()+$hash->{INTERVAL}, "CO20_poll", $hash, 1);
  411. if( $hash->{manufacturer} && $hash->{product} ) {
  412. my $seq = sprintf("%04X",$hash->{seq4});
  413. $hash->{seq4} = ($hash->{seq4} +1) & 0xFFFF;
  414. my $buf = "@".$seq.$reqstr;
  415. if($cmd ne "reset_device") {
  416. $buf .= "\x02";
  417. if($cmd eq "recalibrate_heater" or $cmd eq "reset_baseline") {
  418. $buf .= "\x01\x80";
  419. } else {
  420. my $h = sprintf("%04X",$val & 0xFFFF);
  421. $buf .= sprintf("%c",hex substr($h,2,2)).sprintf("%c",hex substr($h,0,2));
  422. Log3 $name, 5, "$val $h \n";
  423. }
  424. }
  425. if (index($reqstr, "KNOBSET") != -1) {
  426. $buf .= ";";
  427. }
  428. $buf .= "\n";
  429. my $buflen = length($buf);
  430. $buf .= "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
  431. my $ret = $hash->{DEV}->interrupt_write(0x00000002, substr($buf,0,16), 0x0000010, $hash->{timeout});
  432. Log3 $name, 4, "setdata write $ret" if($ret != 16);
  433. if($ret == 16 and ($buflen > 16 or $cmd eq "reset_device")) {
  434. $ret = $hash->{DEV}->interrupt_write(0x00000002, substr($buf,16,16), 0x0000010, $hash->{timeout});
  435. Log3 $name, 4, "setdata write $ret" if($ret != 16);
  436. }
  437. if($ret == 16 and $buflen > 32) {
  438. $ret = $hash->{DEV}->interrupt_write(0x00000002, substr($buf,32,16), 0x0000010, $hash->{timeout});
  439. Log3 $name, 4, "setdata write $ret" if($ret != 16);
  440. }
  441. if($ret == 16 and $buflen > 15) {
  442. $hash->{DEV}->interrupt_read(0x00000081, $buf, 0x0000010, $hash->{timeout});
  443. $buflen = length($buf);
  444. Log3 $name, 5, "getdata read $ret";
  445. if($buflen > 15)
  446. {
  447. my $intdata .= ord(substr($buf,0,1))." ".ord(substr($buf,1,1))." ".ord(substr($buf,2,1))." ".ord(substr($buf,3,1))." ".ord(substr($buf,4,1))." ".ord(substr($buf,5,1))." ".ord(substr($buf,6,1))." ".ord(substr($buf,7,1))." ".ord(substr($buf,8,1))." ".ord(substr($buf,9,1))." ".ord(substr($buf,10,1))." ".ord(substr($buf,11,1))." ".ord(substr($buf,12,1))." ".ord(substr($buf,13,1))." ".ord(substr($buf,14,1))." ".ord(substr($buf,15,1));
  448. Log3 $name, 4, "$buf";
  449. }
  450. } else {
  451. Log3 $name, 4, "set data failed: $buf";
  452. return undef;
  453. }
  454. }
  455. return undef;
  456. }
  457. sub
  458. CO20_Get($$@)
  459. {
  460. my ($hash, $name, $cmd) = @_;
  461. my $list = "update:noArg";
  462. $list = "update:noArg air_data:noArg knob_data:noArg flag_data:noArg stick_data:noArg" if( AttrVal($name, "advanced", 0 ) == 1 );
  463. if( $cmd eq "air_data" or $cmd eq "update" ) {
  464. $hash->{LOCAL} = 1;
  465. CO20_poll($hash);
  466. delete $hash->{LOCAL};
  467. return undef;
  468. } elsif( $cmd eq "knob_data" ) {
  469. $hash->{BLOCKED} = 1;
  470. CO20_dataread($hash,"knobdata");
  471. delete $hash->{BLOCKED};
  472. return undef;
  473. } elsif( $cmd eq "flag_data" ) {
  474. $hash->{BLOCKED} = 1;
  475. CO20_dataread($hash,"flagdata");
  476. delete $hash->{BLOCKED};
  477. return undef;
  478. } elsif( $cmd eq "stick_data" ) {
  479. $hash->{BLOCKED} = 1;
  480. CO20_dataread($hash,"stickdata");
  481. delete $hash->{BLOCKED};
  482. return undef;
  483. }
  484. return "Unknown argument $cmd, choose one of $list";
  485. }
  486. sub
  487. CO20_Set($$$$)
  488. {
  489. my ($hash, $name, $cmd, $val) = @_;
  490. my $list = "";
  491. $list = "knob_CO2/VOC_level_warn1 knob_CO2/VOC_level_warn2 knob_Reg_Set knob_Reg_P knob_Reg_I knob_Reg_D knob_LogInterval knob_ui16StartupBits recalibrate_heater:noArg reset_baseline:noArg reset_device:noArg" if( AttrVal($name, "advanced", 0 ) == 1 );
  492. if (index($list, $cmd) != -1) {
  493. $hash->{BLOCKED} = 1;
  494. CO20_dataset($hash,$cmd,$val);
  495. delete $hash->{BLOCKED};
  496. return undef;
  497. }
  498. return "Unknown argument $cmd, choose one of $list";
  499. }
  500. sub
  501. CO20_Attr($$$)
  502. {
  503. my ($cmd, $name, $attrName, $attrVal) = @_;
  504. my $orig = $attrVal;
  505. $attrVal = int($attrVal) if($attrName eq "interval" || $attrName eq "retries" || $attrName eq "timeout");
  506. $attrVal = 10 if($attrName eq "interval" && $attrVal < 10 && $attrVal != 0);
  507. $attrVal = 20 if($attrName eq "retries" && ($attrVal < 0));
  508. $attrVal = 20 if($attrName eq "retries" && ($attrVal > 20));
  509. $attrVal = 250 if($attrName eq "timeout" && ($attrVal != 0 && $attrVal < 250));
  510. $attrVal = 10000 if($attrName eq "timeout" && ($attrVal != 0 && $attrVal > 10000));
  511. if( $attrName eq "disable" ) {
  512. my $hash = $defs{$name};
  513. if( $cmd eq "set" && $attrVal ne "0" ) {
  514. CO20_Disconnect($hash);
  515. } else {
  516. $attr{$name}{$attrName} = 0;
  517. CO20_Disconnect($hash);
  518. CO20_Connect($hash);
  519. }
  520. } elsif( $attrName eq "interval" ) {
  521. my $hash = $defs{$name};
  522. $hash->{INTERVAL} = $attrVal;
  523. CO20_poll($hash) if( $init_done );
  524. } elsif( $attrName eq "retries" ) {
  525. my $hash = $defs{$name};
  526. $hash->{retries} = $attrVal;
  527. } elsif( $attrName eq "timeout" ) {
  528. my $hash = $defs{$name};
  529. $hash->{timeout} = $attrVal;
  530. }
  531. if( $cmd eq "set" ) {
  532. if( $orig ne $attrVal ) {
  533. $attr{$name}{$attrName} = $attrVal;
  534. return $attrName ." set to ". $attrVal;
  535. }
  536. }
  537. return;
  538. }
  539. 1;
  540. =pod
  541. =begin html
  542. <a name="CO20"></a>
  543. <h3>CO20</h3>
  544. <ul>
  545. Module for measuring air quality with usb sticks based on the AppliedSensor iAQ-Engine sensor.
  546. Products currently know to work are the VOLTCRAFT CO-20, the Sentinel Haus Institut RaumluftW&auml;chter
  547. and the VELUX Raumluftf&uuml;hler.<br>
  548. Probably works with all devices recognized as iAQ Stick (0x03eb:0x2013).<br><br>
  549. Notes:
  550. <ul>
  551. <li>Device::USB hast to be installed on the FHEM host.<br>
  552. It can be installed with '<code>cpan install Device::USB</code>'<br>
  553. or on debian with '<code>sudo apt-get install libdevice-usb-perl'</code>'</li>
  554. <li>FHEM has to have permissions to open the device. To configure this with udev
  555. rules see here: <a href="https://code.google.com/p/usb-sensors-linux/wiki/Install_AirSensor_Linux">Install_AirSensor_Linux
  556. usb-sensors-linux</a></li>
  557. <li>Advanced features are only available after setting the attribute <i>advanced</i>.<br>
  558. Almost all the hidden settings from the Windows application are implemented in this mode.<br>
  559. Readout of values gathered in standalone mode is not possible yet.</li>
  560. </ul><br>
  561. <a name="CO20_Define"></a>
  562. <b>Define</b>
  563. <ul>
  564. <code>define &lt;name&gt; CO20 [bus:device]</code><br>
  565. <br>
  566. Defines a CO20 device. bus:device hast to be used if more than one sensor is connected to the same host.<br><br>
  567. Examples:
  568. <ul>
  569. <code>define CO20 CO20</code><br>
  570. </ul>
  571. </ul><br>
  572. <a name="CO20_Readings"></a>
  573. <b>Readings</b>
  574. <ul>
  575. <li>voc<br>
  576. CO2 equivalents in ppm</li>
  577. <li>debug<br>
  578. debug value</li>
  579. <li>pwm<br>
  580. pwm value</li>
  581. <li>r_h<br>
  582. resistance of heating element in Ohm (?)</li>
  583. <li>r_s<br>
  584. resistance of sensor element in Ohm (?)</li>
  585. </ul><br>
  586. <a name="CO20_Get"></a>
  587. <b>Get</b>
  588. <ul>
  589. <li>update / air_data<br>
  590. trigger an update</li>
  591. <li>flag_data<br>
  592. get internal flag values</li>
  593. <li>knob_data<br>
  594. get internal knob values</li>
  595. <li>stick_data<br>
  596. get stick information</li>
  597. </ul><br>
  598. <a name="CO20_Set"></a>
  599. <b>Set</b>
  600. <ul>
  601. <li>KNOB_CO2_VOC_level_warn1<br>
  602. sets threshold for yellow led</li>
  603. <li>KNOB_CO2_VOC_level_warn2<br>
  604. sets threshold for red led</li>
  605. <li>KNOB_Reg_Set<br>
  606. internal value, affects voc reading</li>
  607. <li>KNOB_Reg_P<br>
  608. internal pid value</li>
  609. <li>KNOB_Reg_I<br>
  610. internal pid value</li>
  611. <li>KNOB_Reg_D<br>
  612. internal pid value</li>
  613. <li>KNOB_LogInterval<br>
  614. log interval for standalone mode</li>
  615. <li>KNOB_ui16StartupBits<br>
  616. set to 0 for no automatic calibration on startup</li>
  617. <li>FLAG_WARMUP<br>
  618. warmup time left in minutes</li>
  619. <li>FLAG_BURN_IN<br>
  620. burn in time left in minutes</li>
  621. <li>FLAG_RESET_BASELINE<br>
  622. reset voc baseline value</li>
  623. <li>FLAG_CALIBRATE_HEATER<br>
  624. trigger calibration / burn in</li>
  625. <li>FLAG_LOGGING<br>
  626. value count from external logging</li>
  627. </ul><br>
  628. <a name="CO20_Attr"></a>
  629. <b>Attributes</b>
  630. <ul>
  631. <li>interval<br>
  632. the interval in seconds used to read updates [10..]. the default ist 300.</li>
  633. <li>retries<br>
  634. number of retries on USB read/write failures [0..20]. the default is 3.</li>
  635. <li>timeout<br>
  636. the USB connection timeout in milliseconds [250..10000]. the default is 1000.</li>
  637. <li>advanced<br>
  638. 1 -> enables most of the advanced settings and readings described here</li>
  639. <li>disable<br>
  640. 1 -> disconnect and stop polling</li>
  641. </ul>
  642. </ul>
  643. =end html
  644. =cut