forked from jimstudt/ook-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwh1080.c
367 lines (319 loc) · 10.4 KB
/
wh1080.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
#include <sys/stat.h>
#include "ook.h"
int verbose=0;
// Datum keeps enough information to sum them up and still compute the standard deviation
struct datum {
unsigned n;
double sum;
double sumOfSquares;
double maximum;
double minimum;
};
static time_t oldestDatum = 0;
static struct datum temperature;
static struct datum humidity;
static struct datum averageWindSpeed;
static struct datum gustWindSpeed;
static struct datum rainfall;
static struct datum batteryLow;
static struct datum windDirection;
static void resetDatum(struct datum *d)
{
memset(d,0,sizeof(*d));
}
static void addSample( struct datum *d, double v)
{
if ( oldestDatum == 0) oldestDatum = time(0);
if ( d->n==0) {
d->minimum = v;
d->maximum = v;
} else {
if ( v > d->maximum) d->maximum = v;
if ( v < d->minimum) d->minimum = v;
}
d->n++;
d->sum += v;
d->sumOfSquares += v*v;
}
static void dumpDatum( struct datum *d, const char *name, const char *units)
{
if ( d->n == 0) {
fprintf(stderr,"%s no data\n", name);
} else {
fprintf(stderr, "%s %u samples, %5.1f %s min %5.1f max %5.1f\n",
name, d->n, d->sum/d->n, units, d->minimum, d->maximum);
}
}
static void dumpWeather( void)
{
dumpDatum( &temperature, "temperature", "C");
dumpDatum( &humidity, "humidity", "%");
dumpDatum( &averageWindSpeed, "wind", "m/s");
dumpDatum( &gustWindSpeed, "gust", "m/s");
dumpDatum( &rainfall, "rain", "mm");
dumpDatum( &batteryLow, "batt", "??");
dumpDatum( &windDirection, "dir", "NEWS");
}
static void recordRecent( const char *file, double temp, double hum, double avgWind, double gustWind, double rain,
int batteryLowBits, int windDirectionBits)
{
char name[256];
strcpy(name,"/tmp/wh1080.XXXXXX");
int fd = mkstemp(name);
if ( fd < 0) {
fprintf(stderr,"Failed to create temp file for recent: %s\n", strerror(errno));
return;
}
if ( fchmod( fd, 0664) != 0) {
fprintf(stderr, "Failed to chmod temp file: %s\n", strerror(errno));
unlink(name);
close(fd);
return;
}
FILE *f = fdopen(fd,"w");
fprintf(f,"{\n");
fprintf(f,"\t\"temperature\":%.1f,\n", temp);
fprintf(f,"\t\"humidity\":%.1f,\n", hum);
fprintf(f,"\t\"avgWindSpeed\":%.1f,\n", avgWind);
fprintf(f,"\t\"gustSpeed\":%.1f,\n", gustWind);
fprintf(f,"\t\"rain\":%.1f,\n", rain);
fprintf(f,"\t\"batteryLow\":%d,\n", batteryLowBits);
fprintf(f,"\t\"windDirection\":%d\n", windDirectionBits*45);
fprintf(f,"}\n");
fclose(f);
if (rename( name, file)) {
fprintf(stderr,"Failed to rename temp file: %s\n", strerror(errno));
}
}
static void recordDatum( FILE *f, struct datum *d, const char *name, int comma)
{
fprintf(f, "\t\"%s\" : { \"n\":%d, \"sum\":%.1f, \"sum2\":%.1f, \"min\":%.1f, \"max\":%.1f }%s\n",
name, d->n, d->sum, d->sumOfSquares, d->minimum, d->maximum, (comma ? ",":""));
resetDatum( d);
}
static void recordPeriodic( const char *file)
{
char name[256];
strcpy(name,"/tmp/wh1080.XXXXXX");
int fd = mkstemp(name);
if ( fd < 0) {
fprintf(stderr,"Failed to create temp file for periodic: %s\n", strerror(errno));
return;
}
if ( fchmod( fd, 0664) != 0) {
fprintf(stderr, "Failed to chmod temp file: %s\n", strerror(errno));
unlink(name);
close(fd);
return;
}
FILE *f = fdopen(fd,"w");
fprintf(f,"{\n");
fprintf(f,"\t\"start\":%lld,\n", (long long)oldestDatum);
fprintf(f,"\t\"end\":%lld,\n", (long long)time(0));
recordDatum( f, &temperature, "temperature", 1);
recordDatum( f, &humidity, "humidity", 1);
recordDatum( f, &averageWindSpeed, "avgWindSpeed", 1);
recordDatum( f, &gustWindSpeed, "gustSpeed", 1);
recordDatum( f, &rainfall, "rainfall", 1);
recordDatum( f, &batteryLow, "batteryLow", 1);
recordDatum( f, &windDirection, "windDirection", 0);
fprintf(f,"}\n");
fclose(f);
char stamp[32]={0};
strftime( stamp, sizeof(stamp), "%Y%m%d-%H%M%S", gmtime(&oldestDatum));
char finalName[1024]="/ERROR";
snprintf(finalName, sizeof(finalName)-1, "%s-%s.json", file, stamp);
if (rename( name, finalName)) {
fprintf(stderr,"Failed to rename temp file: %s\n", strerror(errno));
}
oldestDatum = 0;
}
/*
* Function taken from Luc Small (http://lucsmall.com), itself
* derived from the OneWire Arduino library. Modifications to
* the polynomial according to Fine Offset's CRC8 calulations.
* Oddly, that may have ultimately come from me. I wrote maybe the
* original OneWire Arduino code back in the day including the CRC.
*/
static uint8_t wh1080_crc8( uint8_t *addr, uint8_t len)
{
uint8_t crc = 0;
// Indicated changes are from reference CRC-8 function in OneWire library
while (len--) {
uint8_t inbyte = *addr++;
uint8_t i;
for (i = 8; i; i--) {
uint8_t mix = (crc ^ inbyte) & 0x80; // changed from & 0x01
crc <<= 1; // changed from right shift
if (mix) crc ^= 0x31;// changed from 0x8C;
inbyte <<= 1; // changed from right shift
}
}
return crc;
}
static void showHelp( FILE *f)
{
fprintf(f,
"Usage: wh1080 [-h] [-?] [-v] [-a mcastaddr] [-p mcastport] [-i mcastinterface]\n"
" -h | -? | --help display usage and exit\n"
" -v | --verbose verbose logging\n"
" -a addr | --multicast-address addr multicast address, default 236.0.0.1\n"
" -p port | --multicast-port port multicast port, default 3636\n"
" -i addr | --multicast-interface addr address of the multicast interface, default 127.0.0.1\n"
" -r path | --recent path path to most recent data, /tmp/current-weather.json\n"
" -P path | --periodic path path to the periodic data, /tmp/weather\n"
" timestamp.json gets appended.\n"
" -m period | --minutes period number of minutes between periodic data files.\n"
);
}
int main( int argc, char **argv)
{
const char *multicastAddress = "236.0.0.1";
const char *multicastPort = "3636";
const char *multicastInterface = "127.0.0.1";
const char *recentFileName = "/tmp/current-weather.json";
const char *periodicFileName = "/tmp/weather";
int minutes = 5;
// Handle options
for(;;) {
int optionIndex = 0;
static struct option options[] = {
{ "verbose", no_argument, 0, 'v' },
{ "help", no_argument, 0, 'h' },
{ "multicast-address", required_argument, 0, 'a'},
{ "multicast-port", required_argument, 0, 'p' },
{ "multicast-interface", required_argument, 0, 'i' },
{ "recent", required_argument, 0, 'r' },
{ "periodic", required_argument, 0, 'P' },
{ "minutes", required_argument, 0, 'm' },
{ 0,0,0,0}
};
int c = getopt_long( argc, argv, "vh?f:a:p:i:m:", options, &optionIndex );
if ( c == -1) break;
switch(c) {
case 'h':
case '?':
showHelp(stdout);
return 0;
case 'v':
verbose = 1;
break;
case 'a':
multicastAddress = optarg;
break;
case 'p':
multicastPort = optarg;
break;
case 'i':
multicastInterface = optarg;
break;
case 'r':
recentFileName = optarg;
break;
case 'P':
periodicFileName = optarg;
break;
case 'm':
{
int m = atoi(optarg);
if (m<1) {
fprintf(stderr,"Illegal minutes, less than 1\n");
exit(1);
}
minutes = m;
}
break;
default:
fprintf(stderr,"Illegal option\n");
showHelp(stderr);
exit(1);
}
}
if ( verbose) fprintf(stderr,"Periodic file is %s\n", periodicFileName);
// Parse our multicast address
int sock = ook_open( multicastAddress, multicastPort, multicastInterface);
if ( sock < 0) {
fprintf(stderr,"Failed to open multicast interface\n");
exit(1);
}
for (;;) {
struct ook_burst *burst;
struct sockaddr_storage addr;
socklen_t addrLen = sizeof(addr);
int e = ook_decode_from_socket( sock, &burst, (struct sockaddr *)&addr, &addrLen, verbose);
if ( e < 0) {
fprintf(stderr,"Failed to decode from socket: %s\n", strerror(errno));
break;
}
if ( e == 0) {
fprintf(stderr,"Corrupt burst\n");
continue;
}
// Data never comes faster than 5 seconds, we are looking at the second of a pair
// of redundant transmissions
if ( oldestDatum && time(0)-oldestDatum < 5) continue;
{
unsigned char *data = 0;
size_t dataLen = 0;
int bits = ook_decode_pulse_width( burst,
1400000,1600000, 400000,600000, 900000,UINT_MAX,
&data, &dataLen,
verbose);
if ( bits == 88) {
if ( verbose) {
for (int i = 0; i < dataLen; i++) fprintf(stderr,"%02x ", data[i]);
fprintf(stderr,"\n");
}
if (data[0] != 0xff) {
if ( verbose) fprintf(stderr,"Did not begin 0xff\n");
goto NotGood;
}
if (wh1080_crc8(data+1,9) != data[10]) {
if ( verbose) fprintf(stderr,"Bad CRC\n");
goto NotGood;
}
//unsigned short deviceId = ( (data[1]<<4) | (data[2]>>4) );
unsigned short temperatureBits = (((data[2]&0xf)<<8) | data[3]);
double temp = (temperatureBits-400)/10.0; // degrees C
unsigned short humidityBits = data[4];
double hum = humidityBits; // %rh
unsigned short averageWindSpeedBits = data[5];
double avgWind = averageWindSpeedBits*0.34; // meters per second
unsigned short gustWindSpeedBits = data[6];
double gustWind = gustWindSpeedBits*0.34; // meters per second
unsigned short rainfallBits = (((data[7]&0x0f)<<8) | data[8]);
double rain = rainfallBits*0.3; // millimeters
unsigned short batteryLowBits = (data[9]>>4);
unsigned short windDirectionBits = (data[9]&0x0f);
if ( oldestDatum && time(0)-oldestDatum > minutes*60) {
recordPeriodic( periodicFileName);
}
addSample( &temperature, temp);
addSample( &humidity, hum);
addSample( &averageWindSpeed, avgWind);
addSample( &gustWindSpeed, gustWind);
addSample( &rainfall, rain);
addSample( &batteryLow, batteryLowBits);
addSample( &windDirection, windDirectionBits);
if ( verbose) dumpWeather();
recordRecent( recentFileName, temp, hum, avgWind, gustWind, rain, batteryLowBits, windDirectionBits);
} else {
if ( verbose) fprintf(stderr,"ignored %d pulse burst\n", burst->pulses);
}
NotGood:
if (data) free(data);
}
fflush(stdin);
free(burst);
}
close(sock);
return 0;
}