14_CUL_TCM97001.pm 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. ##############################################
  2. # From dancer0705
  3. #
  4. # Receive temperature sensor
  5. # Supported models:
  6. # - "TCM97..."
  7. # - "ABS700"
  8. # - "TCM21...."
  9. # - "Prologue"
  10. # - "Rubicson"
  11. # - "NC_WS"
  12. # - "GT_WT_02"
  13. # - "AURIOL"
  14. # - "KW9010"
  15. #
  16. # Unsupported models are saved in a device named CUL_TCM97001_Unknown
  17. #
  18. # Copyright (C) 2016 Bjoern Hempel
  19. #
  20. # This program is free software; you can redistribute it and/or modify it under
  21. # the terms of the GNU General Public License as published by the Free Software
  22. # Foundation; either version 2 of the License, or (at your option) any later
  23. # version.
  24. #
  25. # This program is distributed in the hope that it will be useful, but
  26. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  27. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  28. # more details.
  29. #
  30. # You should have received a copy of the GNU General Public License along with
  31. # this program; if not, write to the
  32. # Free Software Foundation, Inc.,
  33. # 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
  34. #
  35. # $Id: 14_CUL_TCM97001.pm 12994 2017-01-07 07:49:53Z bjoernh $
  36. #
  37. ##############################################
  38. package main;
  39. use strict;
  40. use warnings;
  41. use SetExtensions;
  42. use constant { TRUE => 1, FALSE => 0 };
  43. #
  44. # All suported models
  45. #
  46. my %models = (
  47. "TCM97..." => 'TCM97...',
  48. "ABS700" => 'ABS700',
  49. "TCM21...." => 'TCM21....',
  50. "Prologue" => 'Prologue',
  51. "Rubicson" => 'Rubicson',
  52. "NC_WS" => 'NC_WS',
  53. "GT_WT_02" => 'GT_WT_02',
  54. "AURIOL" => 'AURIOL',
  55. "Type1" => 'Type1',
  56. "Mebus" => 'Mebus',
  57. "Eurochron" => 'Eurochron',
  58. "KW9010" => 'KW9010',
  59. "Unknown" => 'Unknown',
  60. );
  61. sub
  62. CUL_TCM97001_Initialize($)
  63. {
  64. my ($hash) = @_;
  65. $hash->{Match} = "^s.....";
  66. $hash->{DefFn} = "CUL_TCM97001_Define";
  67. $hash->{UndefFn} = "CUL_TCM97001_Undef";
  68. $hash->{ParseFn} = "CUL_TCM97001_Parse";
  69. $hash->{AttrList} = "IODev do_not_notify:1,0 ignore:0,1 showtime:1,0 " .
  70. "$readingFnAttributes " .
  71. "model:".join(",", sort keys %models);
  72. $hash->{AutoCreate}=
  73. {
  74. "CUL_TCM97001_Unknown.*" => { GPLOT => "", FILTER => "%NAME", autocreateThreshold => "2:10" },
  75. "CUL_TCM97001.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  76. "Prologue_.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  77. "Mebus_.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  78. "NC_WS.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  79. "ABS700.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  80. "Eurochron.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  81. "TCM21....*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  82. "TCM97..._.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  83. "GT_WT_02.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  84. "Type1.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  85. "Rubicson.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  86. "AURIOL.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  87. "KW9010.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:180"},
  88. "TCM97001.*" => { ATTR => "event-on-change-reading:.*", GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME", autocreateThreshold => "2:340"},
  89. "Unknown_.*" => { autocreateThreshold => "2:10"}
  90. };
  91. }
  92. #############################
  93. sub
  94. CUL_TCM97001_Define($$)
  95. {
  96. my ($hash, $def) = @_;
  97. my @a = split("[ \t][ \t]*", $def);
  98. return "wrong syntax: define <name> CUL_TCM97001 <code>"
  99. if(int(@a) < 3 || int(@a) > 5);
  100. $hash->{CODE} = $a[2];
  101. $hash->{lastT} = 0;
  102. $hash->{lastH} = 0;
  103. $modules{CUL_TCM97001}{defptr}{$a[2]} = $hash;
  104. $hash->{STATE} = "Defined";
  105. return undef;
  106. }
  107. #####################################
  108. sub
  109. CUL_TCM97001_Undef($$)
  110. {
  111. my ($hash, $name) = @_;
  112. delete($modules{CUL_TCM97001}{defptr}{$hash->{CODE}})
  113. if(defined($hash->{CODE}) &&
  114. defined($modules{CUL_TCM97001}{defptr}{$hash->{CODE}}));
  115. return undef;
  116. }
  117. #
  118. # CRC Check for TCM 21....
  119. #
  120. sub checkCRC {
  121. my $msg = shift;
  122. my @a = split("", $msg);
  123. my $bitReverse = undef;
  124. my $x = undef;
  125. foreach $x (@a) {
  126. my $bin3=sprintf("%04b",hex($x));
  127. $bitReverse = $bitReverse . reverse($bin3);
  128. }
  129. my $hexReverse = unpack("H*", pack ("B*", $bitReverse));
  130. #Split reversed a again
  131. my @aReverse = split("", $hexReverse);
  132. my $CRC = (hex($aReverse[0])+hex($aReverse[1])+hex($aReverse[2])+hex($aReverse[3])
  133. +hex($aReverse[4])+hex($aReverse[5])+hex($aReverse[6])+hex($aReverse[7])) & 15;
  134. if ($CRC + hex($aReverse[8]) == 15) {
  135. return TRUE;
  136. }
  137. return FALSE;
  138. }
  139. #
  140. # CRC Check for KW9010....
  141. #
  142. sub checkCRCKW9010 {
  143. my $msg = shift;
  144. Log3 "CUL_TCM97001", 5 , "crc calc for: $msg";
  145. my @a = split("", $msg);
  146. my $bitReverse = undef;
  147. my $x = undef;
  148. foreach $x (@a) {
  149. my $bin3=sprintf("%04b",hex($x));
  150. $bitReverse = $bitReverse . reverse($bin3);
  151. }
  152. my $hexReverse = unpack("H*", pack ("B*", $bitReverse));
  153. #Split reversed a again
  154. my @aReverse = split("", $hexReverse);
  155. my $CRC = (hex($aReverse[0])+hex($aReverse[1])+hex($aReverse[2])+hex($aReverse[3])
  156. +hex($aReverse[4])+hex($aReverse[5])+hex($aReverse[6])+hex($aReverse[7])) & 15;
  157. Log3 "CUL_TCM97001", 5 , "calc crc is: $CRC";
  158. Log3 "CUL_TCM97001", 5 , "ref crc is :".hex($aReverse[8]);
  159. if ($CRC == hex($aReverse[8])) {
  160. return TRUE;
  161. }
  162. return FALSE;
  163. }
  164. #
  165. # CRC Check for Mebus
  166. #
  167. sub checkCRC_Mebus {
  168. my $msg = shift;
  169. my @a = split("", $msg);
  170. my $CRC = ((hex($a[1])+hex($a[2])+hex($a[3])
  171. +hex($a[4])+hex($a[5])+hex($a[6])) -1) & 15;
  172. my $CRCCHECKVAL= (hex($a[0]));
  173. if ($CRC == $CRCCHECKVAL) {
  174. return TRUE;
  175. }
  176. return FALSE;
  177. }
  178. #
  179. # CRC Check for GT_WT_02
  180. #
  181. sub checkCRC_GTWT02 {
  182. my $msg = shift;
  183. my @a = split("", $msg);
  184. my $CRC = (hex($a[0])+hex($a[1])+hex($a[2])+hex($a[3])
  185. +hex($a[4])+hex($a[5])+hex($a[6])+(hex($a[7]) & 0xE));
  186. # my $CRC = (hex($a[0])+hex($a[1])+hex($a[2])+hex($a[3])
  187. # +hex($a[4])+hex($a[5])+hex($a[6])+hex($a[7])) -1;
  188. my $CRCCHECKVAL= (hex($a[7].$a[8].$a[9]) & 0x1F8) >> 3;
  189. if ($CRC == $CRCCHECKVAL) {
  190. return TRUE;
  191. }
  192. return FALSE;
  193. }
  194. #
  195. # CRC Check for Sensor-Type1
  196. #
  197. sub checkCRC_Type1 {
  198. my $msg = shift;
  199. my @a = split("", $msg);
  200. my $CRC = (hex($a[0])+hex($a[1])+hex($a[2])+hex($a[3])
  201. +hex($a[4])+hex($a[5])+hex($a[6])+hex($a[7]));
  202. my $CRCCHECKVAL= (hex($a[7].$a[8].$a[9]) & 0x1F8) >> 3;
  203. if ($CRC == $CRCCHECKVAL) {
  204. return TRUE;
  205. }
  206. return FALSE;
  207. }
  208. sub checkValues {
  209. my $temp = shift;
  210. my $humidy = shift;
  211. if (!defined($humidy)) {
  212. $humidy = 50;
  213. }
  214. if ($temp <= 60 && $temp >= -30
  215. && $humidy >= 0 && $humidy <= 100) {
  216. return TRUE;
  217. }
  218. return FALSE;
  219. }
  220. ###################################
  221. sub
  222. CUL_TCM97001_Parse($$)
  223. {
  224. my $enableLongIDs = TRUE; # Disable short ID support, enable longIDs
  225. my ($hash, $msg) = @_;
  226. $msg = substr($msg, 1);
  227. my @a = split("", $msg);
  228. my $id3 = hex($a[0] . $a[1]);
  229. #my $id4 = hex($a[0] . $a[1] . $a[2] . (hex($a[3]) & 0x3));
  230. my $def = $modules{CUL_TCM97001}{defptr}{$id3}; # test for already defined devices use old naming convention
  231. #my $def2 = $modules{CUL_TCM97001}{defptr}{$idType2};
  232. if(!$def) {
  233. $def = $modules{CUL_TCM97001}{defptr}{'CUL_TCM97001_'.$id3}; # use new naming convention
  234. }
  235. my $now = time();
  236. my $name = "Unknown";
  237. if($def) {
  238. $name = $def->{NAME};
  239. }
  240. my $readedModel = AttrVal($name, "model", "Unknown");
  241. my $syncTimeIndex = rindex($msg,";");
  242. my @syncBit;
  243. if ($syncTimeIndex != -1) {
  244. my $syncTimeMsg = substr($msg, $syncTimeIndex + 1);
  245. @syncBit = split(":", $syncTimeMsg);
  246. $msg = substr($msg, 0, $syncTimeIndex);
  247. } else {
  248. $syncBit[0] = 0;
  249. $syncBit[1] = 4000;
  250. }
  251. my $rssi;
  252. my $l = length($msg);
  253. $rssi = substr($msg, $l-2, 2);
  254. undef($rssi) if ($rssi eq "00");
  255. if (defined($rssi))
  256. {
  257. $rssi = hex($rssi);
  258. $rssi = ($rssi>=128 ? (($rssi-256)/2-74) : ($rssi/2-74)) if defined($rssi);
  259. Log3 $name, 4, "CUL_TCM97001 $name $id3 ($msg) length: $l RSSI: $rssi";
  260. } else {
  261. Log3 $name, 4, "CUL_TCM97001 $name $id3 ($msg) length: $l";
  262. }
  263. my ($msgtype, $msgtypeH);
  264. my $packageOK = FALSE;
  265. my $batbit=undef;
  266. my $mode=undef;
  267. my $trend=undef;
  268. my $hashumidity = FALSE;
  269. my $hasbatcheck = FALSE;
  270. my $hastrend = FALSE;
  271. my $haschannel = FALSE;
  272. my $hasmode = FALSE;
  273. my $model="Unknown";
  274. my $temp = undef;
  275. my $humidity=undef;
  276. my $channel = undef;
  277. my $longids = AttrVal($hash->{NAME},'longids',1);
  278. if (length($msg) == 8) {
  279. # Only tmp TCM device
  280. #eg. 1000 1111 0100 0011 0110 1000 = 21.8C
  281. #eg. --> shift2 0100 0011 0110 10
  282. my $tcm97id = hex($a[0] . $a[1]);
  283. $def = $modules{CUL_TCM97001}{defptr}{$tcm97id};
  284. if($def) {
  285. $name = $def->{NAME};
  286. }
  287. $readedModel = AttrVal($name, "model", "Unknown");
  288. if ($readedModel eq "Unknown" || $readedModel eq "TCM97...") {
  289. $temp = (hex($a[3].$a[4].$a[5]) >> 2) & 0xFFFF;
  290. my $negative = (hex($a[2]) >> 0) & 0x3;
  291. if ($negative == 0x3) {
  292. $temp = (~$temp & 0x03FF) + 1;
  293. $temp = -$temp;
  294. }
  295. $temp = $temp / 10;
  296. if (checkValues($temp, 50)) {
  297. $model="TCM97...";
  298. # I think bit 3 on byte 3 is battery warning
  299. $batbit = (hex($a[2]) >> 0) & 0x4;
  300. $batbit = ~$batbit & 0x1; # Bat bit umdrehen
  301. $mode = (hex($a[5]) >> 0) & 0x1;
  302. my $unknown = (hex($a[4]) >> 0) & 0x2;
  303. if ($mode) {
  304. Log3 $name, 5, "CUL_TCM97001 Mode: manual triggert";
  305. } else {
  306. Log3 $name, 5, "CUL_TCM97001 Mode: auto triggert";
  307. }
  308. if ($unknown) {
  309. Log3 $name, 5, "CUL_TCM97001 Unknown Bit: $unknown";
  310. }
  311. my $deviceCode;
  312. if (!defined($modules{CUL_TCM97001}{defptr}{$tcm97id}))
  313. {
  314. if ( $enableLongIDs == TRUE || (($longids != "0") && ($longids eq "1" || $longids eq "ALL" || (",$longids," =~ m/,$model,/))))
  315. {
  316. $deviceCode="CUL_TCM97001_".$tcm97id;
  317. Log3 $hash,4, "CUL_TCM97001 using longid: $longids model: $model";
  318. } else {
  319. $deviceCode="CUL_TCM97001_" . $model;
  320. }
  321. } else {
  322. $deviceCode=$tcm97id;
  323. }
  324. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  325. if($def) {
  326. $name = $def->{NAME};
  327. }
  328. if(!$def) {
  329. Log3 $name, 2, "CUL_TCM97001 Unknown device $deviceCode, please define it";
  330. return "UNDEFINED $model" . substr($deviceCode, rindex($deviceCode,"_")) . " CUL_TCM97001 $deviceCode";
  331. }
  332. $packageOK = TRUE;
  333. $hasbatcheck = TRUE;
  334. $hasmode = TRUE;
  335. $readedModel=$model;
  336. }
  337. }
  338. if ($readedModel eq "Unknown" || $readedModel eq "ABS700") {
  339. $temp = (hex($a[2].$a[3]) & 0x7F)+(hex($a[5])/10);
  340. if ((hex($a[2]) & 0x8) == 0x8) {
  341. $temp = -$temp;
  342. }
  343. if (checkValues($temp, 50)) {
  344. $model="ABS700";
  345. $batbit = ((hex($a[4]) & 0x8) != 0x8);
  346. $mode = (hex($a[4]) & 0x4) >> 2;
  347. my $deviceCode;
  348. if (!defined($modules{CUL_TCM97001}{defptr}{$tcm97id}))
  349. {
  350. if ( $enableLongIDs == TRUE || (($longids != "0") && ($longids eq "1" || $longids eq "ALL" || (",$longids," =~ m/,$model,/))))
  351. {
  352. $deviceCode="CUL_TCM97001_".$tcm97id;
  353. Log3 $hash,4, "CUL_TCM97001 using longid: $longids model: $model";
  354. } else {
  355. $deviceCode="CUL_TCM97001_" . $model;
  356. }
  357. } else {
  358. $deviceCode=$tcm97id;
  359. }
  360. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  361. if($def) {
  362. $name = $def->{NAME};
  363. }
  364. if(!$def) {
  365. Log3 $name, 2, "CUL_TCM97001 Unknown device $deviceCode, please define it";
  366. return "UNDEFINED $model" . substr($deviceCode, rindex($deviceCode,"_")) . " CUL_TCM97001 $deviceCode";
  367. }
  368. $hasbatcheck = TRUE;
  369. $packageOK = TRUE;
  370. $hasmode = TRUE;
  371. $readedModel=$model;
  372. }
  373. }
  374. } elsif (length($msg) == 10) {
  375. #Log3 $name, 2, "CUL_TCM97001 10er msg: " . $msg;
  376. my $idType2 = hex($a[1] . $a[2]);
  377. my $deviceCode = $idType2;
  378. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode}; # test for already defined devices use old naming convention
  379. if(!$def) {
  380. $deviceCode = "CUL_TCM97001_" . $idType2; # use new naming convention
  381. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  382. }
  383. if($def) {
  384. $name = $def->{NAME};
  385. }
  386. $readedModel = AttrVal($name, "model", "Unknown");
  387. if (checkCRC_Mebus($msg) == TRUE && ($readedModel eq "Unknown" || $readedModel eq "Mebus")) {
  388. # Protocol mebus start everytime with 1001
  389. # Sync bit 9700ms, bit 0 = 350ms, bit 1 = 2000ms
  390. # e.g. 8250ED70 1000 0010 0101 0000 1110 1101 0111
  391. # A B C D E F G
  392. # A = CRC ((B+C+D+E+F+G)-1)
  393. # B+C = Random Address
  394. # D+E+F temp (/10)
  395. # G Bit 4,3 = Channel, Bit 2 = Battery, Bit 1 = Force sending
  396. $temp = (hex($a[3].$a[4].$a[5])) & 0x3FF;
  397. my $negative = (hex($a[3])) & 0xC;
  398. if ($negative == 0xC) {
  399. $temp = (~$temp & 0x03FF) + 1;
  400. $temp = -$temp;
  401. }
  402. $temp = $temp / 10;
  403. if (checkValues($temp, 50)) {
  404. $batbit = (hex($a[6]) & 0x2) >> 1;
  405. #$batbit = ~$batbit & 0x1; # Bat bit umdrehen
  406. $mode = (hex($a[6]) & 0x1);
  407. $channel = (hex($a[6]) & 0xC) >> 2;
  408. $model="Mebus";
  409. my $deviceCode;
  410. if (!defined($modules{CUL_TCM97001}{defptr}{$idType2}))
  411. {
  412. if ( $enableLongIDs == TRUE || (($longids != "0") && ($longids eq "1" || $longids eq "ALL" || (",$longids," =~ m/,$model,/))))
  413. {
  414. $deviceCode="CUL_TCM97001_".$idType2;
  415. Log3 $hash,4, "CUL_TCM97001 using longid: $longids model: $model";
  416. } else {
  417. $deviceCode="CUL_TCM97001_" . $model . "_" . $channel;
  418. }
  419. } else { # Fallback for already defined devices use old naming convention
  420. $deviceCode=$idType2;
  421. }
  422. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  423. if($def) {
  424. $name = $def->{NAME};
  425. }
  426. if(!$def) {
  427. Log3 $name, 2, "CUL_TCM97001 Unknown device $deviceCode, please define it";
  428. return "UNDEFINED $model" . substr($deviceCode, rindex($deviceCode,"_")) . " CUL_TCM97001 $deviceCode";
  429. }
  430. $packageOK = TRUE;
  431. $readedModel=$model;
  432. $hasmode = TRUE;
  433. $hasbatcheck = TRUE;
  434. $haschannel = TRUE;
  435. $id3 = $idType2
  436. } else {
  437. $name = "Unknown";
  438. }
  439. }
  440. if ($packageOK == FALSE) {
  441. my $idType1 = hex($a[0] . $a[1]);
  442. $deviceCode = $idType1;
  443. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode}; # test for already defined devices use old naming convention
  444. #my $def2 = $modules{CUL_TCM97001}{defptr}{$idType2};
  445. #my $def3 = $modules{CUL_TCM97001}{defptr}{$idType3};
  446. if(!$def) {
  447. $deviceCode = "CUL_TCM97001_" . $idType1; # use new naming convention
  448. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  449. }
  450. if($def) {
  451. $name = $def->{NAME};
  452. #} elsif($def2) {
  453. # $def = $def2;
  454. # $name = $def->{NAME};
  455. #} elsif($def3) {
  456. # $def = $def3;
  457. # $name = $def->{NAME};
  458. }
  459. $readedModel = AttrVal($name, "model", "Unknown");
  460. if (($readedModel eq "AURIOL" || $readedModel eq "Unknown")) {
  461. # Implementation from Femduino
  462. # AURIOL (Lidl Version: 09/2013)
  463. # /--------------------------------- Channel, changes after every battery change
  464. # / / ------------------------ Battery state 1 == Ok
  465. # / / /------------------------ Battery changed, Sync startet
  466. # / / / ----------------------- Unknown
  467. # / / / / /--------------------- neg Temp: if 1 then temp = temp - 4096
  468. # / / / / /---------------------- 12 Bit Temperature
  469. # / / / / / /---------- ??? CRC
  470. # / / / / / / /---- Trend 10 == rising, 01 == falling
  471. # 0101 0101 1 0 00 0001 0000 1011 1100 01 00
  472. # Bit 0 8 9 10 12 24 30
  473. $def = $modules{CUL_TCM97001}{defptr}{$idType1};
  474. if($def) {
  475. $name = $def->{NAME};
  476. }
  477. $temp = (hex($a[3].$a[4].$a[5])) & 0x7FF;
  478. my $negative = (hex($a[3])) & 0x8;
  479. if ($negative == 0x8) {
  480. $temp = (~$temp & 0x07FF) + 1;
  481. $temp = -$temp;
  482. }
  483. $temp = $temp / 10;
  484. if (checkValues($temp, 50)) {
  485. $batbit = (hex($a[2]) & 0x8) >> 3;
  486. $batbit = ~$batbit & 0x1; # Bat bit umdrehen
  487. $mode = (hex($a[2]) & 0x4) >> 2;
  488. $trend = (hex($a[7]) & 0x3);
  489. $model="AURIOL";
  490. if ($deviceCode ne $idType1) # new naming convention
  491. {
  492. if ( $enableLongIDs == TRUE || (($longids != "0") && ($longids eq "1" || $longids eq "ALL" || (",$longids," =~ m/,$model,/))))
  493. {
  494. $deviceCode="CUL_TCM97001_".$idType1;
  495. } else {
  496. $deviceCode="CUL_TCM97001_" . $model . "_" . $channel;
  497. }
  498. }
  499. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  500. if($def) {
  501. $name = $def->{NAME};
  502. }
  503. if(!$def) {
  504. Log3 $name, 2, "CUL_TCM97001 Unknown device $deviceCode, please define it";
  505. return "UNDEFINED $model" . substr($deviceCode, rindex($deviceCode,"_")) . " CUL_TCM97001 $deviceCode";
  506. }
  507. $hasbatcheck = TRUE;
  508. $hastrend = TRUE;
  509. $packageOK = TRUE;
  510. $hasmode = TRUE;
  511. $readedModel=$model;
  512. } else {
  513. $name = "Unknown";
  514. }
  515. }
  516. }
  517. } elsif (length($msg) == 12) {
  518. my $bin = undef;
  519. my $deviceCode;
  520. my $idType1 = hex($a[0] . $a[1]);
  521. #my $idType2 = hex($a[1] . $a[2]);
  522. #my $idType3 = hex($a[0] . $a[1] . $a[2] . (hex($a[3]) & 0x3));
  523. $deviceCode = $idType1;
  524. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode}; # test for already defined devices use old naming convention
  525. #my $def2 = $modules{CUL_TCM97001}{defptr}{$idType2};
  526. #my $def3 = $modules{CUL_TCM97001}{defptr}{$idType3};
  527. if(!$def) {
  528. $deviceCode = "CUL_TCM97001_" . $idType1; # use new naming convention
  529. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  530. }
  531. if($def) {
  532. $name = $def->{NAME};
  533. #} elsif($def2) {
  534. # $def = $def2;
  535. # $name = $def->{NAME};
  536. #} elsif($def3) {
  537. # $def = $def3;
  538. # $name = $def->{NAME};
  539. }
  540. $readedModel = AttrVal($name, "model", "Unknown");
  541. Log3 $name, 4, "CUL_TCM97001 Parse Name: $name , devicecode: $deviceCode , Model defined: $readedModel";
  542. if (($readedModel eq "Eurochron" || (hex($a[6]) == 0xF && $readedModel eq "Unknown") && $syncBit[1] < 5000)) {
  543. # EAS 800
  544. # G is every time 1111
  545. #
  546. # 0100 1110 1001 0000 1010 0001 1111 0100 1001
  547. # A B C D E F G H I
  548. #
  549. # A+B = ID = 4E
  550. # C Bit 0 = Bat (1) OK
  551. # C Bit 1-3 = Channel 001 = 1
  552. # D-F = Temp (0000 1010 0001) = 161 ~ 16,1°
  553. # G = Unknown
  554. # H+I = hum (0100 1001) = 73
  555. $def = $modules{CUL_TCM97001}{defptr}{$idType1};
  556. if($def) {
  557. $name = $def->{NAME};
  558. } else {
  559. # Redirect to
  560. my $SD_WS07_ClientMatch=index($hash->{Clients},"SD_WS07");
  561. if ($SD_WS07_ClientMatch == -1) {
  562. # Append Clients and MatchList for CUL
  563. $hash->{Clients} = $hash->{Clients}.":SD_WS07:";
  564. $hash->{MatchList}{"C:SD_WS07"} = "^P7#[A-Fa-f0-9]{6}F[A-Fa-f0-9]{2}";
  565. }
  566. my $dmsg = "P7#" . substr($msg, 0, $l-2, 2);
  567. $hash->{RAWMSG} = $msg;
  568. my %addvals = (RAWMSG => $msg, DMSG => $dmsg);
  569. Log3 $name, 5, "CUL_TCM97001 Dispatch $dmsg to other modul";
  570. Dispatch($hash, $dmsg, \%addvals); ## Dispatch to other Modules
  571. return "";
  572. }
  573. $temp = (hex($a[3].$a[4].$a[5])) & 0x7FF;
  574. my $negative = (hex($a[3])) & 0x8;
  575. if ($negative == 0x8) {
  576. $temp = (~$temp & 0x07FF) + 1;
  577. $temp = -$temp;
  578. }
  579. $temp = $temp / 10;
  580. $humidity = hex($a[7].$a[8]) & 0x7F;
  581. if (checkValues($temp, $humidity)) {
  582. $batbit = (hex($a[2]) & 0x8) >> 3;
  583. #$batbit = ~$batbit & 0x1; # Bat bit umdrehen
  584. $mode = (hex($a[2]) & 0x4) >> 2;
  585. $channel = ((hex($a[2])) & 0x3) + 1;
  586. $model="Eurochron";
  587. if ($deviceCode ne $idType1) # new naming convention
  588. {
  589. if ( $enableLongIDs == TRUE || (($longids != "0") && ($longids eq "1" || $longids eq "ALL" || (",$longids," =~ m/,$model,/))))
  590. {
  591. Log3 $hash,4, "CUL_TCM97001 using longid: $longids model: $model";
  592. } else {
  593. $deviceCode="CUL_TCM97001_" . $model . "_" . $channel;
  594. }
  595. }
  596. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  597. if($def) {
  598. $name = $def->{NAME};
  599. }
  600. if(!$def) {
  601. Log3 $name, 2, "CUL_TCM97001 Unknown device $deviceCode, please define it";
  602. return "UNDEFINED $model" . substr($deviceCode, rindex($deviceCode,"_")) . " CUL_TCM97001 $deviceCode";
  603. }
  604. if (defined($humidity)) {
  605. if ($humidity >= 20) {
  606. $hashumidity = TRUE;
  607. }
  608. }
  609. $hasbatcheck = TRUE;
  610. $haschannel = TRUE;
  611. $packageOK = TRUE;
  612. $hasmode = TRUE;
  613. $readedModel=$model;
  614. } else {
  615. $name = "Unknown";
  616. }
  617. }
  618. if (checkCRC($msg) == TRUE && ($readedModel eq "Unknown" || $readedModel eq "TCM21....")) {
  619. # Long with tmp
  620. # All nibbles must be reversed
  621. # e.g. 154E800480 0001 0101 0100 1110 1000 0000 0000 0100 1000 0000
  622. # A B C D E F G H I
  623. # A+B = Addess
  624. # C Bit 1 Battery
  625. # D+E+F Temp
  626. # G+H Hum
  627. # I CRC
  628. my @a = split("", $msg);
  629. my $bitReverse = undef;
  630. my $x = undef;
  631. foreach $x (@a) {
  632. my $bin3=sprintf("%024b",hex($x));
  633. $bitReverse = $bitReverse . substr(reverse($bin3),0,4);
  634. }
  635. my $hexReverse = unpack("H*", pack ("B*", $bitReverse));
  636. #Split reversed a again
  637. my @aReverse = split("", $hexReverse);
  638. if (hex($aReverse[5]) > 3) {
  639. # negative temp
  640. $temp = ((hex($aReverse[3]) + hex($aReverse[4]) * 16 + hex($aReverse[5]) * 256));
  641. $temp = (~$temp & 0x03FF) + 1;
  642. $temp = -$temp/10;
  643. } else {
  644. # positive temp
  645. $temp = (hex($aReverse[3]) + hex($aReverse[4]) * 16 + hex($aReverse[5]) * 256)/10;
  646. }
  647. $humidity = hex($aReverse[7]).hex($aReverse[6]);
  648. if (checkValues($temp, $humidity)) {
  649. $batbit = (hex($a[2]) & 0x8) >> 3;
  650. #$mode = (hex($a[2]) & 0x4) >> 2;
  651. $model="TCM21....";
  652. if ($deviceCode ne $idType1) # new naming convention
  653. {
  654. if ( $enableLongIDs == TRUE || (($longids != "0") && ($longids eq "1" || $longids eq "ALL" || (",$longids," =~ m/,$model,/))))
  655. {
  656. Log3 $hash,4, "CUL_TCM97001 using longid: $longids model: $model";
  657. } else {
  658. $deviceCode="CUL_TCM97001_" . $model . "_" . $channel;
  659. }
  660. }
  661. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  662. if($def) {
  663. $name = $def->{NAME};
  664. }
  665. if(!$def) {
  666. Log3 $name, 2, "CUL_TCM97001 Unknown device $deviceCode, please define it";
  667. return "UNDEFINED $model" . substr($deviceCode, rindex($deviceCode,"_")) . " CUL_TCM97001 $deviceCode";
  668. }
  669. $hashumidity = TRUE;
  670. $packageOK = TRUE;
  671. $hasbatcheck = TRUE;
  672. $readedModel=$model;
  673. } else {
  674. $name = "Unknown";
  675. }
  676. }
  677. if (checkCRC_GTWT02($msg) == TRUE && ($readedModel eq "GT_WT_02" || $readedModel eq "Type1" || $readedModel eq "Unknown")
  678. || checkCRC_Type1($msg) == TRUE && ($readedModel eq "Type1" || $readedModel eq "GT_WT_02" || $readedModel eq "Unknown")) {
  679. # F F 0 0 F 9 5 5 F
  680. # 1111 1111 0000 0000 1111 1001 0101 0101 1111
  681. # A B C D E F G H I
  682. # A+B = Zufällige Code wechelt beim Batteriewechsel
  683. # C Bit 4 Battery, 3 Manual, 2+1 Channel
  684. # D+E+F Temperatur, wenn es negativ wird muss man negieren und dann 1 addieren, wie im ersten Post beschrieben.
  685. # G+H Hum - bit 0-7
  686. # I CRC?
  687. #$def = $modules{CUL_TCM97001}{defptr}{$idType3};
  688. $temp = (hex($a[3].$a[4].$a[5])) & 0x3FF;
  689. my $negative = (hex($a[3])) & 0xC;
  690. if ($negative == 0xC) {
  691. $temp = (~$temp & 0x03FF) + 1;
  692. $temp = -$temp;
  693. }
  694. $temp = $temp / 10;
  695. $humidity = (hex($a[6].$a[7]) & 0x0FE) >> 1; # only the first 7 bits are the humidity
  696. if ($humidity > 100) {
  697. # HH - Workaround
  698. $humidity = 100;
  699. } elsif ($humidity < 20) {
  700. # LL - Workaround
  701. $humidity = 20;
  702. }
  703. if (checkValues($temp, $humidity)) {
  704. $channel = ((hex($a[2])) & 0x3) + 1;
  705. $batbit = ((hex($a[2]) & 0x8) != 0x8);
  706. $mode = (hex($a[2]) & 0x4) >> 2;
  707. if (checkCRC_GTWT02($msg) == TRUE) {
  708. $model="GT_WT_02";
  709. } else {
  710. $model="Type1";
  711. }
  712. if ($deviceCode ne $idType1) # new naming convention
  713. {
  714. if ( $enableLongIDs == TRUE || (($longids != "0") && ($longids eq "1" || $longids eq "ALL" || (",$longids," =~ m/,$model,/))))
  715. {
  716. Log3 $hash,4, "CUL_TCM97001 using longid: $longids model: $model";
  717. } else {
  718. $deviceCode="CUL_TCM97001_" . $model . "_" . $channel;
  719. }
  720. }
  721. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  722. if($def) {
  723. $name = $def->{NAME};
  724. }
  725. if(!$def) {
  726. Log3 $name, 2, "CUL_TCM97001 Unknown device $deviceCode, please define it";
  727. return "UNDEFINED $model" . substr($deviceCode, rindex($deviceCode,"_")) . " CUL_TCM97001 $deviceCode";
  728. }
  729. $hashumidity = TRUE;
  730. $hasbatcheck = TRUE;
  731. $haschannel = TRUE;
  732. $hasmode = TRUE;
  733. $packageOK = TRUE;
  734. $readedModel=$model;
  735. } else {
  736. $name = "Unknown";
  737. }
  738. }
  739. #Log3 $name, 4, "CUL_TCM97001: CRC for TCM21.... Failed, checking other protocolls";
  740. # Check for Prologue
  741. if ($readedModel eq "Prologue" || (hex($a[0]) == 0x9 && $readedModel eq "Unknown")) {
  742. # Protocol prologue start everytime with 1001
  743. # e.g. 91080F614C 1001 0001 0000 1000 0000 1111 0110 0001 0100 1100
  744. # A B C D E F G H I
  745. # A = Startbit 1001
  746. # B+C = Random Address
  747. # D Bit 4 Battery, 3 Manual, 2+1 Channel
  748. # E+F+G Bit 15+16 negativ temp, 14-0 temp
  749. # H+I Hum
  750. #$def = $modules{CUL_TCM97001}{defptr}{$idType3};
  751. #$def = $modules{CUL_TCM97001}{defptr}{$idType1};
  752. $temp = (hex($a[4].$a[5].$a[6])) & 0x3FFF;
  753. my $negative = (hex($a[4])) & 0xC;
  754. if ($negative == 0xC) {
  755. $temp = (~$temp & 0x03FF) + 1;
  756. $temp = -$temp;
  757. }
  758. $temp = $temp / 10;
  759. if (!(hex($a[7]) == 0xC && hex($a[8]) == 0xC)) {
  760. $humidity = hex($a[7].$a[8]);
  761. }
  762. if (checkValues($temp, $humidity)) {
  763. $channel = (hex($a[3])) & 0x3;
  764. $batbit = (hex($a[3]) & 0x8) >> 3;
  765. $batbit = ~$batbit & 0x1; # Bat bit umdrehen
  766. $mode = (hex($a[3]) & 0x4) >> 2;
  767. $model="Prologue";
  768. if ($deviceCode ne $idType1) # new naming convention
  769. {
  770. if ( $enableLongIDs == TRUE || (($longids != "0") && ($longids eq "1" || $longids eq "ALL" || (",$longids," =~ m/,$model,/))))
  771. {
  772. Log3 $hash,4, "CUL_TCM97001 using longid: $longids model: $model";
  773. } else {
  774. $deviceCode="CUL_TCM97001_" . $model . "_" . $channel;
  775. }
  776. }
  777. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  778. if($def) {
  779. $name = $def->{NAME};
  780. }
  781. if(!$def) {
  782. Log3 $name, 2, "CUL_TCM97001 Unknown device $deviceCode, please define it";
  783. return "UNDEFINED $model" . substr($deviceCode, rindex($deviceCode,"_")) . " CUL_TCM97001 $deviceCode";
  784. }
  785. if (defined($humidity)) {
  786. if ($humidity >= 20) {
  787. $hashumidity = TRUE;
  788. }
  789. }
  790. $hasbatcheck = TRUE;
  791. $hasmode = TRUE;
  792. $packageOK = TRUE;
  793. $haschannel = TRUE;
  794. $readedModel=$model;
  795. } else {
  796. $name = "Unknown";
  797. }
  798. }
  799. if ($readedModel eq "NC_WS" || (hex($a[0]) == 0x5 && $readedModel eq "Unknown")) {
  800. # Implementation from Femduino
  801. # PEARL NC7159, LogiLink WS0002
  802. # /--------------------------------- Sensdortype
  803. # / / ---------------------------- ID, changes after every battery change
  804. # / / /--------------------- Battery state 0 == Ok
  805. # / / / / ------------------ forced send
  806. # / / / / / ---------------- Channel (0..2)
  807. # / / / / / / -------------- neg Temp: if 1 then temp = temp - 2048
  808. # / / / / / / / ----------- Temp
  809. # / / / / / / / /-- unknown
  810. # / / / / / / / / / Humidity
  811. # 0101 0010 1001 0 0 00 0 010 0011 0000 1 101 1101
  812. # Bit 0 4 12 13 14 16 17 28 29 36
  813. #$def = $modules{CUL_TCM97001}{defptr}{$idType3};
  814. $temp = (hex($a[4].$a[5].$a[6])) & 0x7FFF;
  815. my $negative = (hex($a[4])) & 0x8;
  816. if ($negative == 0x8) {
  817. $temp = (~$temp & 0x07FF) + 1;
  818. $temp = -$temp;
  819. }
  820. $temp = $temp / 10;
  821. $humidity = hex($a[7].$a[8]) & 0x7F;
  822. if (checkValues($temp, $humidity)) {
  823. $model="NC_WS";
  824. $channel = (hex($a[3])) & 0x3;
  825. $batbit = (hex($a[3]) & 0x8) >> 3;
  826. $batbit = ~$batbit & 0x1; # Bat bit umdrehen
  827. $mode = (hex($a[3]) & 0x4) >> 2;
  828. if ($deviceCode ne $idType1) # new naming convention
  829. {
  830. if ( $enableLongIDs == TRUE || (($longids != "0") && ($longids eq "1" || $longids eq "ALL" || (",$longids," =~ m/,$model,/))))
  831. {
  832. Log3 $hash,4, "CUL_TCM97001 using longid: $longids model: $model";
  833. } else {
  834. $deviceCode="CUL_TCM97001_" . $model . "_" . $channel;
  835. }
  836. }
  837. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  838. if($def) {
  839. $name = $def->{NAME};
  840. }
  841. if(!$def) {
  842. Log3 $name, 2, "CUL_TCM97001 Unknown device $deviceCode, please define it";
  843. return "UNDEFINED $model" . substr($deviceCode, rindex($deviceCode,"_")) . " CUL_TCM97001 $deviceCode";
  844. }
  845. $hashumidity = TRUE;
  846. $hasbatcheck = TRUE;
  847. $hasmode = TRUE;
  848. $packageOK = TRUE;
  849. $haschannel = TRUE;
  850. $readedModel=$model;
  851. } else {
  852. $name = "Unknown";
  853. }
  854. }
  855. if ($readedModel eq "Rubicson" || (hex($a[2]) == 0x8 && $readedModel eq "Unknown")) {
  856. # Protocol Rubicson has as nibble C every time 1000
  857. # e.g. F4806B8E14 1111 0100 1000 0000 0110 1011 1000 1110 0001 0100
  858. # A B C D E F G H I
  859. # A+B = Random Address
  860. # C = Rubicson = 1000
  861. # D+E+F 12 bit temp
  862. # G+H+I Unknown
  863. $def = $modules{CUL_TCM97001}{defptr}{$idType1};
  864. if($def) {
  865. $name = $def->{NAME};
  866. }
  867. $temp = (hex($a[3].$a[4].$a[5])) & 0x3FF;
  868. my $negative = (hex($a[3])) & 0xC;
  869. if ($negative == 0xC) {
  870. $temp = (~$temp & 0x03FF) + 1;
  871. $temp = -$temp;
  872. }
  873. $temp = $temp / 10;
  874. if (checkValues($temp, 50)) {
  875. $model="Rubicson";
  876. if ($deviceCode ne $idType1) # new naming convention
  877. {
  878. if ( $enableLongIDs == TRUE || (($longids != "0") && ($longids eq "1" || $longids eq "ALL" || (",$longids," =~ m/,$model,/))))
  879. {
  880. Log3 $hash,4, "CUL_TCM97001 using longid: $longids model: $model";
  881. } else {
  882. $deviceCode="CUL_TCM97001_" . $model . "_" . $channel;
  883. }
  884. }
  885. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  886. if($def) {
  887. $name = $def->{NAME};
  888. }
  889. if(!$def) {
  890. Log3 $name, 2, "CUL_TCM97001 Unknown device $deviceCode, please define it";
  891. return "UNDEFINED $model" . substr($deviceCode, rindex($deviceCode,"_")) . " CUL_TCM97001 $deviceCode";
  892. }
  893. $packageOK = TRUE;
  894. $readedModel=$model;
  895. } else {
  896. $name = "Unknown";
  897. }
  898. }
  899. #if (($readedModel eq "Unknown" || $readedModel eq "KW9010")) {
  900. if (checkCRCKW9010($msg) == TRUE && ($readedModel eq "Unknown" || $readedModel eq "KW9010")) {
  901. # Re: Tchibo Wetterstation 433 MHz - KW9010
  902. # See also http://forum.arduino.cc/index.php?PHPSESSID=ffoeoe9qeuv7rf4fh0d637hd74&topic=136836.msg1536416#msg1536416
  903. # /------------------------------------- Random ID part one
  904. # / / -------------------------------- Channel switch
  905. # / / /------------------------------- Random ID part two
  906. # / / / / ---------------------------- Battery state 0 == Ok
  907. # / / / / / --------------------------- Trend (continous, rising, falling
  908. # / / / / / / ------------------------- forced send
  909. # / / / / / / / ----------------------- Temperature
  910. # / / / / / / / /-------------- Temperature sign bit. if 1 then temp = temp - 4096
  911. # / / / / / / / / /------------ Humidity
  912. # / / / / / / / / / /----- Checksum
  913. # 0110 00 10 1 00 1 000000100011 00001101 1101
  914. # 0110 01 00 0 10 1 100110001001 00001011 0101
  915. # Bit 0 4 6 8 9 11 12 24 32
  916. #
  917. #5922B07BC0 42 21.2 66
  918. # 0101 10 01 0 01 0 001010110000 01111011 1100 0000
  919. # 000011010100 11011110
  920. # 212 222-156=66
  921. my @a = split("", $msg);
  922. my $bitReverse = undef;
  923. my $x = undef;
  924. foreach $x (@a) {
  925. $bitReverse = $bitReverse . reverse(sprintf("%04b",hex($x)));
  926. }
  927. Log3 $hash, 5 , "KW9010 CRC Matched: ($bitReverse)";
  928. my $hexReverse = unpack("H*", pack ("B*", $bitReverse));
  929. #Split reversed a again
  930. my @aReverse = split("", $hexReverse);
  931. if (hex($aReverse[5]) > 3) {
  932. # negative temp
  933. $temp = ((hex($aReverse[3]) + hex($aReverse[4]) * 16 + hex($aReverse[5]) * 256));
  934. $temp = (~$temp & 0x03FF) + 1;
  935. $temp = -$temp/10;
  936. } else {
  937. # positive temp
  938. $temp = (hex($aReverse[3]) + hex($aReverse[4]) * 16 + hex($aReverse[5]) * 256)/10;
  939. }
  940. $humidity = hex($aReverse[7].$aReverse[6]) - 156;
  941. if (checkValues($temp, $humidity)) {
  942. Log3 $hash, 5 , "KW9010 values are matching";
  943. $batbit = (hex($a[2]) & 0x8) >> 3;
  944. #$mode = (hex($a[2]) & 0x4) >> 2;
  945. $channel = ((hex($a[1])) & 0xC) >> 2;
  946. $mode = (hex($a[2]) & 0x1);
  947. $trend = (hex($a[2]) & 0x6) >> 1;
  948. $model="KW9010";
  949. if ($deviceCode ne $idType1) # new naming convention
  950. {
  951. if ( $enableLongIDs == TRUE || (($longids != "0") && ($longids eq "1" || $longids eq "ALL" || (",$longids," =~ m/,$model,/))))
  952. {
  953. Log3 $hash,4, "CUL_TCM97001 using longid: $longids model: $model";
  954. } else {
  955. $deviceCode="CUL_TCM97001_" . $model . "_" . $channel;
  956. }
  957. }
  958. $def = $modules{CUL_TCM97001}{defptr}{$deviceCode};
  959. if($def) {
  960. $name = $def->{NAME};
  961. }
  962. if(!$def) {
  963. Log3 $name, 2, "CUL_TCM97001 Unknown device $deviceCode, please define it";
  964. return "UNDEFINED $model" . substr($deviceCode, rindex($deviceCode,"_")) . " CUL_TCM97001 $deviceCode";
  965. }
  966. $hashumidity = TRUE;
  967. $packageOK = TRUE;
  968. $hasbatcheck = TRUE;
  969. $hastrend = TRUE;
  970. $haschannel = TRUE;
  971. $readedModel=$model;
  972. } else {
  973. Log3 $hash, 5 , "KW9010 t:$temp / h:$humidity mismatch";
  974. $name = "Unknown";
  975. }
  976. }
  977. }
  978. if ($packageOK == TRUE) {
  979. if($def) {
  980. $def->{lastT} = $now;
  981. }
  982. readingsBeginUpdate($def);
  983. my ($val, $valH, $state);
  984. $msgtype = "temperature";
  985. $val = sprintf("%2.1f", ($temp) );
  986. $state="T: $val";
  987. # if ($hashumidity == TRUE) {
  988. # if ($model eq "Prologue") {
  989. # # plausibility check
  990. # my $oldhumidity = ReadingsVal($name, "humidity", "unknown");
  991. # if ($oldhumidity eq "unknown" || ($humidity+15 > $oldhumidity && $humidity-15 < $oldhumidity)) {
  992. # $hashumidity = TRUE;
  993. # } else {
  994. # $hashumidity = FALSE;
  995. # }
  996. # }
  997. # }
  998. if ($hashumidity == TRUE) {
  999. $msgtypeH = "humidity";
  1000. $valH = $humidity;
  1001. $state="$state H: $valH";
  1002. Log3 $name, 4, "CUL_TCM97001 $msgtype $name $id3 T: $val H: $valH";
  1003. } else {
  1004. Log3 $name, 4, "CUL_TCM97001 $msgtype $name $id3 T: $val";
  1005. }
  1006. if($hastrend) {
  1007. my $readTrend = ReadingsVal($name, "trend", "unknown");
  1008. if ($trend == 1) {
  1009. if ($readTrend ne "falling") { readingsBulkUpdate($def, "trend", "falling"); }
  1010. } else {
  1011. if ($readTrend ne "rising") { readingsBulkUpdate($def, "trend", "rising"); }
  1012. }
  1013. }
  1014. if ($hasbatcheck) {
  1015. my $battery = ReadingsVal($name, "battery", "unknown");
  1016. if ($batbit) {
  1017. if ($battery ne "ok") { readingsBulkUpdate($def, "battery", "ok"); }
  1018. } else {
  1019. if ($battery ne "low") { readingsBulkUpdate($def, "battery", "low"); }
  1020. }
  1021. }
  1022. if ($hasmode) {
  1023. my $modeVal = ReadingsVal($name, "mode", "unknown");
  1024. if ($mode) {
  1025. if ($modeVal ne "forced") { readingsBulkUpdate($def, "mode", "forced"); }
  1026. } else {
  1027. if ($modeVal ne "normal") { readingsBulkUpdate($def, "mode", "normal"); }
  1028. }
  1029. }
  1030. if ($haschannel) {
  1031. my $readChannel = ReadingsVal($name, "channel", "");
  1032. if (defined($readChannel) && $readChannel ne $channel) { readingsBulkUpdate($def, "channel", $channel); }
  1033. }
  1034. # if ($model eq "Prologue" || $model eq "Eurochron") {
  1035. # # plausibility check
  1036. # my $oldtemp = ReadingsVal($name, "temperature", "unknown");
  1037. # if ($oldtemp eq "unknown" || ($val+5 > $oldtemp && $val-5 < $oldtemp)) {
  1038. # readingsBulkUpdate($def, $msgtype, $val);
  1039. # }
  1040. # } else {
  1041. readingsBulkUpdate($def, $msgtype, $val);
  1042. # }
  1043. if ($hashumidity == TRUE) {
  1044. readingsBulkUpdate($def, $msgtypeH, $valH);
  1045. }
  1046. readingsBulkUpdate($def, "state", $state);
  1047. # for testing only
  1048. #my $rawlen = length($msg);
  1049. #my $rawVal = substr($msg, 0, $rawlen-2);
  1050. #readingsBulkUpdate($def, "RAW", $rawVal);
  1051. readingsEndUpdate($def, 1);
  1052. if(defined($rssi)) {
  1053. $def->{RSSI} = $rssi;
  1054. }
  1055. $attr{$name}{model} = $model;
  1056. return $name;
  1057. } else {
  1058. if (length($msg) == 8 || length($msg) == 10 || length($msg) == 12 || length($msg) == 14) {
  1059. my $defUnknown = $modules{CUL_TCM97001}{defptr}{"CUL_TCM97001_Unknown"};
  1060. if (!$defUnknown) {
  1061. Log3 "Unknown", 2, "CUL_TCM97001 Unknown device Unknown, please define it";
  1062. return "UNDEFINED Unknown CUL_TCM97001 CUL_TCM97001_Unknown";
  1063. }
  1064. $name = $defUnknown->{NAME};
  1065. Log3 $name, 4, "CUL_TCM97001 Device not implemented yet name Unknown msg $msg";
  1066. my $rawlen = length($msg);
  1067. my $rawVal = substr($msg, 0, $rawlen-2);
  1068. my $state="Code: $rawVal";
  1069. if ($defUnknown) {
  1070. $defUnknown->{lastT} = $now;
  1071. }
  1072. $attr{$name}{model} = $model;
  1073. readingsBeginUpdate($defUnknown);
  1074. readingsBulkUpdate($defUnknown, "state", $state);
  1075. # for testing only
  1076. #readingsBulkUpdate($defUnknown, "RAW", $rawVal);
  1077. readingsEndUpdate($defUnknown, 1);
  1078. if(defined($rssi)) {
  1079. $defUnknown->{RSSI} = $rssi;
  1080. }
  1081. #my $defSvg = $defs{"SVG_CUL_TCM97001_Unknown"};
  1082. #if ($defSvg) {
  1083. # CommandDelete(undef, $defSvg->{NAME});
  1084. #}
  1085. return $name;
  1086. }
  1087. }
  1088. return undef;
  1089. }
  1090. 1;
  1091. =pod
  1092. =item summary This module interprets temperature sensor messages.
  1093. =item summary_DE Module verarbeitet empfangene Nachrichten von Temp-Sensoren.
  1094. =begin html
  1095. <a name="CUL_TCM97001"></a>
  1096. <h3>CUL_TCM97001</h3>
  1097. <ul>
  1098. The CUL_TCM97001 module interprets temperature sensor messages received by a Device like CUL, CUN, SIGNALduino etc.<br>
  1099. <br>
  1100. <b>Supported models:</b>
  1101. <ul>
  1102. <li>TCM97...</li>
  1103. <li>ABS700</li>
  1104. <li>TCM21....</li>
  1105. <li>Prologue</li>
  1106. <li>Rubicson</li>
  1107. <li>NC_WS</li>
  1108. <li>GT_WT_02</li>
  1109. <li>AURIOL</li>
  1110. <li>Eurochron</li>
  1111. <li>KW9010</li>
  1112. </ul>
  1113. <br>
  1114. New received device packages are add in fhem category CUL_TCM97001 with autocreate.
  1115. <br><br>
  1116. <a name="CUL_TCM97001_Define"></a>
  1117. <b>Define</b>
  1118. <ul>The received devices created automatically.<br>
  1119. The ID of the defive are the first two Hex values of the package as dezimal.<br>
  1120. </ul>
  1121. <br>
  1122. <a name="CUL_TCM97001 Events"></a>
  1123. <b>Generated events:</b>
  1124. <ul>
  1125. <li>temperature: The temperature</li>
  1126. <li>humidity: The humidity (if available)</li>
  1127. <li>battery: The battery state: low or ok (if available)</li>
  1128. <li>channel: The Channelnumber (if available)</li>
  1129. <li>trend: The temperature trend (if available)</li>
  1130. </ul>
  1131. <br>
  1132. <b>Attributes</b>
  1133. <ul>
  1134. <li><a href="#IODev">IODev</a>
  1135. Note: by setting this attribute you can define different sets of 8
  1136. devices in FHEM, each set belonging to a Device which is capable of receiving the signals. It is important, however,
  1137. that a device is only received by the defined IO Device, e.g. by using
  1138. different Frquencies (433MHz vs 868MHz)
  1139. </li>
  1140. <li><a href="#do_not_notify">do_not_notify</a></li>
  1141. <li><a href="#ignore">ignore</a></li>
  1142. <li><a href="#model">model</a> (TCM97..., ABS700, TCM21...., Prologue, Rubicson, NC_WS, GT_WT_02, AURIOL, KW9010, Unknown)</li>
  1143. <li><a href="#showtime">showtime</a></li>
  1144. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  1145. </ul>
  1146. </ul>
  1147. =end html
  1148. =begin html_DE
  1149. <a name="CUL_TCM97001"></a>
  1150. <h3>CUL_TCM97001</h3>
  1151. <ul>
  1152. Das CUL_TCM97001 Module verarbeitet von einem IO Gerät (CUL, CUN, SIGNALDuino, etc.) empfangene Nachrichten von Temperatur-Sensoren.<br>
  1153. <br>
  1154. <b>Unterstütze Modelle:</b>
  1155. <ul>
  1156. <li>TCM97...</li>
  1157. <li>ABS700</li>
  1158. <li>TCM21....</li>
  1159. <li>Prologue</li>
  1160. <li>Rubicson</li>
  1161. <li>NC_WS</li>
  1162. <li>GT_WT_02</li>
  1163. <li>AURIOL</li>
  1164. <li>Eurochron</li>
  1165. <li>KW9010</li>
  1166. </ul>
  1167. <br>
  1168. Neu empfangene Sensoren werden in der fhem Kategory CUL_TCM97001 per autocreate angelegt.
  1169. <br><br>
  1170. <a name="CUL_TCM97001_Define"></a>
  1171. <b>Define</b>
  1172. <ul>Die empfangenen Sensoren werden automatisch angelegt.<br>
  1173. Die ID der angelgten Sensoren sind die ersten zwei HEX Werte des empfangenen Paketes in dezimaler Schreibweise.<br>
  1174. </ul>
  1175. <br>
  1176. <a name="CUL_TCM97001 Events"></a>
  1177. <b>Generierte Events:</b>
  1178. <ul>
  1179. <li>temperature: Die aktuelle Temperatur</li>
  1180. <li>humidity: Die aktuelle Luftfeutigkeit (falls verfügbar)</li>
  1181. <li>battery: Der Batteriestatus: low oder ok (falls verfügbar)</li>
  1182. <li>channel: Kanalnummer (falls verfügbar)</li>
  1183. <li>trend: Der Temperaturtrend (falls verfügbar)</li>
  1184. </ul>
  1185. <br>
  1186. <b>Attribute</b>
  1187. <ul>
  1188. <li><a href="#IODev">IODev</a>
  1189. Spezifiziert das physische Ger&auml;t, das die Ausstrahlung der Befehle f&uuml;r das
  1190. "logische" Ger&auml;t ausf&uuml;hrt. Ein Beispiel f&uuml;r ein physisches Ger&auml;t ist ein CUL.<br>
  1191. </li>
  1192. <li><a href="#do_not_notify">do_not_notify</a></li>
  1193. <li><a href="#ignore">ignore</a></li>
  1194. <li><a href="#model">model</a> (TCM97..., ABS700, TCM21...., Prologue, Rubicson, NC_WS, GT_WT_02, AURIOL, KW9010, Unknown)</li>
  1195. <li><a href="#showtime">showtime</a></li>
  1196. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  1197. </ul>
  1198. </ul>
  1199. =end html_DE
  1200. =cut