14_Hideki.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. ##############################################
  2. # $Id: 14_Hideki.pm 12722 2016-12-06 21:03:14Z mrsidey $
  3. # The file is taken from the SIGNALduino project
  4. # see http://www.fhemwiki.de/wiki/SIGNALduino
  5. # and was modified by a few additions
  6. # to support Hideki Sensors
  7. # S. Butzek & HJGode & Ralf9 2015-2016
  8. #
  9. package main;
  10. use strict;
  11. use warnings;
  12. use POSIX;
  13. #use Data::Dumper;
  14. #####################################
  15. sub
  16. Hideki_Initialize($)
  17. {
  18. my ($hash) = @_;
  19. $hash->{Match} = "^P12#75[A-F0-9]{17,30}"; # Laenge (Anhahl nibbles nach 0x75 )noch genauer spezifizieren
  20. $hash->{DefFn} = "Hideki_Define";
  21. $hash->{UndefFn} = "Hideki_Undef";
  22. $hash->{AttrFn} = "Hideki_Attr";
  23. $hash->{ParseFn} = "Hideki_Parse";
  24. $hash->{AttrList} = "IODev do_not_notify:0,1 showtime:0,1 "
  25. ."ignore:0,1 "
  26. ." $readingFnAttributes";
  27. $hash->{AutoCreate}=
  28. { "Hideki.*" => { ATTR => "event-min-interval:.*:300 event-on-change-reading:.*", FILTER => "%NAME", GPLOT => "temp4hum4:Temp/Hum,", autocreateThreshold => "2:180"} };
  29. }
  30. #####################################
  31. sub
  32. Hideki_Define($$)
  33. {
  34. my ($hash, $def) = @_;
  35. my @a = split("[ \t][ \t]*", $def);
  36. return "wrong syntax: define <name> Hideki <code>".int(@a)
  37. if(int(@a) < 3);
  38. $hash->{CODE} = $a[2];
  39. $hash->{lastMSG} = "";
  40. my $name= $hash->{NAME};
  41. $modules{Hideki}{defptr}{$a[2]} = $hash;
  42. $hash->{STATE} = "Defined";
  43. #AssignIoPort($hash);
  44. return undef;
  45. }
  46. #####################################
  47. sub
  48. Hideki_Undef($$)
  49. {
  50. my ($hash, $name) = @_;
  51. delete($modules{Hideki}{defptr}{$hash->{CODE}}) if($hash && $hash->{CODE});
  52. return undef;
  53. }
  54. #####################################
  55. sub
  56. Hideki_Parse($$)
  57. {
  58. my ($iohash,$msg) = @_;
  59. my (undef ,$rawData) = split("#",$msg);
  60. my $name = $iohash->{NAME};
  61. my @a = split("", $msg);
  62. Log3 $iohash, 4, "Hideki_Parse $name incomming $msg";
  63. # decrypt bytes
  64. my $decodedString = decryptBytes($rawData); # decrpyt hex string to hex string
  65. #convert dectypted hex str back to array of bytes:
  66. my @decodedBytes = map { hex($_) } ($decodedString =~ /(..)/g);
  67. if (!@decodedBytes)
  68. {
  69. Log3 $iohash, 4, "$name decrypt failed";
  70. return '';
  71. }
  72. my $sensorTyp=getSensorType($decodedBytes[3]);
  73. Log3 $iohash, 4, "Hideki_Parse SensorTyp = $sensorTyp decodedString = $decodedString";
  74. if (!Hideki_crc(\@decodedBytes))
  75. {
  76. Log3 $iohash, 4, "$name crc failed";
  77. return '';
  78. }
  79. my $id=substr($decodedString,2,2); # get the random id from the data
  80. my $channel=0;
  81. my $temp=0;
  82. my $hum=0;
  83. my $rain=0;
  84. my $rc;
  85. my $val;
  86. my $bat;
  87. my $deviceCode;
  88. my $model= "Hideki_$sensorTyp";
  89. ## 1. Detect what type of sensor we have, then call specific function to decode
  90. if ($sensorTyp==0x1E){
  91. ($channel, $temp, $hum) = decodeThermoHygro(\@decodedBytes); # decodeThermoHygro($decodedString);
  92. $bat = ($decodedBytes[2] >> 6 == 3) ? 'ok' : 'low'; # decode battery
  93. $val = "T: $temp H: $hum Bat: $bat";
  94. }elsif($sensorTyp==0x0E){
  95. ($channel, $rain) = decodeRain(\@decodedBytes); # decodeThermoHygro($decodedString);
  96. $bat = ($decodedBytes[2] >> 6 == 3) ? 'ok' : 'low'; # decode battery
  97. $val = "R: $rain Bat: $bat";
  98. }
  99. else{
  100. Log3 $iohash, 4, "$name Sensor Typ $sensorTyp not supported, please report sensor information!";
  101. return "";
  102. }
  103. my $longids = AttrVal($iohash->{NAME},'longids',0);
  104. if ( ($longids ne "0") && ($longids eq "1" || $longids eq "ALL" || (",$longids," =~ m/,$model,/)))
  105. {
  106. $deviceCode=$model . "_" . $id . "." . $channel;
  107. Log3 $iohash,4, "$name using longid: $longids model: $model";
  108. } else {
  109. $deviceCode = $model . "_" . $channel;
  110. }
  111. Log3 $iohash, 4, "$name decoded Hideki protocol model=$model, sensor id=$id, channel=$channel, temp=$temp, humidity=$hum, bat=$bat, rain=$rain";
  112. Log3 $iohash, 5, "deviceCode: $deviceCode";
  113. my $def = $modules{Hideki}{defptr}{$iohash->{NAME} . "." . $deviceCode};
  114. $def = $modules{Hideki}{defptr}{$deviceCode} if(!$def);
  115. if(!$def) {
  116. Log3 $iohash, 1, "Hideki: UNDEFINED sensor $sensorTyp detected, code $deviceCode";
  117. return "UNDEFINED $deviceCode Hideki $deviceCode";
  118. }
  119. my $hash = $def;
  120. $name = $hash->{NAME};
  121. return "" if(IsIgnored($name));
  122. #Log3 $name, 4, "Hideki: $name ($msg)";
  123. if (!defined(AttrVal($hash->{NAME},"event-min-interval",undef)))
  124. {
  125. my $minsecs = AttrVal($iohash->{NAME},'minsecs',0);
  126. if($hash->{lastReceive} && (time() - $hash->{lastReceive} < $minsecs)) {
  127. Log3 $iohash, 4, "$deviceCode Dropped ($decodedString) due to short time. minsecs=$minsecs";
  128. return "";
  129. }
  130. }
  131. $hash->{lastReceive} = time();
  132. $def->{lastMSG} = $decodedString;
  133. #Log3 $name, 4, "Hideki update $name:". $name;
  134. readingsBeginUpdate($hash);
  135. readingsBulkUpdate($hash, "state", $val);
  136. readingsBulkUpdate($hash, "battery", $bat) if ($bat ne "");
  137. readingsBulkUpdate($hash, "channel", $channel) if ($channel ne "");
  138. if ($sensorTyp==0x1E){
  139. readingsBulkUpdate($hash, "humidity", $hum) if ($hum ne "");
  140. readingsBulkUpdate($hash, "temperature", $temp) if ($temp ne "");
  141. }
  142. elsif($sensorTyp==0x0E){
  143. readingsBulkUpdate($hash, "rain", $rain) if ($rain ne "");
  144. }
  145. readingsEndUpdate($hash, 1); # Notify is done by Dispatch
  146. return $name;
  147. }
  148. # check crc for incoming message
  149. # in: hex string with encrypted, raw data, starting with 75
  150. # out: 1 for OK, 0 for failed
  151. # sample "75BDBA4AC2BEC855AC0A00"
  152. sub Hideki_crc{
  153. #my $Hidekihex=shift;
  154. #my @Hidekibytes=shift;
  155. my @Hidekibytes = @{$_[0]};
  156. #push @Hidekibytes,0x75; #first byte always 75 and will not be included in decrypt/encrypt!
  157. #convert to array except for first hex
  158. #for (my $i=1; $i<(length($Hidekihex))/2; $i++){
  159. # my $hex=Hideki_decryptByte(hex(substr($Hidekihex, $i*2, 2)));
  160. # push (@Hidekibytes, $hex);
  161. #}
  162. my $cs1=0; #will be zero for xor over all (bytes>>1)&0x1F except first byte (always 0x75)
  163. #my $rawData=shift;
  164. #todo add the crc check here
  165. my $count=($Hidekibytes[2]>>1) & 0x1f;
  166. my $b;
  167. #iterate over data only, first byte is 0x75 always
  168. for (my $i=1; $i<$count+2 && $i<scalar @Hidekibytes; $i++) {
  169. $b = $Hidekibytes[$i];
  170. $cs1 = $cs1 ^ $b; # calc first chksum
  171. }
  172. if($cs1==0){
  173. return 1;
  174. }
  175. else{
  176. return 0;
  177. }
  178. }
  179. # return decoded sensor type
  180. # in: one byte
  181. # out: one byte
  182. # Der Typ eines Sensors steckt in Byte 3:
  183. # Byte3 & 0x1F Device
  184. # 0x0C Anemometer
  185. # 0x0D UV sensor
  186. # 0x0E Rain level meter
  187. # 0x1E Thermo/hygro-sensor
  188. sub getSensorType{
  189. return $_[0] & 0x1F;
  190. }
  191. # decrypt bytes of hex string
  192. # in: hex string
  193. # out: decrypted hex string
  194. sub decryptBytes{
  195. my $Hidekihex=shift;
  196. #create array of hex string
  197. my @Hidekibytes = map { Hideki_decryptByte(hex($_)) } ($Hidekihex =~ /(..)/g);
  198. my $result="75"; # Byte 0 is not encrypted
  199. for (my $i=1; $i<scalar (@Hidekibytes); $i++){
  200. $result.=sprintf("%02x",$Hidekibytes[$i]);
  201. }
  202. return $result;
  203. }
  204. sub Hideki_decryptByte{
  205. my $byte = shift;
  206. #printf("\ndecryptByte 0x%02x >>",$byte);
  207. my $ret2 = ($byte ^ ($byte << 1) & 0xFF); #gives possible overflow to left so c3->145 instead of 45
  208. #printf(" %02x\n",$ret2);
  209. return $ret2;
  210. }
  211. # decode byte array and return channel, temperature and hunidity
  212. # input: decrypted byte array starting with 0x75, passed by reference as in mysub(\@array);
  213. # output <return code>, <channel>, <temperature>, <humidity>
  214. # was unable to get this working with an array ref as input, so switched to hex string input
  215. sub decodeThermoHygro {
  216. my @Hidekibytes = @{$_[0]};
  217. #my $Hidekihex = shift;
  218. #my @Hidekibytes=();
  219. #for (my $i=0; $i<(length($Hidekihex))/2; $i++){
  220. # my $hex=hex(substr($Hidekihex, $i*2, 2)); ## Mit split und map geht es auch ... $str =~ /(..?)/g;
  221. # push (@Hidekibytes, $hex);
  222. #}
  223. my $channel=0;
  224. my $temp=0;
  225. my $humi=0;
  226. $channel = $Hidekibytes[1] >> 5;
  227. # //Internally channel 4 is used for the other sensor types (rain, uv, anemo).
  228. # //Therefore, if channel is decoded 5 or 6, the real value set on the device itself is 4 resp 5.
  229. if ($channel >= 5) {
  230. $channel--;
  231. }
  232. my $sensorId = $Hidekibytes[1] & 0x1f; # Extract random id from sensor
  233. #my $devicetype = $Hidekibytes[3]&0x1f;
  234. $temp = 100 * ($Hidekibytes[5] & 0x0f) + 10 * ($Hidekibytes[4] >> 4) + ($Hidekibytes[4] & 0x0f);
  235. ## // temp is negative?
  236. if (!($Hidekibytes[5] & 0x80)) {
  237. $temp = -$temp;
  238. }
  239. $humi = 10 * ($Hidekibytes[6] >> 4) + ($Hidekibytes[6] & 0x0f);
  240. $temp = $temp / 10;
  241. return ($channel, $temp, $humi);
  242. }
  243. # decode byte array and return channel and total rain in mm
  244. # input: decrypted byte array starting with 0x75, passed by reference as in mysub(\@array);
  245. # output <return code>, <channel>, <totalrain>
  246. # was unable to get this working with an array ref as input, so switched to hex string input
  247. sub decodeRain {
  248. my @Hidekibytes = @{$_[0]};
  249. #my $Hidekihex = shift;
  250. #my @Hidekibytes=();
  251. #for (my $i=0; $i<(length($Hidekihex))/2; $i++){
  252. # my $hex=hex(substr($Hidekihex, $i*2, 2)); ## Mit split und map geht es auch ... $str =~ /(..?)/g;
  253. # push (@Hidekibytes, $hex);
  254. #}
  255. my $channel=0;
  256. my $rain=0;
  257. my $tests=0;
  258. #additional checks?
  259. if($Hidekibytes[2]==0xCC){
  260. $tests+=1;
  261. }
  262. if($Hidekibytes[6]==0x66){
  263. $tests+=1;
  264. }
  265. # possibly test if $tests==2 for sanity check
  266. #printf("SANITY CHECK tests=%i\n", $tests);
  267. $channel = $Hidekibytes[1] >> 5;
  268. # //Internally channel 4 is used for the other sensor types (rain, uv, anemo).
  269. # //Therefore, if channel is decoded 5 or 6, the real value set on the device itself is 4 resp 5.
  270. if ($channel >= 5) {
  271. $channel--;
  272. }
  273. my $sensorId = $Hidekibytes[1] & 0x1f; # Extract random id from sensor
  274. $rain = ($Hidekibytes[4] + $Hidekibytes[5]*0xff)*0.7;
  275. return ($channel, $rain);
  276. }
  277. sub
  278. Hideki_Attr(@)
  279. {
  280. my @a = @_;
  281. # Make possible to use the same code for different logical devices when they
  282. # are received through different physical devices.
  283. return if($a[0] ne "set" || $a[2] ne "IODev");
  284. my $hash = $defs{$a[1]};
  285. my $iohash = $defs{$a[3]};
  286. my $cde = $hash->{CODE};
  287. delete($modules{Hideki}{defptr}{$cde});
  288. $modules{Hideki}{defptr}{$iohash->{NAME} . "." . $cde} = $hash;
  289. return undef;
  290. }
  291. 1;
  292. =pod
  293. =item summary Supports various rf sensors with hideki protocol
  294. =item summary_DE Unterst&uumltzt verschiedenen Funksensoren mit hideki Protokol
  295. =begin html
  296. <a name="Hideki"></a>
  297. <h3>Hideki</h3>
  298. <ul>
  299. The Hideki module is a module for decoding weather sensors, which use the hideki protocol. Known brands are Bresser, Cresta, TFA and Hama.
  300. <br><br>
  301. <a name="Hideki_define"></a>
  302. <b>Supported Brands</b>
  303. <ul>
  304. <li>Hama</li>
  305. <li>Bresser</li>
  306. <li>TFA Dostman</li>
  307. <li>Arduinos with remote Sensor lib from Randy Simons</li>
  308. <li>Cresta</li>
  309. <li>Hideki</li>
  310. <li>all other devices, which use the Hideki protocol</li>
  311. </ul>
  312. Please note, currently temp/hum devices are implemented. Please report data for other sensortypes.
  313. <a name="Hideki_define"></a>
  314. <b>Define</b>
  315. <ul>
  316. <code>define &lt;name&gt; Hideki &lt;code&gt; </code> <br>
  317. <br>
  318. &lt;code&gt; is the address of the sensor device and
  319. is build by the sensor type and the channelnumber (1 to 5) or if the attribute longid is specfied an autogenerated address build when inserting
  320. the battery (this adress will change every time changing the battery).<br>
  321. If autocreate is enabled, the device will be defined via autocreate. This is also the preferred mode of defining such a device.
  322. </ul>
  323. <a name="Hideki_readings"></a>
  324. <b>Generated readings</b>
  325. <ul>
  326. <li>state (T:x H:y B:z)</li>
  327. <li>temperature (&deg;C)</li>
  328. <li>humidity (0-100)</li>
  329. <li>battery (ok or low)</li>
  330. <li>channel (The Channelnumber (number if)</li>
  331. </ul>
  332. <a name="Hideki_unset"></a>
  333. <b>Set</b> <ul>N/A</ul><br>
  334. <a name="Hideki_unget"></a>
  335. <b>Get</b> <ul>N/A</ul><br>
  336. <a name="Hideki_attr"></a>
  337. <b>Attributes</b>
  338. <ul>
  339. <li><a href="#do_not_notify">do_not_notify</a></li>
  340. <li><a href="#eventMap">eventMap</a></li>
  341. <li><a href="#ignore">ignore</a></li>
  342. <li><a href="#showtime">showtime</a></li>
  343. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  344. </ul>
  345. <br>
  346. </ul>
  347. =end html
  348. =begin html_DE
  349. <a name="Hideki"></a>
  350. <h3>Hideki</h3>
  351. <ul>
  352. Das Hideki module dekodiert empfangene Nachrichten von Wettersensoren, welche das Hideki Protokoll verwenden.
  353. <br><br>
  354. <a name="Hideki_define"></a>
  355. <b>Unterstuetzte Hersteller</b>
  356. <ul>
  357. <li>Hama</li>
  358. <li>Bresser</li>
  359. <li>TFA Dostman</li>
  360. <li>Arduinos with remote Sensor lib from Randy Simons</li>
  361. <li>Cresta</li>
  362. <li>Hideki</li>
  363. <li>Alle anderen, welche das Hideki Protokoll verwenden</li>
  364. </ul>
  365. Hinweis, Aktuell sind nur temp/feuchte Sensoren implementiert. Bitte sendet uns Daten zu anderen Sensoren.
  366. <a name="Hideki_define"></a>
  367. <b>Define</b>
  368. <ul>
  369. <li><code>define &lt;name&gt; Hideki &lt;code&gt; </code></li>
  370. <li>
  371. <br>
  372. &lt;code&gt; besteht aus dem Sensortyp und der Kanalnummer (1..5) oder wenn das Attribut longid im IO Device gesetzt ist aus einer Zufallsadresse, die durch den Sensor beim einlegen der
  373. Batterie generiert wird (Die Adresse aendert sich bei jedem Batteriewechsel).<br>
  374. </li>
  375. <li>Wenn autocreate aktiv ist, dann wird der Sensor automatisch in FHEM angelegt. Das ist der empfohlene Weg, neue Sensoren hinzuzuf&uumlgen.</li>
  376. </ul>
  377. <br>
  378. <a name="Hideki_readings"></a>
  379. <b>Erzeugte Readings</b>
  380. <ul>
  381. <li>state (T:x H:y B:z)</li>
  382. <li>temperature (&deg;C)</li>
  383. <li>humidity (0-100)</li>
  384. <li>battery (ok or low)</li>
  385. <li>channel (Der Sensor Kanal)</li>
  386. </ul>
  387. <a name="Hideki_unset"></a>
  388. <b>Set</b> <ul>N/A</ul><br>
  389. <a name="Hideki_unget"></a>
  390. <b>Get</b> <ul>N/A</ul><br>
  391. <a name="Hideki_attr"></a>
  392. <b>Attribute</b>
  393. <ul>
  394. <li><a href="#do_not_notify">do_not_notify</a></li>
  395. <li><a href="#eventMap">eventMap</a></li>
  396. <li><a href="#ignore">ignore</a></li>
  397. <li><a href="#showtime">showtime</a></li>
  398. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  399. </ul>
  400. <br>
  401. </ul>
  402. =end html_DE
  403. =cut