-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
rtl_433.c
executable file
·1736 lines (1511 loc) · 56.5 KB
/
rtl_433.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* rtl_433, turns your Realtek RTL2832 based DVB dongle into a 433.92MHz generic data receiver
* Copyright (C) 2012 by Benjamin Larsson <benjamin@southpole.se>
*
* Based on rtl_sdr
*
* Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* Currently this can decode the temperature and id from Rubicson sensors
*
* the sensor sends 36 bits 12 times pwm modulated
* the data is grouped into 9 nibles
* [id0] [id1], [unk0] [temp0], [temp1] [temp2], [unk1] [unk2], [unk3]
*
* The id changes when the battery is changed in the sensor.
* unk0 is always 1 0 0 0, most likely 2 channel bits as the sensor can recevice 3 channels
* unk1-3 changes and the meaning is unknown
* temp is 12 bit signed scaled by 10
*
* The sensor can be bought at Kjell&Co
*/
/* Prologue sensor protocol
*
* the sensor sends 36 bits 7 times, before the first packet there is a pulse sent
* the packets are pwm modulated
*
* the data is grouped in 9 nibles
* [id0] [rid0] [rid1] [data0] [temp0] [temp1] [temp2] [humi0] [humi1]
*
* id0 is always 1001,9
* rid is a random id that is generated when the sensor starts, could include battery status
* the same batteries often generate the same id
* data(3) is 0 the battery status, 1 ok, 0 low, first reading always say low
* data(2) is 1 when the sensor sends a reading when pressing the button on the sensor
* data(1,0)+1 forms the channel number that can be set by the sensor (1-3)
* temp is 12 bit signed scaled by 10
* humi0 is always 1100,c if no humidity sensor is available
* humi1 is always 1100,c if no humidity sensor is available
*
* The sensor can be bought at Clas Ohlson
*/
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifndef _WIN32
#include <unistd.h>
#else
#include <Windows.h>
#include <io.h>
#include <fcntl.h>
#include "getopt/getopt.h"
#endif
#include "rtl-sdr.h"
#define DEFAULT_SAMPLE_RATE 250000
#define DEFAULT_FREQUENCY 433920000
#define DEFAULT_HOP_TIME (60*10)
#define DEFAULT_HOP_EVENTS 2
#define DEFAULT_ASYNC_BUF_NUMBER 32
#define DEFAULT_BUF_LENGTH (16 * 16384)
#define DEFAULT_LEVEL_LIMIT 10000
#define DEFAULT_DECIMATION_LEVEL 0
#define MINIMAL_BUF_LENGTH 512
#define MAXIMAL_BUF_LENGTH (256 * 16384)
#define FILTER_ORDER 1
#define MAX_PROTOCOLS 10
#define SIGNAL_GRABBER_BUFFER (12 * DEFAULT_BUF_LENGTH)
#define BITBUF_COLS 34
#define BITBUF_ROWS 50
static int do_exit = 0;
static int do_exit_async=0, frequencies=0, events=0;
uint32_t frequency[MAX_PROTOCOLS];
time_t rawtime_old;
int flag;
uint32_t samp_rate=DEFAULT_SAMPLE_RATE;
static uint32_t bytes_to_read = 0;
static rtlsdr_dev_t *dev = NULL;
static uint16_t scaled_squares[256];
static int debug_output = 0;
static int override_short = 0;
static int override_long = 0;
/* Supported modulation types */
#define OOK_PWM_D 1 /* Pulses are of the same length, the distance varies */
#define OOK_PWM_P 2 /* The length of the pulses varies */
typedef struct {
unsigned int id;
char name[256];
unsigned int modulation;
unsigned int short_limit;
unsigned int long_limit;
unsigned int reset_limit;
int (*json_callback)(uint8_t bits_buffer[BITBUF_ROWS][BITBUF_COLS]) ;
} r_device;
static int debug_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
int i,j,k;
fprintf(stderr, "\n");
for (i=0 ; i<BITBUF_ROWS ; i++) {
fprintf(stderr, "[%02d] ",i);
for (j=0 ; j<BITBUF_COLS ; j++) {
fprintf(stderr, "%02x ", bb[i][j]);
}
fprintf(stderr, ": ");
for (j=0 ; j<BITBUF_COLS ; j++) {
for (k=7 ; k>=0 ; k--) {
if (bb[i][j] & 1<<k)
fprintf(stderr, "1");
else
fprintf(stderr, "0");
}
fprintf(stderr, " ");
}
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
return 0;
}
static int silvercrest_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
/* FIXME validate the received message better */
if (bb[1][0] == 0xF8 &&
bb[2][0] == 0xF8 &&
bb[3][0] == 0xF8 &&
bb[4][0] == 0xF8 &&
bb[1][1] == 0x4d &&
bb[2][1] == 0x4d &&
bb[3][1] == 0x4d &&
bb[4][1] == 0x4d) {
/* Pretty sure this is a Silvercrest remote */
fprintf(stderr, "Remote button event:\n");
fprintf(stderr, "model = Silvercrest\n");
fprintf(stderr, "%02x %02x %02x %02x %02x\n",bb[1][0],bb[0][1],bb[0][2],bb[0][3],bb[0][4]);
if (debug_output)
debug_callback(bb);
return 1;
}
return 0;
}
static int rubicson_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
int temperature_before_dec;
int temperature_after_dec;
int16_t temp;
/* FIXME validate the received message better, figure out crc */
if (bb[1][0] == bb[2][0] && bb[2][0] == bb[3][0] && bb[3][0] == bb[4][0] &&
bb[4][0] == bb[5][0] && bb[5][0] == bb[6][0] && bb[6][0] == bb[7][0] && bb[7][0] == bb[8][0] &&
bb[8][0] == bb[9][0] && (bb[5][0] != 0 && bb[5][1] != 0 && bb[5][2] != 0)) {
/* Nible 3,4,5 contains 12 bits of temperature
* The temerature is signed and scaled by 10 */
temp = (int16_t)((uint16_t)(bb[0][1] << 12) | (bb[0][2] << 4));
temp = temp >> 4;
temperature_before_dec = abs(temp / 10);
temperature_after_dec = abs(temp % 10);
fprintf(stderr, "Sensor temperature event:\n");
fprintf(stderr, "protocol = Rubicson/Auriol\n");
fprintf(stderr, "rid = %x\n",bb[0][0]);
fprintf(stderr, "temp = %s%d.%d\n",temp<0?"-":"",temperature_before_dec, temperature_after_dec);
fprintf(stderr, "%02x %02x %02x %02x %02x\n",bb[1][0],bb[0][1],bb[0][2],bb[0][3],bb[0][4]);
if (debug_output)
debug_callback(bb);
return 1;
}
return 0;
}
static int prologue_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
int rid;
int16_t temp2;
/* FIXME validate the received message better */
if (((bb[1][0]&0xF0) == 0x90 && (bb[2][0]&0xF0) == 0x90 && (bb[3][0]&0xF0) == 0x90 && (bb[4][0]&0xF0) == 0x90 &&
(bb[5][0]&0xF0) == 0x90 && (bb[6][0]&0xF0) == 0x90) ||
((bb[1][0]&0xF0) == 0x50 && (bb[2][0]&0xF0) == 0x50 && (bb[3][0]&0xF0) == 0x50 && (bb[4][0]&0xF0) == 0x50)) {
/* Prologue sensor */
temp2 = (int16_t)((uint16_t)(bb[1][2] << 8) | (bb[1][3]&0xF0));
temp2 = temp2 >> 4;
fprintf(stderr, "Sensor temperature event:\n");
fprintf(stderr, "protocol = Prologue\n");
fprintf(stderr, "button = %d\n",bb[1][1]&0x04?1:0);
fprintf(stderr, "battery = %s\n",bb[1][1]&0x08?"Ok":"Low");
fprintf(stderr, "temp = %s%d.%d\n",temp2<0?"-":"",abs((int16_t)temp2/10),abs((int16_t)temp2%10));
fprintf(stderr, "humidity = %d\n", ((bb[1][3]&0x0F)<<4)|(bb[1][4]>>4));
fprintf(stderr, "channel = %d\n",(bb[1][1]&0x03)+1);
fprintf(stderr, "id = %d\n",(bb[1][0]&0xF0)>>4);
rid = ((bb[1][0]&0x0F)<<4)|(bb[1][1]&0xF0)>>4;
fprintf(stderr, "rid = %d\n", rid);
fprintf(stderr, "hrid = %02x\n", rid);
fprintf(stderr, "%02x %02x %02x %02x %02x\n",bb[1][0],bb[1][1],bb[1][2],bb[1][3],bb[1][4]);
if (debug_output)
debug_callback(bb);
return 1;
}
return 0;
}
static int waveman_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
/* Two bits map to 2 states, 0 1 -> 0 and 1 1 -> 1 */
int i;
uint8_t nb[3] = {0};
if (((bb[0][0]&0x55)==0x55) && ((bb[0][1]&0x55)==0x55) && ((bb[0][2]&0x55)==0x55) && ((bb[0][3]&0x55)==0x00)) {
for (i=0 ; i<3 ; i++) {
nb[i] |= ((bb[0][i]&0xC0)==0xC0) ? 0x00 : 0x01;
nb[i] |= ((bb[0][i]&0x30)==0x30) ? 0x00 : 0x02;
nb[i] |= ((bb[0][i]&0x0C)==0x0C) ? 0x00 : 0x04;
nb[i] |= ((bb[0][i]&0x03)==0x03) ? 0x00 : 0x08;
}
fprintf(stderr, "Remote button event:\n");
fprintf(stderr, "model = Waveman Switch Transmitter\n");
fprintf(stderr, "id = %c\n", 'A'+nb[0]);
fprintf(stderr, "channel = %d\n", (nb[1]>>2)+1);
fprintf(stderr, "button = %d\n", (nb[1]&3)+1);
fprintf(stderr, "state = %s\n", (nb[2]==0xe) ? "on" : "off");
fprintf(stderr, "%02x %02x %02x\n",nb[0],nb[1],nb[2]);
if (debug_output)
debug_callback(bb);
return 1;
}
return 0;
}
static int steffen_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
if (bb[0][0]==0x00 && ((bb[1][0]&0x07)==0x07) && bb[1][0]==bb[2][0] && bb[2][0]==bb[3][0]) {
fprintf(stderr, "Remote button event:\n");
fprintf(stderr, "model = Steffan Switch Transmitter\n");
fprintf(stderr, "code = %d%d%d%d%d\n", (bb[1][0]&0x80)>>7, (bb[1][0]&0x40)>>6, (bb[1][0]&0x20)>>5, (bb[1][0]&0x10)>>4, (bb[1][0]&0x08)>>3);
if ((bb[1][2]&0x0f)==0x0e)
fprintf(stderr, "button = A\n");
else if ((bb[1][2]&0x0f)==0x0d)
fprintf(stderr, "button = B\n");
else if ((bb[1][2]&0x0f)==0x0b)
fprintf(stderr, "button = C\n");
else if ((bb[1][2]&0x0f)==0x07)
fprintf(stderr, "button = D\n");
else if ((bb[1][2]&0x0f)==0x0f)
fprintf(stderr, "button = ALL\n");
else
fprintf(stderr, "button = unknown\n");
if ((bb[1][2]&0xf0)==0xf0) {
fprintf(stderr, "state = OFF\n");
} else {
fprintf(stderr, "state = ON\n");
}
if (debug_output)
debug_callback(bb);
return 1;
}
return 0;
}
uint16_t AD_POP(uint8_t bb[BITBUF_COLS], uint8_t bits, uint8_t bit) {
uint16_t val = 0;
uint8_t i, byte_no, bit_no;
for (i=0;i<bits;i++) {
byte_no= (bit+i)/8 ;
bit_no =7-((bit+i)%8);
if (bb[byte_no]&(1<<bit_no)) val = val | (1<<i);
}
return val;
}
static int em1000_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
// based on fs20.c
uint8_t dec[10];
uint8_t bytes=0;
uint8_t bit=18; // preamble
uint8_t bb_p[14];
char* types[] = {"S", "?", "GZ"};
uint8_t checksum_calculated = 0;
uint8_t i;
uint8_t stopbit;
uint8_t checksum_received;
// check and combine the 3 repetitions
for (i = 0; i < 14; i++) {
if(bb[0][i]==bb[1][i] || bb[0][i]==bb[2][i]) bb_p[i]=bb[0][i];
else if(bb[1][i]==bb[2][i]) bb_p[i]=bb[1][i];
else return 0;
}
// read 9 bytes with stopbit ...
for (i = 0; i < 9; i++) {
dec[i] = AD_POP (bb_p, 8, bit); bit+=8;
stopbit=AD_POP (bb_p, 1, bit); bit+=1;
if (!stopbit) {
// fprintf(stderr, "!stopbit: %i\n", i);
return 0;
}
checksum_calculated ^= dec[i];
bytes++;
}
// Read checksum
checksum_received = AD_POP (bb_p, 8, bit); bit+=8;
if (checksum_received != checksum_calculated) {
// fprintf(stderr, "checksum_received != checksum_calculated: %d %d\n", checksum_received, checksum_calculated);
return 0;
}
//for (i = 0; i < bytes; i++) fprintf(stderr, "%02X ", dec[i]); fprintf(stderr, "\n");
// based on 15_CUL_EM.pm
fprintf(stderr, "Energy sensor event:\n");
fprintf(stderr, "protocol = ELV EM 1000\n");
fprintf(stderr, "type = EM 1000-%s\n",dec[0]>=1&&dec[0]<=3?types[dec[0]-1]:"?");
fprintf(stderr, "code = %d\n",dec[1]);
fprintf(stderr, "seqno = %d\n",dec[2]);
fprintf(stderr, "total cnt = %d\n",dec[3]|dec[4]<<8);
fprintf(stderr, "current cnt = %d\n",dec[5]|dec[6]<<8);
fprintf(stderr, "peak cnt = %d\n",dec[7]|dec[8]<<8);
return 1;
}
static int ws2000_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
// based on http://www.dc3yc.privat.t-online.de/protocol.htm
uint8_t dec[13];
uint8_t nibbles=0;
uint8_t bit=11; // preamble
char* types[]={"!AS3", "AS2000/ASH2000/S2000/S2001A/S2001IA/ASH2200/S300IA", "!S2000R", "!S2000W", "S2001I/S2001ID", "!S2500H", "!Pyrano", "!KS200/KS300"};
uint8_t check_calculated=0, sum_calculated=0;
uint8_t i;
uint8_t stopbit;
uint8_t sum_received;
dec[0] = AD_POP (bb[0], 4, bit); bit+=4;
stopbit= AD_POP (bb[0], 1, bit); bit+=1;
if (!stopbit) {
//fprintf(stderr, "!stopbit\n");
return 0;
}
check_calculated ^= dec[0];
sum_calculated += dec[0];
// read nibbles with stopbit ...
for (i = 1; i <= (dec[0]==4?12:8); i++) {
dec[i] = AD_POP (bb[0], 4, bit); bit+=4;
stopbit= AD_POP (bb[0], 1, bit); bit+=1;
if (!stopbit) {
//fprintf(stderr, "!stopbit %i\n", i);
return 0;
}
check_calculated ^= dec[i];
sum_calculated += dec[i];
nibbles++;
}
if (check_calculated) {
//fprintf(stderr, "check_calculated (%d) != 0\n", check_calculated);
return 0;
}
// Read sum
sum_received = AD_POP (bb[0], 4, bit); bit+=4;
sum_calculated+=5;
sum_calculated&=0xF;
if (sum_received != sum_calculated) {
//fprintf(stderr, "sum_received (%d) != sum_calculated (%d) ", sum_received, sum_calculated);
return 0;
}
//for (i = 0; i < nibbles; i++) fprintf(stderr, "%02X ", dec[i]); fprintf(stderr, "\n");
fprintf(stderr, "Weather station sensor event:\n");
fprintf(stderr, "protocol = ELV WS 2000\n");
fprintf(stderr, "type (!=ToDo) = %s\n", dec[0]<=7?types[dec[0]]:"?");
fprintf(stderr, "code = %d\n", dec[1]&7);
fprintf(stderr, "temp = %s%d.%d\n", dec[1]&8?"-":"", dec[4]*10+dec[3], dec[2]);
fprintf(stderr, "humidity = %d.%d\n", dec[7]*10+dec[6], dec[5]);
if(dec[0]==4) {
fprintf(stderr, "pressure = %d\n", 200+dec[10]*100+dec[9]*10+dec[8]);
}
return 1;
}
// ** Acurite 5n1 functions **
const float acurite_winddirections[] =
{ 337.5, 315.0, 292.5, 270.0, 247.5, 225.0, 202.5, 180,
157.5, 135.0, 112.5, 90.0, 67.5, 45.0, 22.5, 0.0 };
static int acurite_raincounter = 0;
static int acurite_crc(uint8_t row[BITBUF_COLS], int cols) {
// sum of first n-1 bytes modulo 256 should equal nth byte
int i;
int sum = 0;
for ( i=0; i < cols; i++)
sum += row[i];
if ( sum % 256 == row[cols] )
return 1;
else
return 0;
}
static int acurite_detect(uint8_t *pRow) {
int i;
if ( pRow[0] != 0x00 ) {
// invert bits due to wierd issue
for (i = 0; i < 8; i++)
pRow[i] = ~pRow[i] & 0xFF;
pRow[0] |= pRow[8]; // fix first byte that has mashed leading bit
if (acurite_crc(pRow, 7))
return 1; // passes crc check
}
return 0;
}
static float acurite_getTemp (uint8_t highbyte, uint8_t lowbyte) {
// range -40 to 158 F
int highbits = (highbyte & 0x0F) << 7 ;
int lowbits = lowbyte & 0x7F;
int rawtemp = highbits | lowbits;
float temp = (rawtemp - 400) / 10.0;
return temp;
}
static int acurite_getWindSpeed (uint8_t highbyte, uint8_t lowbyte) {
// range: 0 to 159 kph
int highbits = ( highbyte & 0x1F) << 3;
int lowbits = ( lowbyte & 0x70 ) >> 4;
int speed = highbits | lowbits;
return speed;
}
static float acurite_getWindDirection (uint8_t byte) {
// 16 compass points, ccw from (NNW) to 15 (N)
int direction = byte & 0x0F;
return acurite_winddirections[direction];
}
static int acurite_getHumidity (uint8_t byte) {
// range: 1 to 99 %RH
int humidity = byte & 0x7F;
return humidity;
}
static int acurite_getRainfallCounter (uint8_t highbyte, uint8_t lowbyte) {
// range: 0 to 99.99 in, 0.01 in incr., rolling counter?
int highbits = (highbyte & 0x3F) << 7 ;
int lowbits = lowbyte & 0x7F;
int raincounter = highbits | lowbits;
return raincounter;
}
static int acurite5n1_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS]) {
// acurite 5n1 weather sensor decoding for rtl_433
// Jens Jensen 2014
int i;
uint8_t *buf = NULL;
// run through rows til we find one with good crc (brute force)
for (i=0; i < BITBUF_ROWS; i++) {
if (acurite_detect(bb[i])) {
buf = bb[i];
break; // done
}
}
if (buf) {
// decode packet here
fprintf(stderr, "Detected Acurite 5n1 sensor\n");
if (debug_output) {
for (i=0; i < 8; i++)
fprintf(stderr, "%02X ", buf[i]);
fprintf(stderr, "CRC OK\n");
}
if ((buf[2] & 0x0F) == 1) {
// wind speed, wind direction, rainfall
float rainfall = 0.00;
int raincounter = 0;
if (acurite_raincounter > 0) {
// track rainfall difference after first run
raincounter = acurite_getRainfallCounter(buf[5], buf[6]);
rainfall = ( raincounter - acurite_raincounter ) * 0.01;
} else {
// capture starting counter
acurite_raincounter = raincounter;
}
fprintf(stderr, "wind speed: %d kph, ",
acurite_getWindSpeed(buf[3], buf[4]));
fprintf(stderr, "wind direction: %0.1f°, ",
acurite_getWindDirection(buf[4]));
fprintf(stderr, "rain gauge: %0.2f in.\n", rainfall);
} else if ((buf[2] & 0x0F) == 8) {
// wind speed, temp, RH
fprintf(stderr, "wind speed: %d kph, ",
acurite_getWindSpeed(buf[3], buf[4]));
fprintf(stderr, "temp: %2.1f° F, ",
acurite_getTemp(buf[4], buf[5]));
fprintf(stderr, "humidity: %d% RH\n",
acurite_getHumidity(buf[6]));
}
}
//if (debug_output)
// debug_callback(bb);
return 1;
}
// timings based on samp_rate=1024000
r_device rubicson = {
/* .id = */ 1,
/* .name = */ "Rubicson Temperature Sensor",
/* .modulation = */ OOK_PWM_D,
/* .short_limit = */ 1744/4,
/* .long_limit = */ 3500/4,
/* .reset_limit = */ 5000/4,
/* .json_callback = */ &rubicson_callback,
};
r_device prologue = {
/* .id = */ 2,
/* .name = */ "Prologue Temperature Sensor",
/* .modulation = */ OOK_PWM_D,
/* .short_limit = */ 3500/4,
/* .long_limit = */ 7000/4,
/* .reset_limit = */ 15000/4,
/* .json_callback = */ &prologue_callback,
};
r_device silvercrest = {
/* .id = */ 3,
/* .name = */ "Silvercrest Remote Control",
/* .modulation = */ OOK_PWM_P,
/* .short_limit = */ 600/4,
/* .long_limit = */ 5000/4,
/* .reset_limit = */ 15000/4,
/* .json_callback = */ &silvercrest_callback,
};
r_device tech_line_fws_500 = {
/* .id = */ 4,
/* .name = */ "Tech Line FWS-500 Sensor",
/* .modulation = */ OOK_PWM_D,
/* .short_limit = */ 3500/4,
/* .long_limit = */ 7000/4,
/* .reset_limit = */ 15000/4,
// /* .json_callback = */ &rubicson_callback,
};
r_device generic_hx2262 = {
/* .id = */ 5,
/* .name = */ "Window/Door sensor",
/* .modulation = */ OOK_PWM_P,
/* .short_limit = */ 1300/4,
/* .long_limit = */ 10000/4,
/* .reset_limit = */ 40000/4,
// /* .json_callback = */ &silvercrest_callback,
};
r_device technoline_ws9118 = {
/* .id = */ 6,
/* .name = */ "Technoline WS9118",
/* .modulation = */ OOK_PWM_D,
/* .short_limit = */ 1800/4,
/* .long_limit = */ 3500/4,
/* .reset_limit = */ 15000/4,
/* .json_callback = */ &debug_callback,
};
r_device elv_em1000 = {
/* .id = */ 7,
/* .name = */ "ELV EM 1000",
/* .modulation = */ OOK_PWM_D,
/* .short_limit = */ 750/4,
/* .long_limit = */ 7250/4,
/* .reset_limit = */ 30000/4,
/* .json_callback = */ &em1000_callback,
};
r_device elv_ws2000 = {
/* .id = */ 8,
/* .name = */ "ELV WS 2000",
/* .modulation = */ OOK_PWM_D,
/* .short_limit = */ (602+(1155-602)/2)/4,
/* .long_limit = */ ((1755635-1655517)/2)/4, // no repetitions
/* .reset_limit = */ ((1755635-1655517)*2)/4,
/* .json_callback = */ &ws2000_callback,
};
r_device waveman = {
/* .id = */ 6,
/* .name = */ "Waveman Switch Transmitter",
/* .modulation = */ OOK_PWM_P,
/* .short_limit = */ 1000/4,
/* .long_limit = */ 8000/4,
/* .reset_limit = */ 30000/4,
/* .json_callback = */ &waveman_callback,
};
r_device steffen = {
/* .id = */ 9,
/* .name = */ "Steffen Switch Transmitter",
/* .modulation = */ OOK_PWM_D,
/* .short_limit = */ 140,
/* .long_limit = */ 270,
/* .reset_limit = */ 1500,
/* .json_callback = */ &steffen_callback,
};
r_device acurite5n1 = {
/* .id = */ 10,
/* .name = */ "Acurite 5n1 Weather Station",
/* .modulation = */ OOK_PWM_P,
/* .short_limit = */ 75,
/* .long_limit = */ 220,
/* .reset_limit = */ 20000,
/* .json_callback = */ &acurite5n1_callback,
};
struct protocol_state {
int (*callback)(uint8_t bits_buffer[BITBUF_ROWS][BITBUF_COLS]);
/* bits state */
int bits_col_idx;
int bits_row_idx;
int bits_bit_col_idx;
uint8_t bits_buffer[BITBUF_ROWS][BITBUF_COLS];
int16_t bits_per_row[BITBUF_ROWS];
int bit_rows;
unsigned int modulation;
/* demod state */
int pulse_length;
int pulse_count;
int pulse_distance;
int sample_counter;
int start_c;
int packet_present;
int pulse_start;
int real_bits;
int start_bit;
/* pwm limits */
int short_limit;
int long_limit;
int reset_limit;
};
struct dm_state {
FILE *file;
int save_data;
int32_t level_limit;
int32_t decimation_level;
int16_t filter_buffer[MAXIMAL_BUF_LENGTH+FILTER_ORDER];
int16_t* f_buf;
int analyze;
int debug_mode;
/* Signal grabber variables */
int signal_grabber;
int8_t* sg_buf;
int sg_index;
int sg_len;
/* Protocol states */
int r_dev_num;
struct protocol_state *r_devs[MAX_PROTOCOLS];
};
void usage(void)
{
fprintf(stderr,
"rtl_433, an ISM band generic data receiver for RTL2832 based DVB-T receivers\n\n"
"Usage:\t[-d device_index (default: 0)]\n"
"\t[-g gain (default: 0 for auto)]\n"
"\t[-a analyze mode, print a textual description of the signal]\n"
"\t[-t signal auto save, use it together with analyze mode (-a -t)\n"
"\t[-l change the detection level used to determine pulses (0-3200) default: %i]\n"
"\t[-f [-f...] receive frequency[s], default: %i Hz]\n"
"\t[-s samplerate (default: %i Hz)]\n"
"\t[-S force sync output (default: async)]\n"
"\t[-r read data from file instead of from a receiver]\n"
"\t[-p ppm_error (default: 0)]\n"
"\t[-r test file name (indata)]\n"
"\t[-m test file mode (0 rtl_sdr data, 1 rtl_433 data)]\n"
"\t[-D print debug info on event\n"
"\t[-z override short value\n"
"\t[-x override long value\n"
"\tfilename (a '-' dumps samples to stdout)\n\n", DEFAULT_LEVEL_LIMIT, DEFAULT_FREQUENCY, DEFAULT_SAMPLE_RATE);
exit(1);
}
#ifdef _WIN32
BOOL WINAPI
sighandler(int signum)
{
if (CTRL_C_EVENT == signum) {
fprintf(stderr, "Signal caught, exiting!\n");
do_exit = 1;
rtlsdr_cancel_async(dev);
return TRUE;
}
return FALSE;
}
#else
static void sighandler(int signum)
{
fprintf(stderr, "Signal caught, exiting!\n");
do_exit = 1;
rtlsdr_cancel_async(dev);
}
#endif
/* precalculate lookup table for envelope detection */
static void calc_squares() {
int i;
for (i=0 ; i<256 ; i++)
scaled_squares[i] = (128-i) * (128-i);
}
/** This will give a noisy envelope of OOK/ASK signals
* Subtract the bias (-128) and get an envelope estimation
* The output will be written in the input buffer
* @returns pointer to the input buffer
*/
static void envelope_detect(unsigned char *buf, uint32_t len, int decimate)
{
uint16_t* sample_buffer = (uint16_t*) buf;
unsigned int i;
unsigned op = 0;
unsigned int stride = 1<<decimate;
for (i=0 ; i<len/2 ; i+=stride) {
sample_buffer[op++] = scaled_squares[buf[2*i ]]+scaled_squares[buf[2*i+1]];
}
}
static void demod_reset_bits_packet(struct protocol_state* p) {
memset(p->bits_buffer, 0 ,BITBUF_ROWS*BITBUF_COLS);
memset(p->bits_per_row, 0 ,BITBUF_ROWS);
p->bits_col_idx = 0;
p->bits_bit_col_idx = 7;
p->bits_row_idx = 0;
p->bit_rows = 0;
}
static void demod_add_bit(struct protocol_state* p, int bit) {
p->bits_buffer[p->bits_row_idx][p->bits_col_idx] |= bit<<p->bits_bit_col_idx;
p->bits_bit_col_idx--;
if (p->bits_bit_col_idx<0) {
p->bits_bit_col_idx = 7;
p->bits_col_idx++;
if (p->bits_col_idx>BITBUF_COLS-1) {
p->bits_col_idx = BITBUF_COLS-1;
// fprintf(stderr, "p->bits_col_idx>%i!\n", BITBUF_COLS-1);
}
}
p->bits_per_row[p->bit_rows]++;
}
static void demod_next_bits_packet(struct protocol_state* p) {
p->bits_col_idx = 0;
p->bits_row_idx++;
p->bits_bit_col_idx = 7;
if (p->bits_row_idx>BITBUF_ROWS-1) {
p->bits_row_idx = BITBUF_ROWS-1;
//fprintf(stderr, "p->bits_row_idx>%i!\n", BITBUF_ROWS-1);
}
p->bit_rows++;
if (p->bit_rows > BITBUF_ROWS-1)
p->bit_rows -=1;
}
static void demod_print_bits_packet(struct protocol_state* p) {
int i,j,k;
fprintf(stderr, "\n");
for (i=0 ; i<p->bit_rows+1 ; i++) {
fprintf(stderr, "[%02d] {%d} ",i, p->bits_per_row[i]);
for (j=0 ; j<((p->bits_per_row[i]+8)/8) ; j++) {
fprintf(stderr, "%02x ", p->bits_buffer[i][j]);
}
fprintf(stderr, ": ");
for (j=0 ; j<((p->bits_per_row[i]+8)/8) ; j++) {
for (k=7 ; k>=0 ; k--) {
if (p->bits_buffer[i][j] & 1<<k)
fprintf(stderr, "1");
else
fprintf(stderr, "0");
}
// fprintf(stderr, "=0x%x ",demod->bits_buffer[i][j]);
fprintf(stderr, " ");
}
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
return;
}
static void register_protocol(struct dm_state *demod, r_device *t_dev) {
struct protocol_state *p = calloc(1,sizeof(struct protocol_state));
p->short_limit = (float)t_dev->short_limit/((float)DEFAULT_SAMPLE_RATE/(float)samp_rate);
p->long_limit = (float)t_dev->long_limit /((float)DEFAULT_SAMPLE_RATE/(float)samp_rate);
p->reset_limit = (float)t_dev->reset_limit/((float)DEFAULT_SAMPLE_RATE/(float)samp_rate);
p->modulation = t_dev->modulation;
p->callback = t_dev->json_callback;
demod_reset_bits_packet(p);
demod->r_devs[demod->r_dev_num] = p;
demod->r_dev_num++;
fprintf(stderr, "Registering protocol[%02d] %s\n",demod->r_dev_num, t_dev->name);
if (demod->r_dev_num > MAX_PROTOCOLS)
fprintf(stderr, "Max number of protocols reached %d\n",MAX_PROTOCOLS);
}
static unsigned int counter = 0;
static unsigned int print = 1;
static unsigned int print2 = 0;
static unsigned int pulses_found = 0;
static unsigned int prev_pulse_start = 0;
static unsigned int pulse_start = 0;
static unsigned int pulse_end = 0;
static unsigned int pulse_avg = 0;
static unsigned int signal_start = 0;
static unsigned int signal_end = 0;
static unsigned int signal_pulse_data[4000][3] = {{0}};
static unsigned int signal_pulse_counter = 0;
static void classify_signal() {
unsigned int i,k, max=0, min=1000000, t;
unsigned int delta, count_min, count_max, min_new, max_new, p_limit;
unsigned int a[3], b[2], a_cnt[3], a_new[3], b_new[2];
unsigned int signal_distance_data[4000] = {0};
struct protocol_state p = {0};
unsigned int signal_type;
if (!signal_pulse_data[0][0])
return;
for (i=0 ; i<1000 ; i++) {
if (signal_pulse_data[i][0] > 0) {
//fprintf(stderr, "[%03d] s: %d\t e:\t %d\t l:%d\n",
//i, signal_pulse_data[i][0], signal_pulse_data[i][1],
//signal_pulse_data[i][2]);
if (signal_pulse_data[i][2] > max)
max = signal_pulse_data[i][2];
if (signal_pulse_data[i][2] <= min)
min = signal_pulse_data[i][2];
}
}
t=(max+min)/2;
//fprintf(stderr, "\n\nMax: %d, Min: %d t:%d\n", max, min, t);
delta = (max - min)*(max-min);
//TODO use Lloyd-Max quantizer instead
k=1;
while((k < 10) && (delta > 0)) {
min_new = 0; count_min = 0;
max_new = 0; count_max = 0;
for (i=0 ; i < 1000 ; i++) {
if (signal_pulse_data[i][0] > 0) {
if (signal_pulse_data[i][2] < t) {
min_new = min_new + signal_pulse_data[i][2];
count_min++;
}
else {
max_new = max_new + signal_pulse_data[i][2];
count_max++;
}
}
}
min_new = min_new / count_min;
max_new = max_new / count_max;
delta = (min - min_new)*(min - min_new) + (max - max_new)*(max - max_new);
min = min_new;
max = max_new;
t = (min + max)/2;
fprintf(stderr, "Iteration %d. t: %d min: %d (%d) max: %d (%d) delta %d\n", k,t, min, count_min, max, count_max, delta);
k++;
}
for (i=0 ; i<1000 ; i++) {
if (signal_pulse_data[i][0] > 0) {
//fprintf(stderr, "%d\n", signal_pulse_data[i][1]);
}
}
/* 50% decision limit */
if (max/min > 1) {
fprintf(stderr, "Pulse coding: Short pulse length %d - Long pulse length %d\n", min, max);
signal_type = 2;
} else {
fprintf(stderr, "Distance coding: Pulse length %d\n", (min+max)/2);
signal_type = 1;
}
p_limit = (max+min)/2;
/* Initial guesses */
a[0] = 1000000;
a[2] = 0;
for (i=1 ; i<1000 ; i++) {
if (signal_pulse_data[i][0] > 0) {
// fprintf(stderr, "[%03d] s: %d\t e:\t %d\t l:%d\t d:%d\n",
// i, signal_pulse_data[i][0], signal_pulse_data[i][1],
// signal_pulse_data[i][2], signal_pulse_data[i][0]-signal_pulse_data[i-1][1]);
signal_distance_data[i-1] = signal_pulse_data[i][0]-signal_pulse_data[i-1][1];
if (signal_distance_data[i-1] > a[2])
a[2] = signal_distance_data[i-1];
if (signal_distance_data[i-1] <= a[0])
a[0] = signal_distance_data[i-1];
}
}
min = a[0];
max = a[2];
a[1] = (a[0]+a[2])/2;
// for (i=0 ; i<1 ; i++) {
// b[i] = (a[i]+a[i+1])/2;
// }
b[0] = (a[0]+a[1])/2;
b[1] = (a[1]+a[2])/2;
// fprintf(stderr, "a[0]: %d\t a[1]: %d\t a[2]: %d\t\n",a[0],a[1],a[2]);
// fprintf(stderr, "b[0]: %d\t b[1]: %d\n",b[0],b[1]);
k=1;
delta = 10000000;
while((k < 10) && (delta > 0)) {
for (i=0 ; i<3 ; i++) {
a_new[i] = 0;
a_cnt[i] = 0;
}
for (i=0 ; i < 1000 ; i++) {
if (signal_distance_data[i] > 0) {
if (signal_distance_data[i] < b[0]) {
a_new[0] += signal_distance_data[i];
a_cnt[0]++;
} else if (signal_distance_data[i] < b[1] && signal_distance_data[i] >= b[0]){
a_new[1] += signal_distance_data[i];
a_cnt[1]++;