-
Notifications
You must be signed in to change notification settings - Fork 0
/
EfergyRPI_001.c
221 lines (168 loc) · 4.67 KB
/
EfergyRPI_001.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
/*
E2 CLASSIC RTL-SDR DECODER via rtl_fm
Modded by Andre Sollie
Copyright 2013 Nathaniel Elijah
Permission is hereby granted to use this Software for any purpose
including combining with commercial products, creating derivative
works, and redistribution of source or binary code, without
limitation or consideration. Any redistributed copies of this
Software must include the above Copyright Notice.
THIS SOFTWARE IS PROVIDED "AS IS". THE AUTHOR OF THIS CODE MAKES NO
WARRANTIES REGARDING THIS SOFTWARE, EXPRESS OR IMPLIED, AS TO ITS
SUITABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Compile:
gcc EfergyRPI_001.c -lm -o EfergyRPI_001
Execute using the following parameters:
rtl_fm -f 433550000 -s 200000 -r 96000 -g 19.7 2>/dev/null | ./EfergyRPI_001
--------------------------------------------------------------------- */
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <math.h>
#define VOLTAGE 230 /* Refernce Voltage */
#define CENTERSAMP 100 /* Number of samples needed to compute for the wave center */
#define PREAMBLE_COUNT 40 /* Number of high(1) samples for a valid preamble */
#define MINLOWBIT 3 /* Number of high(1) samples for a logic 0 */
#define MINHIGHBIT 8 /* Number of high(1) samples for a logic 1 */
#define E2BYTECOUNT 8 /* Efergy E2 Message Byte Count */
#define FRAMEBITCOUNT 64 /* Number of bits for the entire frame (not including preamble) */
int calculate_watts(char bytes[])
{
char tbyte;
double current_adc;
double result;
int i;
time_t ltime;
struct tm *curtime;
char buffer[80];
/* add all captured bytes and mask lower 8 bits */
tbyte = 0;
for(i=0;i<7;i++)
tbyte += bytes[i];
tbyte &= 0xff;
/* if checksum matches get watt data */
if (tbyte == bytes[7])
{
time( <ime );
curtime = localtime( <ime );
strftime(buffer,80,"%x,%X", curtime);
current_adc = (bytes[4] * 256) + bytes[5];
result = (VOLTAGE * current_adc) / ((double) 32768 / (double) pow(2,bytes[6]));
printf("%s,%f\n",buffer,result);
/* printf("%X %X %X %X %X %X %X %X %X %X\n",bytes[0],bytes[1],bytes[2],bytes[3],bytes[4],bytes[5],bytes[6],bytes[7],bytes[8],bytes[9]); */
fflush(stdout);
return 1;
}
//printf("Checksum Error \n");
return 0;
}
void main (int argc, char**argv)
{
char bytearray[9];
char bytedata;
int prvsamp;
int hctr;
int cursamp;
int bitpos;
int bytecount;
int i;
int preamble;
int frame;
int dcenter;
int dbit;
long center;
/* printf("Efergy E2 Classic decode \n\n"); */
/* initialize variables */
cursamp = 0;
prvsamp = 0;
bytedata = 0;
bytecount = 0;
hctr = 0;
bitpos = 0;
dbit = 0;
preamble = 0;
frame = 0;
dcenter = CENTERSAMP;
center = 0;
while( !feof(stdin) )
{
cursamp = (int16_t) (fgetc(stdin) | fgetc(stdin)<<8);
/* initially capture CENTERSAMP samples for wave center computation */
if (dcenter > 0)
{
dcenter--;
center = center + cursamp; /* Accumulate FSK wave data */
if (dcenter == 0)
{
/* compute for wave center and re-initialize frame variables */
center = (long) (center/CENTERSAMP);
hctr = 0;
bytedata = 0;
bytecount = 0;
bitpos = 0;
dbit = 0;
preamble = 0;
frame = 0;
}
}
else
{
if ((cursamp > center) && (prvsamp < center)) /* Detect for positive edge of frame data */
hctr = 0;
else
if ((cursamp > center) && (prvsamp > center)) /* count samples at high logic */
{
hctr++;
if (hctr > PREAMBLE_COUNT)
preamble = 1;
}
else
if (( cursamp < center) && (prvsamp > center))
{
/* at negative edge */
if ((hctr > MINLOWBIT) && (frame == 1))
{
dbit++;
bitpos++;
bytedata = bytedata << 1;
if (hctr > MINHIGHBIT)
bytedata = bytedata | 0x1;
if (bitpos > 7)
{
bytearray[bytecount] = bytedata;
bytedata = 0;
bitpos = 0;
bytecount++;
if (bytecount == E2BYTECOUNT)
{
/* at this point check for checksum and calculate watt data */
/* if there is a checksum mismatch compute for a new wave center */
if (calculate_watts(bytearray) == 0)
dcenter = CENTERSAMP; /* make dcenter non-zero to trigger center resampling */
}
}
if (dbit > FRAMEBITCOUNT)
{
/* reset frame variables */
bitpos = 0;
bytecount = 0;
dbit = 0;
frame = 0;
preamble = 0;
bytedata = 0;
}
}
hctr = 0;
}
else
hctr = 0;
if ((hctr == 0) && (preamble == 1))
{
/* end of preamble, start of frame data */
preamble = 0;
frame = 1;
}
} /* dcenter */
prvsamp = cursamp;
} /* while */
}