-
Notifications
You must be signed in to change notification settings - Fork 0
/
elster.cpp
272 lines (217 loc) · 5.52 KB
/
elster.cpp
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
/*
Arduino library to read data from an Elster A100C electricity meter.
Copyright (C) 2012 Dave Berkeley projects@rotwang.co.uk
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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include <pins_arduino.h>
#endif
#include <elster.h>
#define BIT_PERIOD 416 // us
typedef uint32_t stamp;
#define BITS(t) (((t) + (BIT_PERIOD/2)) / BIT_PERIOD)
unsigned long bcdtol(const unsigned char* data, int bytes)
{
unsigned long result = 0;
for (int i = 0; i < bytes; i++)
{
const unsigned char digit = *data++;
result *= 100;
result += 10 * (digit >> 4);
result += digit & 0xF;
}
return result;
}
/*
* Called on every InfraRed pulse, ie. every '1' bit.
*/
static stamp last_us;
void ElsterA100C::on_change(void)
{
// push the number of bit periods since the last interrupt to a buffer.
const stamp us = micros();
const int diff = us - last_us;
last_us = us;
const int bit_periods = BITS(diff);
buff_add(& bits, bit_periods);
}
static ElsterA100C* meters[2];
void on_change_0(void)
{
if (meters[0]) meters[0]->on_change();
}
void on_change_1(void)
{
if (meters[1]) meters[1]->on_change();
}
void ElsterA100C::init(int irq)
{
buff_init(& bits);
buff_init(& bytes);
meters[irq] = this;
switch (irq) {
case 0 : attachInterrupt(0, on_change_0, RISING); break;
case 1 : attachInterrupt(1, on_change_1, RISING); break;
}
}
/*
* Decode bit stream
*/
int ElsterA100C::add_bit(int state)
{
bit_data >>= 1;
if (state)
bit_data += 0x200;
if (bit_data)
bit_index += 1;
if (bit_index < 10)
return 0;
if (bit_data & 1) // start bit
{
if (!state) // stop bit
{
const int d = (bit_data >> 1) & 0xFF;
// high represents '0', so invert the bits
buff_add(& bytes, (d ^ 0xFF));
bit_data = bit_index = 0;
return 1;
}
}
// bad frame
bit_data = bit_index = 0;
return 0;
}
void ElsterA100C::on_timeout()
{
// too long since last bits count
if (bit_index)
while (bit_data) // flush with trailing '0' bits
add_bit(0);
bit_data = bit_index = 0;
}
void ElsterA100C::on_bits(int bits)
{
// called on each bits count.
// the bit periods represent a '1' followed by N '0's.
if (bits < 1)
{
on_timeout();
return;
}
// the elapsed bit periods since last interrupt
// represents a '1' followed by 0 or more '0's.
add_bit(1);
for (; bits > 1; bits--)
add_bit(0);
}
/*
*
*/
int ElsterA100C::good_cs(unsigned char cs, unsigned char check)
{
// note : should be 'cs == check', but I'm seeing
// systematic 1-bit errors which I can't track down.
/*
Serial.print("bcc=");
Serial.print(check, HEX);
Serial.print(" cs=");
Serial.print(cs, HEX);
Serial.print(" xor=");
Serial.print(cs ^ check, HEX);
Serial.print("\r\n");
*/
int bits = 0;
int delta = cs ^ check;
for (; delta; delta >>= 1)
if (delta & 0x01)
bits += 1;
return bits < 2;
}
void ElsterA100C::good_packet()
{
struct info* info = (struct info*) data;
handler(bcdtol(info->rate_1_import_kWh, 5));
}
unsigned char ElsterA100C::bcc(unsigned char cs, const unsigned char* data, int count)
{
for (int i = 0; i < count; ++i)
cs += *data++;
return cs;
}
void ElsterA100C::on_data(unsigned char c)
{
// match the packet header
const unsigned char match[] = { 0x01, 0x00, sizeof(info), 0x02 };
if (!reading)
{
// keep a log of the last 4 chars
last_4[0] = last_4[1];
last_4[1] = last_4[2];
last_4[2] = last_4[3];
last_4[3] = c;
if (memcmp(match, last_4, sizeof(match)) == 0)
{
idx = 0;
reading = 1;
return;
}
return;
}
if (idx < sizeof(info))
{
data[idx++] = c;
return;
}
const unsigned char etx = 0x03;
if (idx == sizeof(info))
{
if (c != etx)
{
printf("etx=%02X\n", c);
reading = idx = 0;
return;
}
idx++;
return;
}
unsigned char cs = 0x00;
cs = bcc(cs, match, sizeof(match));
cs = bcc(cs, data, sizeof(data));
cs = bcc(cs, & etx, 1);
if (good_cs(cs, c))
good_packet();
reading = 0;
}
int ElsterA100C::decode_bit_stream(void)
{
// look for a pause in the bit stream
const stamp us = micros();
const int diff = us - last_us;
if (BITS(diff) > 20)
on_timeout();
// read the bit stream
int bit_count;
if (buff_get(& bits, & bit_count) < 0)
return -1;
on_bits(bit_count);
// read the byte stream decoded above
int byte_data;
if (buff_get(& bytes, & byte_data) < 0)
return -1;
return byte_data;
}
// FIN