Редизайн на основе текущей ветки мейнстрима + новые устройства.
[rtl-433.git] / src / devices / esperanza_ews.c
1 #include <bitbuffer.h>
2 #include "util.h"
3 #include "data.h"
4 #include "rtl_433.h"
5
6
7 /*
8     Esperanza EWS-103 sensor on 433.92Mhz
9
10     Copyright (C) 2015 Alberts Saulitis
11     This program is free software; you can redistribute it and/or modify
12     it under the terms of the GNU General Public License version 3 as
13     published by the Free Software Foundation.
14 */
15
16 /*
17     No frame description was available on the internet therefore it was required
18     to reverse engineer it.
19     0        1        2        3        4        5
20     AAAABBBB ????CCTT TTTTTTTT TTHHHHHH HH?????? ??
21
22     A - Preamble
23     B - Rolling device ID
24     C - Channel (1-3)
25     T - Temperature (Little-endian)
26     H - Humidity (Little-endian)
27     ? - Unknown
28 */
29
30 /*
31    Sample Data:
32     Esperanze EWS: TemperatureF=55.5 TemperatureC=13.1 Humidity=74 Device_id=0 Channel=1
33
34     *** signal_start = 16189, signal_end = 262145
35     signal_len = 245956,  pulses = 266
36     Iteration 1. t: 142    min: 141 (37)    max: 143 (229)    delta 4
37     Iteration 2. t: 142    min: 141 (2)    max: 143 (264)    delta 0
38     Distance coding: Pulse length 142
39
40     Short distance: 487, long distance: 964, packet distance: 1920
41
42     p_limit: 142
43     bitbuffer:: Number of rows: 14
44     [00] {0} :
45     [01] {0} :
46     [02] {42} 00 53 e5 69 02 00 : 00000000 01010011 11100101 01101001 00000010 00
47     [03] {0} :
48     [04] {42} 00 53 e5 69 02 00 : 00000000 01010011 11100101 01101001 00000010 00
49     [05] {0} :
50     [06] {42} 00 53 e5 69 02 00 : 00000000 01010011 11100101 01101001 00000010 00
51     [07] {0} :
52     [08] {42} 00 53 e5 69 02 00 : 00000000 01010011 11100101 01101001 00000010 00
53     [09] {0} :
54     [10] {42} 00 53 e5 69 02 00 : 00000000 01010011 11100101 01101001 00000010 00
55     [11] {0} :
56     [12] {42} 00 53 e5 69 02 00 : 00000000 01010011 11100101 01101001 00000010 00
57     [13] {0} :
58     Test mode file issued 4 packets
59 */
60
61 static int esperanza_ews_process_row(const bitbuffer_t *bitbuffer, int row)
62 {
63     const uint8_t *b = bitbuffer->bb[row];
64     uint8_t humidity;
65     uint16_t temperature_with_offset;
66     uint8_t device_id;
67     uint8_t channel;
68     float temperature_f;
69
70     data_t *data;
71
72     char time_str[LOCAL_TIME_BUFLEN];
73
74     local_time_str(0, time_str);
75
76     humidity = (uint8_t)((b[3] << 6) | ((b[4] >> 2) & 0x0F) | ((b[3] >>2) & 0xF));
77     temperature_with_offset = (uint16_t)(((b[2] << 10) | ((b[3] << 2) & 0x300) | ((b[3] << 2) & 0xF0) | ((b[1] << 2) & 0x0C) |  b[2] >> 6) & 0x0FFF);
78     device_id = (uint8_t)(b[0] & 0x0F);
79     channel = (uint8_t)((b[1] & 0x0C)+1);
80     temperature_f = (float)((temperature_with_offset-900)/10.0);
81
82     data = data_make("time",          "",            DATA_STRING, time_str,
83                      "model",         "",            DATA_STRING, "Esperanza EWS",
84                      "id",            "",            DATA_INT, device_id,
85                      "channel",       "Channel",     DATA_INT, channel,
86                      "temperature_F", "Temperature", DATA_FORMAT, "%.02f F", DATA_DOUBLE, temperature_f,
87                      "humidity",      "Humidity",    DATA_FORMAT, "%u %%", DATA_INT, humidity,
88                       NULL);
89     data_acquired_handler(data);
90
91     return 1;
92 }
93
94 static char *output_fields[] = {
95     "time",
96     "model",
97     "id",
98     "channel",
99     "temperature_F",
100     "humidity",
101     NULL
102 };
103
104 static int esperanza_ews_callback(bitbuffer_t *bitbuffer)
105 {
106     if (bitbuffer->num_rows == 14) {
107         for (int row=2; row < bitbuffer->num_rows-3; row+=2) {
108             if (memcmp(bitbuffer->bb[row], bitbuffer->bb[row+2], sizeof(bitbuffer->bb[row])) != 0 || bitbuffer->bits_per_row[row] != 42) return 0;
109         }
110         esperanza_ews_process_row(bitbuffer, 2);
111         return 1;
112     }
113     return 0;
114 }
115
116
117 r_device esperanza_ews = {
118         .name          = "Esperanza EWS",
119         .modulation    = OOK_PULSE_PPM_RAW,
120         .short_limit   = 2800,
121         .long_limit    = 4400,
122         .reset_limit   = 8000,
123         .json_callback = &esperanza_ews_callback,
124         .disabled      = 0,
125         .demod_arg     = 0,
126 };