gt_wt_02.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "gt_wt_02.h"
  2. #define TAG "WSProtocolGT_WT02"
  3. /*
  4. * Help
  5. * https://github.com/merbanan/rtl_433/blob/master/src/devices/gt_wt_02.c
  6. *
  7. * GT-WT-02 sensor on 433.92MHz.
  8. * Example and frame description provided by https://github.com/ludwich66
  9. * [01] {37} 34 00 ed 47 60 : 00110100 00000000 11101101 01000111 01100000
  10. * code, BatOK,not-man-send, Channel1, +23,7°C, 35%
  11. * [01] {37} 34 8f 87 15 90 : 00110100 10001111 10000111 00010101 10010000
  12. * code, BatOK,not-man-send, Channel1,-12,1°C, 10%
  13. * Humidity:
  14. * - the working range is 20-90 %
  15. * - if "LL" in display view it sends 10 %
  16. * - if "HH" in display view it sends 110%
  17. * SENSOR: GT-WT-02 (ALDI Globaltronics..)
  18. * TYP IIIIIIII BMCCTTTT TTTTTTTT HHHHHHHX XXXXX
  19. * TYPE Description:
  20. * - I = Random Device Code, changes with battery reset
  21. * - B = Battery 0=OK 1=LOW
  22. * - M = Manual Send Button Pressed 0=not pressed 1=pressed
  23. * - C = Channel 00=CH1, 01=CH2, 10=CH3
  24. * - T = Temperature, 12 Bit 2's complement, scaled by 10
  25. * - H = Humidity = 7 Bit bin2dez 00-99, Display LL=10%, Display HH=110% (Range 20-90%)
  26. * - X = Checksum, sum modulo 64
  27. * A Lidl AURIO (from 12/2018) with PCB marking YJ-T12 V02 has two extra bits in front.
  28. *
  29. */
  30. static const SubGhzBlockConst ws_protocol_gt_wt_02_const = {
  31. .te_short = 500,
  32. .te_long = 2000,
  33. .te_delta = 150,
  34. .min_count_bit_for_found = 37,
  35. };
  36. struct WSProtocolDecoderGT_WT02 {
  37. SubGhzProtocolDecoderBase base;
  38. SubGhzBlockDecoder decoder;
  39. WSBlockGeneric generic;
  40. };
  41. struct WSProtocolEncoderGT_WT02 {
  42. SubGhzProtocolEncoderBase base;
  43. SubGhzProtocolBlockEncoder encoder;
  44. WSBlockGeneric generic;
  45. };
  46. typedef enum {
  47. GT_WT02DecoderStepReset = 0,
  48. GT_WT02DecoderStepSaveDuration,
  49. GT_WT02DecoderStepCheckDuration,
  50. } GT_WT02DecoderStep;
  51. const SubGhzProtocolDecoder ws_protocol_gt_wt_02_decoder = {
  52. .alloc = ws_protocol_decoder_gt_wt_02_alloc,
  53. .free = ws_protocol_decoder_gt_wt_02_free,
  54. .feed = ws_protocol_decoder_gt_wt_02_feed,
  55. .reset = ws_protocol_decoder_gt_wt_02_reset,
  56. .get_hash_data = ws_protocol_decoder_gt_wt_02_get_hash_data,
  57. .serialize = ws_protocol_decoder_gt_wt_02_serialize,
  58. .deserialize = ws_protocol_decoder_gt_wt_02_deserialize,
  59. .get_string = ws_protocol_decoder_gt_wt_02_get_string,
  60. };
  61. const SubGhzProtocolEncoder ws_protocol_gt_wt_02_encoder = {
  62. .alloc = NULL,
  63. .free = NULL,
  64. .deserialize = NULL,
  65. .stop = NULL,
  66. .yield = NULL,
  67. };
  68. const SubGhzProtocol ws_protocol_gt_wt_02 = {
  69. .name = WS_PROTOCOL_GT_WT_02_NAME,
  70. .type = SubGhzProtocolWeatherStation,
  71. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  72. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  73. .decoder = &ws_protocol_gt_wt_02_decoder,
  74. .encoder = &ws_protocol_gt_wt_02_encoder,
  75. };
  76. void* ws_protocol_decoder_gt_wt_02_alloc(SubGhzEnvironment* environment) {
  77. UNUSED(environment);
  78. WSProtocolDecoderGT_WT02* instance = malloc(sizeof(WSProtocolDecoderGT_WT02));
  79. instance->base.protocol = &ws_protocol_gt_wt_02;
  80. instance->generic.protocol_name = instance->base.protocol->name;
  81. return instance;
  82. }
  83. void ws_protocol_decoder_gt_wt_02_free(void* context) {
  84. furi_assert(context);
  85. WSProtocolDecoderGT_WT02* instance = context;
  86. free(instance);
  87. }
  88. void ws_protocol_decoder_gt_wt_02_reset(void* context) {
  89. furi_assert(context);
  90. WSProtocolDecoderGT_WT02* instance = context;
  91. instance->decoder.parser_step = GT_WT02DecoderStepReset;
  92. }
  93. static bool ws_protocol_gt_wt_02_check(WSProtocolDecoderGT_WT02* instance) {
  94. if(!instance->decoder.decode_data) return false;
  95. uint8_t sum = (instance->decoder.decode_data >> 5) & 0xe;
  96. uint64_t temp_data = instance->decoder.decode_data >> 9;
  97. for(uint8_t i = 0; i < 7; i++) {
  98. sum += (temp_data >> (i * 4)) & 0xF;
  99. }
  100. return ((uint8_t)(instance->decoder.decode_data & 0x3F) == (sum & 0x3F));
  101. }
  102. /**
  103. * Analysis of received data
  104. * @param instance Pointer to a WSBlockGeneric* instance
  105. */
  106. static void ws_protocol_gt_wt_02_remote_controller(WSBlockGeneric* instance) {
  107. instance->id = (instance->data >> 29) & 0xFF;
  108. instance->battery_low = (instance->data >> 28) & 1;
  109. instance->btn = (instance->data >> 27) & 1;
  110. instance->channel = ((instance->data >> 25) & 0x3) + 1;
  111. if(!((instance->data >> 24) & 1)) {
  112. instance->temp = (float)((instance->data >> 13) & 0x07FF) / 10.0f;
  113. } else {
  114. instance->temp = (float)((~(instance->data >> 13) & 0x07FF) + 1) / -10.0f;
  115. }
  116. instance->humidity = (instance->data >> 6) & 0x7F;
  117. if(instance->humidity <= 10) // actually the sensors sends 10 below working range of 20%
  118. instance->humidity = 0;
  119. else if(instance->humidity > 90) // actually the sensors sends 110 above working range of 90%
  120. instance->humidity = 100;
  121. }
  122. void ws_protocol_decoder_gt_wt_02_feed(void* context, bool level, uint32_t duration) {
  123. furi_assert(context);
  124. WSProtocolDecoderGT_WT02* instance = context;
  125. switch(instance->decoder.parser_step) {
  126. case GT_WT02DecoderStepReset:
  127. if((!level) && (DURATION_DIFF(duration, ws_protocol_gt_wt_02_const.te_short * 18) <
  128. ws_protocol_gt_wt_02_const.te_delta * 8)) {
  129. //Found syncPrefix
  130. instance->decoder.parser_step = GT_WT02DecoderStepSaveDuration;
  131. instance->decoder.decode_data = 0;
  132. instance->decoder.decode_count_bit = 0;
  133. }
  134. break;
  135. case GT_WT02DecoderStepSaveDuration:
  136. if(level) {
  137. instance->decoder.te_last = duration;
  138. instance->decoder.parser_step = GT_WT02DecoderStepCheckDuration;
  139. } else {
  140. instance->decoder.parser_step = GT_WT02DecoderStepReset;
  141. }
  142. break;
  143. case GT_WT02DecoderStepCheckDuration:
  144. if(!level) {
  145. if(DURATION_DIFF(instance->decoder.te_last, ws_protocol_gt_wt_02_const.te_short) <
  146. ws_protocol_gt_wt_02_const.te_delta) {
  147. if(DURATION_DIFF(duration, ws_protocol_gt_wt_02_const.te_short * 18) <
  148. ws_protocol_gt_wt_02_const.te_delta * 8) {
  149. //Found syncPostfix
  150. instance->decoder.parser_step = GT_WT02DecoderStepReset;
  151. if((instance->decoder.decode_count_bit ==
  152. ws_protocol_gt_wt_02_const.min_count_bit_for_found) &&
  153. ws_protocol_gt_wt_02_check(instance)) {
  154. instance->generic.data = instance->decoder.decode_data;
  155. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  156. ws_protocol_gt_wt_02_remote_controller(&instance->generic);
  157. if(instance->base.callback)
  158. instance->base.callback(&instance->base, instance->base.context);
  159. } else if(instance->decoder.decode_count_bit == 1) {
  160. instance->decoder.parser_step = GT_WT02DecoderStepSaveDuration;
  161. }
  162. instance->decoder.decode_data = 0;
  163. instance->decoder.decode_count_bit = 0;
  164. } else if(
  165. DURATION_DIFF(duration, ws_protocol_gt_wt_02_const.te_long) <
  166. ws_protocol_gt_wt_02_const.te_delta * 2) {
  167. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  168. instance->decoder.parser_step = GT_WT02DecoderStepSaveDuration;
  169. } else if(
  170. DURATION_DIFF(duration, ws_protocol_gt_wt_02_const.te_long * 2) <
  171. ws_protocol_gt_wt_02_const.te_delta * 4) {
  172. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  173. instance->decoder.parser_step = GT_WT02DecoderStepSaveDuration;
  174. } else {
  175. instance->decoder.parser_step = GT_WT02DecoderStepReset;
  176. }
  177. } else {
  178. instance->decoder.parser_step = GT_WT02DecoderStepReset;
  179. }
  180. } else {
  181. instance->decoder.parser_step = GT_WT02DecoderStepReset;
  182. }
  183. break;
  184. }
  185. }
  186. uint8_t ws_protocol_decoder_gt_wt_02_get_hash_data(void* context) {
  187. furi_assert(context);
  188. WSProtocolDecoderGT_WT02* instance = context;
  189. return subghz_protocol_blocks_get_hash_data(
  190. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  191. }
  192. SubGhzProtocolStatus ws_protocol_decoder_gt_wt_02_serialize(
  193. void* context,
  194. FlipperFormat* flipper_format,
  195. SubGhzRadioPreset* preset) {
  196. furi_assert(context);
  197. WSProtocolDecoderGT_WT02* instance = context;
  198. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  199. }
  200. SubGhzProtocolStatus
  201. ws_protocol_decoder_gt_wt_02_deserialize(void* context, FlipperFormat* flipper_format) {
  202. furi_assert(context);
  203. WSProtocolDecoderGT_WT02* instance = context;
  204. return ws_block_generic_deserialize_check_count_bit(
  205. &instance->generic, flipper_format, ws_protocol_gt_wt_02_const.min_count_bit_for_found);
  206. }
  207. void ws_protocol_decoder_gt_wt_02_get_string(void* context, FuriString* output) {
  208. furi_assert(context);
  209. WSProtocolDecoderGT_WT02* instance = context;
  210. furi_string_printf(
  211. output,
  212. "%s %dbit\r\n"
  213. "Key:0x%lX%08lX\r\n"
  214. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  215. "Temp:%3.1f C Hum:%d%%",
  216. instance->generic.protocol_name,
  217. instance->generic.data_count_bit,
  218. (uint32_t)(instance->generic.data >> 32),
  219. (uint32_t)(instance->generic.data),
  220. instance->generic.id,
  221. instance->generic.channel,
  222. instance->generic.battery_low,
  223. (double)instance->generic.temp,
  224. instance->generic.humidity);
  225. }