/
switchdoclabs_FT020T.c
269 lines (235 loc) · 9.67 KB
/
switchdoclabs_FT020T.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* SwitchDoc Labs WeatherSense FT020T All In One Weather Sensor Pack
*
*/
#include "decoder.h"
// three repeats without gap
// full preamble is 0x00145 (the last bits might not be fixed, e.g. 0x00146)
// and on decoding also 0xffd45
static const uint8_t preamble_pattern[2] = {0x01, 0x45}; // 12 bits
static const uint8_t preamble_inverted[2] = {0xfd, 0x45}; // 12 bits
//============================================================================================
// CRC table
//============================================================================================
const unsigned char crc_table[256] = {
0x00, 0x31, 0x62, 0x53, 0xc4, 0xf5, 0xa6, 0x97, 0xb9, 0x88, 0xdb, 0xea, 0x7d, 0x4c, 0x1f, 0x2e,
0x43, 0x72, 0x21, 0x10, 0x87, 0xb6, 0xe5, 0xd4, 0xfa, 0xcb, 0x98, 0xa9, 0x3e, 0x0f, 0x5c, 0x6d,
0x86, 0xb7, 0xe4, 0xd5, 0x42, 0x73,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 0x1b, 0x2a,
0xc1, 0xf0, 0xa3, 0x92, 0x05, 0x34, 0x67, 0x56, 0x78, 0x49, 0x1a, 0x2b, 0xbc, 0x8d, 0xde, 0xef,
0x82, 0xb3, 0xe0, 0xd1, 0x46, 0x77, 0x24, 0x15, 0x3b, 0x0a, 0x59, 0x68, 0xff, 0xce, 0x9d, 0xac
};
//============================================================================================
// Function: Calculate the CRC value
// Input: crc: CRC initial value. lpBuff: string. ucLen: string length
// Return: CRC value
//============================================================================================
uint8_t GetCRC(uint8_t crc, uint8_t * lpBuff,uint8_t ucLen)
{
while (ucLen)
{
ucLen--;
crc = crc_table[*lpBuff ^ crc];
lpBuff++;
}
return crc;
}
static int
switchdoclabs_weather_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos)
{
uint8_t b[16];
data_t *data;
int i;
for (i=0; i< 16; i++)
b[i] = 0;
bitbuffer_extract_bytes(bitbuffer, row, bitpos, b, 16*8);
/*
for (i=0; i< 16; i++)
{
fprintf(stderr,"%02x ", b[i]);
}
fprintf(stderr,"\n");
*/
uint8_t myDevice;
uint8_t mySerial;
uint8_t myFlags;
uint8_t myBatteryLow;
uint16_t myAveWindSpeed;
uint16_t myGust;
uint16_t myWindDirection;
uint32_t myCumulativeRain;
uint8_t mySecondFlags;
uint16_t myTemperature;
uint8_t myHumidity;
uint32_t myLight;
uint16_t myUV;
uint8_t myCRC;
uint8_t b2[16];
uint8_t myCalculated;
b2[0] = 0xd4; // Shift all 4 bits fix b2[0];
for (i = 0; i < 15; i++)
{
b2[i+1] = ((b[i] &0x0f)<<4) + ((b[i+1] & 0xf0)>>4);
b2[i] = b2[i+1]; // shift 8
}
/*
for (i=0; i< 16; i++)
{
fprintf(stderr,"%02x ", b2[i]);
}
fprintf(stderr,"\n");
*/
uint8_t expected = b2[13];
myCalculated = GetCRC(0xc0, b2, 13);
uint8_t calculated = myCalculated;
if (expected != calculated) {
if (decoder->verbose) {
fprintf(stderr, "Checksum error in SwitchDoc Labs Weather message. Expected: %02x Calculated: %02x\n", expected, calculated);
fprintf(stderr, "Message: ");
bitrow_print(b, 48);
}
return 0;
}
myDevice = (b2[0] & 0xf0)>>4;
if (myDevice != 0x0c)
{
return 0; // not my device
}
mySerial = (b2[0]&0x0f)<<4 & (b2[1] % 0xf0)>>4;
myFlags = b2[1] & 0x0f;
myBatteryLow = (myFlags & 0x08) >> 3;
myAveWindSpeed = b2[2] | ((myFlags & 0x01)<<8);
myGust = b2[3] | ((myFlags & 0x02)<<7);
myWindDirection= b2[4] | ((myFlags & 0x04)<<6);
myCumulativeRain=(b2[5]<<8) + b2[6];
mySecondFlags = (b2[7] & 0xf0)>>4;
myTemperature = ((b2[7] & 0x0f)<<8) + b2[8];
myHumidity = b2[9];
myLight = (b2[10]<<8) + b2[11] + ((mySecondFlags & 0x08)<<9);
myUV = b2[12];
//myUV = myUV + 10;
myCRC = b2[13];
if (myTemperature == 0xFF)
{
return 0; // Bad Data
}
if (myAveWindSpeed == 0xFF)
{
return 0; // Bad Data
}
/*
myDevice = (b[0]) & 0x0f;
mySerial = b[1];
myFlags = (b[2]>>4) & 0x0f;
myBatteryLow = (myFlags & 0x08) >> 3;
myAveWindSpeed = ((b[2] & 0x0f)<<4 ) + (b[3]>>4)+ ((myFlags & 0x01)<<8);
myGust = ((b[3] & 0x0f)<<4 ) + (b[4]>>4)+ ((myFlags & 0x02)<<7);
myWindDirection= ((b[4] & 0x0f)<<4 ) + (b[5]>>4) + ((myFlags & 0x04)<<6);
myCumulativeRain=((((b[5] & 0x0f)<<4 ) + (b[6]>>4))<<8) + (((b[6] & 0x0f)<<4 ) + (b[7]>>4)) ;
mySecondFlags = (b[7]) & 0x0f;
myTemperature = ((b[8]& 0xf0)<<4) + ((b[8] &0x0f)<<4) + ((b[9] &0xf0) >>4);
myHumidity = ((b[9] &0x0f)<<4) + (b[10]>>4);
myLight = ((b[10] & 0x0f)<<12) + ((b[11] & 0xf0)<< 4) + ((b[11] &0x0f)<< 4) + (b[12]>>4)+ ((mySecondFlags & 0x08)<<9);
myUV = ((b[12] & 0x0f) << 4) + ((b[13] & 0xf0)>>4);
//myUV = myUV + 10;
myCRC = ((b[13] & 0x0f) << 4) + ((b[14] & 0xf0)>>4);
*/
/*
fprintf(stderr,"myDevice = %02x %d\n", myDevice, myDevice );
fprintf(stderr,"mySerial = %02x %d\n", mySerial, mySerial );
fprintf(stderr,"myFlags = %01x %d\n", myFlags, myFlags );
fprintf(stderr,"myBatteryLow = %01x %d\n", myBatteryLow , myBatteryLow );
fprintf(stderr,"myAveWindSpeed = %02x %d\n", myAveWindSpeed, myAveWindSpeed );
fprintf(stderr,"myGust = %02x %d\n", myGust, myGust );
fprintf(stderr,"myWindDirection = %02x %d\n", myWindDirection, myWindDirection );
fprintf(stderr,"myCumulativeRain = %04x %d\n", myCumulativeRain, myCumulativeRain );
fprintf(stderr,"mySecondFlags = %01x %d\n", mySecondFlags, mySecondFlags );
fprintf(stderr,"myTemperature = %04x %d\n", myTemperature, myTemperature );
fprintf(stderr,"myLight = %04x %d\n", myLight, myLight );
fprintf(stderr,"myUV = %02x %d\n", myUV, myUV );
fprintf(stderr,"myCRC = %02x %d\n", myCRC, myCRC );
fprintf(stderr,"myCalculated = %02x %d\n", myCalculated, myCalculated );
*/
data = data_make(
"model", "", DATA_STRING, _X("SwitchDoc Labs-FT020T","SwitchDoc Labs FT020T AIO"),
_X("id","device"), "Device", DATA_INT, myDevice,
"id", "Serial Number", DATA_INT, mySerial,
"batterylow", "Battery Low", DATA_INT, myBatteryLow,
"avewindspeed", "Ave Wind Speed", DATA_INT, myAveWindSpeed,
"gustwindspeed", "Gust", DATA_INT, myGust,
"winddirection", "Wind Direction", DATA_INT, myWindDirection,
"cumulativerain", "Cum Rain", DATA_INT, myCumulativeRain,
"temperature", "Temperature", DATA_INT, myTemperature,
"humidity", "Humidity", DATA_INT, myHumidity,
"light", "Light", DATA_INT, myLight,
"uv", "UV Index", DATA_INT, myUV,
"mic", "Integrity", DATA_STRING, "CRC",
NULL);
decoder_output_data(decoder, data);
return 1;
}
static int
switchdoclabs_weather_callback(r_device *decoder, bitbuffer_t *bitbuffer)
{
int row;
unsigned bitpos;
int events = 0;
for (row = 0; row < bitbuffer->num_rows; ++row) {
bitpos = 0;
// Find a preamble with enough bits after it that it could be a complete packet
while ((bitpos = bitbuffer_search(bitbuffer, row, bitpos,
(const uint8_t *)&preamble_pattern, 12)) + 8+6*8 <=
bitbuffer->bits_per_row[row]) {
//fprintf(stderr,"before decode1\n");
events += switchdoclabs_weather_decode(decoder, bitbuffer, row, bitpos + 8);
if (events) return events; // for now, break after first successful message
bitpos += 16;
}
bitpos = 0;
while ((bitpos = bitbuffer_search(bitbuffer, row, bitpos,
(const uint8_t *)&preamble_inverted, 12)) + 8+6*8 <=
bitbuffer->bits_per_row[row]) {
//fprintf(stderr,"before decode2\n");
events += switchdoclabs_weather_decode(decoder, bitbuffer, row, bitpos + 8);
if (events) return events; // for now, break after first successful message
bitpos += 15;
}
}
return events;
}
static char *output_fields[] = {
"model",
"device",
"id",
"batterylow",
"avewindspeed",
"gustwindspeed",
"winddirection",
"cumulativerain",
"temperature",
"humidity",
"light",
"uv",
"mic",
NULL
};
r_device switchdoclabs_FT020T = {
.name = "SwitchDoc Labs Weather FT020T Sensors",
.modulation = OOK_PULSE_MANCHESTER_ZEROBIT,
.short_width = 488,
.long_width = 0, // not used
.reset_limit = 2400,
.decode_fn = &switchdoclabs_weather_callback,
.disabled = 0,
.fields = output_fields
};