-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEMT7110.cpp
221 lines (187 loc) · 5.61 KB
/
EMT7110.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
#include "EMT7110.h"
// Data rate: 9.579 kbit/s
// Message-Format
// ---------------------------------------------------------------------------
// Byte 0 + 1 Identification normally: 25 6A during Pairing: 25 2A
// Byte 2 + 3 ID
// Byte 4 Bit 6 Device connected
// Byte 4 Bit 7 Pairing flag
// Byte 4 Bit 0..5 Power 0.5 W steps
// Byte 5 Power 0.5 W steps
// Byte 6 + 7 Current (mA),
// Byte 8 Voltage (0.5 V steps - 128 V)
// Byte 9 Bit 6 ???
// Byte 9 Bit 7 ???
// Byte 9 Bit 0..5 Accumulated Power (0.01 kWh steps)
// Byte 10 Accumulated Power (0.01 kWh steps)
// Byte 11 CRC sum on all 12 bytes == 0
void EMT7110::DecodeFrame(byte *data, struct Frame *frame) {
frame->IsValid = true;
frame->Header1 = data[0];
frame->Header2 = data[1];
frame->PairingFlag = (data[4] & 0b10000000) > 0;
frame->ID = (data[2] << 8) | data[3];
frame->Byte9_6 = (data[9] & 0b01000000) > 0;
frame->Byte9_7 = (data[9] & 0b10000000) > 0;
if (frame->PairingFlag) {
frame->Voltage = 0.0;
frame->Current = 0.0;
frame->Power = 0.0;
frame->AccumulatedPower = 0.0;
frame->ConsumersConnected = false;
frame->CRC = 0;
frame->IsValid = false;
}
else {
frame->Voltage = 128.0 + data[8] * 0.5;
frame->Current = (data[6] << 8) | data[7];
frame->Power = ((data[4] & 0x3F) << 8 | data[5]) / 2;
frame->AccumulatedPower = ((data[9] & 0x3F) << 8 | data[10]) / 100.0;
frame->ConsumersConnected = (data[4] & 0b01000000) > 0;
frame->CRC = data[11];
frame->IsValid = CrcIsValid(data);
}
}
bool EMT7110::DisplayFrame(byte *data, struct EMT7110::Frame &frame, bool fOnlyIfValid) {
if (fOnlyIfValid && !frame.IsValid) {
return frame.IsValid;
}
// MilliSeconds
static unsigned long lastMillis;
unsigned long now = millis();
char div[16];
sprintf(div, "%06d ", now - lastMillis);
lastMillis = millis();
Serial.print(div);
// Show the raw data bytes
Serial.print("EMT7110 [");
for (int i = 0; i < FRAME_LENGTH; i++) {
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print("]");
// Check CRC
if (!frame.IsValid) {
Serial.print(" CRC:WRONG");
}
else {
Serial.print(" CRC:OK");
}
// Start
Serial.print(" S:");
Serial.print(frame.Header1, HEX);
Serial.print(" ");
Serial.print(frame.Header2, HEX);
// ID
Serial.print(" ID:");
Serial.print(frame.ID, HEX);
// Voltage
Serial.print(" V:");
Serial.print(frame.Voltage);
// Current
Serial.print(" mA:");
Serial.print(frame.Current);
// Power
Serial.print(" W:");
Serial.print(frame.Power);
// AccumulatedPower
Serial.print(" kWh:");
Serial.print(frame.AccumulatedPower);
// Connected
Serial.print(" Con.:");
Serial.print(frame.ConsumersConnected);
// Pairing
Serial.print(" Pair:");
Serial.print(frame.PairingFlag);
// CRC
Serial.print(" CRC:");
Serial.print(frame.CRC);
Serial.println();
return frame.IsValid;
}
void EMT7110::AnalyzeFrame(byte *data, bool fOnlyIfValid) {
struct Frame frame;
DecodeFrame(data, &frame);
DisplayFrame(data, frame, fOnlyIfValid);
}
bool EMT7110::CrcIsValid(byte data[]) {
byte crc = 0;
for (int i=0; i < FRAME_LENGTH; i++) {
crc += data[i];
}
return crc == 0;
}
String EMT7110::GetFhemDataString(struct Frame *frame) {
// Format
//
// OK EMT7110 84 81 8 237 0 13 0 2 1 6 1 -> ID 5451 228,5V 13mA 2W 2,62kWh
// OK EMT7110 84 162 8 207 0 76 0 7 0 0 1
// OK EMT7110 ID ID VV VV AA AA WW WW KW KW Flags
// | | | | | | | | | | |
// | | | | | | | | | | `--- AccumulatedPower * 100 LSB
// | | | | | | | | | `------ AccumulatedPower * 100 MSB
// | | | | | | | | `--- Power (W) LSB
// | | | | | | | `------ Power (W) MSB
// | | | | | | `--- Current (mA) LSB
// | | | | | `------ Current (mA) MSB
// | | | | `--- Voltage (V) * 10 LSB
// | | | `----- Voltage (V) * 10 MSB
// | | `--- ID
// | `------- ID
// `--- fix "EMT7110"
// Header and ID
String result;
result += "OK EMT7110 ";
result += (byte)(frame->ID >> 8);
result += " ";
result += (byte)(frame->ID);
result += " ";
// Voltage (V * 10)
int volt = (int)(frame->Voltage * 10);
result += (byte)(volt >> 8);
result += " ";
result += (byte)(volt);
result += " ";
// Current (mA)
result += (byte)((int)frame->Current >> 8);
result += " ";
result += (byte)(frame->Current);
result += " ";
// Power (W)
result += (byte)((int)frame->Power >> 8);
result += " ";
result += (byte)(frame->Power);
result += " ";
// AccumulatedPower (kWh * 100)
int acp = (int)(frame->AccumulatedPower * 100);
result += (byte)(acp >> 8);
result += " ";
result += (byte)(acp);
result += " ";
// Flags
byte flags = 0;
flags += frame->ConsumersConnected * 1;
flags += frame->PairingFlag * 2;
result += flags;
return result;
}
bool EMT7110::TryHandleData(byte *data, bool fFhemDisplay) {
if (data[0] == 0x25 && (data[1] == 0x6A || data[1] == 0x2A || data[1] == 0x40)) {
struct Frame frame;
DecodeFrame(data, &frame);
if (frame.IsValid) {
if (fFhemDisplay) {
String fhemString = "";
fhemString = GetFhemDataString(&frame);
if (fhemString.length() > 0) {
Serial.println(fhemString);
}
return fhemString.length() > 0;
}
else {
return DisplayFrame(data, frame);
}
}
}
return false;
}