-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfoxess_t_series.h
389 lines (338 loc) · 14.3 KB
/
foxess_t_series.h
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
#include "esphome.h"
unsigned long millis_lastmessage = 0;
const long inverter_timeout = 300000;
int inverter_mode = 99; //0=Offline, 1=Online, 2=Error, 99=Waiting for response...
class foxesscomponent : public PollingComponent, public Sensor, public UARTDevice {
public:
foxesscomponent(UARTComponent *parent) : PollingComponent(600), UARTDevice(parent) {}
Sensor *frame_header = new Sensor;
Sensor *function_code = new Sensor;
Sensor *time_stamp = new Sensor;
Sensor *data_length = new Sensor;
Sensor *grid_power = new Sensor;
Sensor *generation_power = new Sensor;
Sensor *loads_power = new Sensor;
Sensor *grid_voltage_r = new Sensor;
Sensor *grid_current_r = new Sensor;
Sensor *grid_frequency_r = new Sensor;
Sensor *grid_power_r = new Sensor;
Sensor *grid_voltage_s = new Sensor;
Sensor *grid_current_s = new Sensor;
Sensor *grid_frequency_s = new Sensor;
Sensor *grid_power_s = new Sensor;
Sensor *grid_voltage_T = new Sensor;
Sensor *grid_current_T = new Sensor;
Sensor *grid_frequency_T = new Sensor;
Sensor *grid_power_T = new Sensor;
Sensor *pv1_voltage = new Sensor;
Sensor *pv1_current = new Sensor;
Sensor *pv1_power = new Sensor;
Sensor *pv2_voltage = new Sensor;
Sensor *pv2_current = new Sensor;
Sensor *pv2_power = new Sensor;
Sensor *pv3_voltage = new Sensor;
Sensor *pv3_current = new Sensor;
Sensor *pv3_power = new Sensor;
Sensor *pv4_voltage = new Sensor;
Sensor *pv4_current = new Sensor;
Sensor *pv4_power = new Sensor;
Sensor *boost_temperature = new Sensor;
Sensor *inverter_temperature = new Sensor;
Sensor *ambient_temperature = new Sensor;
Sensor *today_yield = new Sensor;
Sensor *generation_total = new Sensor;
Sensor *from_grid_yield_generation = new Sensor;
Sensor *inverter_state = new Sensor;
void setup() override {
id(inverter_state).publish_state(inverter_mode);
millis_lastmessage = millis();
}
std::vector<int> bytes;
//void loop() override {
void update() {
if(millis_lastmessage + inverter_timeout < millis()) {
if(inverter_mode != 0){
inverter_mode = 0; //offline
id(inverter_state).publish_state(inverter_mode);
id(generation_power).publish_state(0);
id(grid_current_r).publish_state(0);
id(grid_power_r).publish_state(0);
id(grid_current_T).publish_state(0);
id(grid_power_T).publish_state(0);
id(grid_current_s).publish_state(0);
id(grid_power_s).publish_state(0);
id(pv1_current).publish_state(0);
id(pv1_power).publish_state(0);
id(pv2_current).publish_state(0);
id(pv2_power).publish_state(0);
id(pv3_current).publish_state(0);
id(pv3_power).publish_state(0);
id(pv4_current).publish_state(0);
id(pv4_power).publish_state(0);
}
}
while(available() > 0) {
bytes.push_back(read());
//ESP_LOGD("custom", "reading bytes");
//make sure at least 9 header bytes are available to check
if(bytes.size() < 9){
continue;
}
if(bytes[0] != 0x7E || bytes[1] != 0x7E || bytes[2] != 0x02) {
bytes.erase(bytes.begin()); //remove first byte from buffer
//buffer will never get above 9 until the header is correct
continue;
}
else {
}
//if(bytes.size() == 9) { //>=
TwoByte message_length;
message_length.Byte[0] = bytes[8];
message_length.Byte[1] = bytes[7];
uint16_t total_message_length = message_length.UInt16 + 13;
//}
if(bytes.size() == total_message_length) { //>=
//if(bytes.size() == 165) { //>=
if(bytes[total_message_length-1] != 0xE7 || bytes[total_message_length-2] != 0xE7) {
//if(bytes[164] != 0xE7 || bytes[163] != 0xE7) {
bytes.clear();
ESP_LOGD("custom", "error in reading message");
continue;
}
ESP_LOGD("custom", "succesfully read realtime data");
ESP_LOGI("custom", "User data length: %i", message_length.UInt16);
ESP_LOGI("custom", "Total message length: %i", total_message_length);
millis_lastmessage = millis();
inverter_mode = 1;
TwoByte grid_power_value;
grid_power_value.Byte[0] = bytes[10];
grid_power_value.Byte[1] = bytes[9];
id(grid_power).publish_state(grid_power_value.UInt16);
delay(50);
TwoByte generation_power_value;
generation_power_value.Byte[0] = bytes[12];
generation_power_value.Byte[1] = bytes[11];
id(generation_power).publish_state(generation_power_value.UInt16);
delay(50);
TwoByte loads_power_value;
loads_power_value.Byte[0] = bytes[14];
loads_power_value.Byte[1] = bytes[13];
id(loads_power).publish_state(loads_power_value.UInt16);
delay(50);
TwoByte grid_voltage_r_value;
grid_voltage_r_value.Byte[0] = bytes[16];
grid_voltage_r_value.Byte[1] = bytes[15];
id(grid_voltage_r).publish_state(grid_voltage_r_value.UInt16);
delay(50);
TwoByte grid_current_r_value;
grid_current_r_value.Byte[0] = bytes[18];
grid_current_r_value.Byte[1] = bytes[17];
id(grid_current_r).publish_state(grid_current_r_value.UInt16);
delay(50);
TwoByte grid_frequency_r_value;
grid_frequency_r_value.Byte[0] = bytes[20];
grid_frequency_r_value.Byte[1] = bytes[19];
id(grid_frequency_r).publish_state(grid_frequency_r_value.UInt16);
delay(50);
TwoByte grid_power_r_value;
grid_power_r_value.Byte[0] = bytes[22];
grid_power_r_value.Byte[1] = bytes[21];
id(grid_power_r).publish_state(grid_power_r_value.UInt16);
delay(50);
TwoByte grid_voltage_s_value;
grid_voltage_s_value.Byte[0] = bytes[24];
grid_voltage_s_value.Byte[1] = bytes[23];
id(grid_voltage_s).publish_state(grid_voltage_s_value.UInt16);
delay(50);
TwoByte grid_current_s_value;
grid_current_s_value.Byte[0] = bytes[26];
grid_current_s_value.Byte[1] = bytes[25];
id(grid_current_s).publish_state(grid_current_s_value.UInt16);
delay(50);
TwoByte grid_frequency_s_value;
grid_frequency_s_value.Byte[0] = bytes[28];
grid_frequency_s_value.Byte[1] = bytes[27];
id(grid_frequency_s).publish_state(grid_frequency_s_value.UInt16);
delay(50);
TwoByte grid_power_s_value;
grid_power_s_value.Byte[0] = bytes[30];
grid_power_s_value.Byte[1] = bytes[29];
id(grid_power_s).publish_state(grid_power_s_value.UInt16);
delay(50);
TwoByte grid_voltage_T_value;
grid_voltage_T_value.Byte[0] = bytes[32];
grid_voltage_T_value.Byte[1] = bytes[31];
id(grid_voltage_T).publish_state(grid_voltage_T_value.UInt16);
delay(50);
TwoByte grid_current_T_value;
grid_current_T_value.Byte[0] = bytes[34];
grid_current_T_value.Byte[1] = bytes[33];
id(grid_current_T).publish_state(grid_current_T_value.UInt16);
delay(50);
TwoByte grid_frequency_T_value;
grid_frequency_T_value.Byte[0] = bytes[36];
grid_frequency_T_value.Byte[1] = bytes[35];
id(grid_frequency_T).publish_state(grid_frequency_T_value.UInt16);
delay(50);
TwoByte grid_power_T_value;
grid_power_T_value.Byte[0] = bytes[38];
grid_power_T_value.Byte[1] = bytes[37];
id(grid_power_T).publish_state(grid_power_T_value.UInt16);
delay(50);
TwoByte pv1_voltage_value;
pv1_voltage_value.Byte[0] = bytes[40];
pv1_voltage_value.Byte[1] = bytes[39];
id(pv1_voltage).publish_state(pv1_voltage_value.UInt16);
delay(50);
TwoByte pv1_current_value;
pv1_current_value.Byte[0] = bytes[42];
pv1_current_value.Byte[1] = bytes[41];
id(pv1_current).publish_state(pv1_current_value.UInt16);
delay(50);
TwoByte pv2_voltage_value;
pv2_voltage_value.Byte[0] = bytes[46];
pv2_voltage_value.Byte[1] = bytes[45];
id(pv2_voltage).publish_state(pv2_voltage_value.UInt16);
delay(50);
TwoByte pv2_current_value;
pv2_current_value.Byte[0] = bytes[48];
pv2_current_value.Byte[1] = bytes[47];
id(pv2_current).publish_state(pv2_current_value.UInt16);
delay(50);
TwoByte pv3_voltage_value;
pv3_voltage_value.Byte[0] = bytes[52];
pv3_voltage_value.Byte[1] = bytes[51];
id(pv3_voltage).publish_state(pv3_voltage_value.UInt16);
delay(50);
TwoByte pv3_current_value;
pv3_current_value.Byte[0] = bytes[54];
pv3_current_value.Byte[1] = bytes[53];
id(pv3_current).publish_state(pv3_current_value.UInt16);
delay(50);
TwoByte pv4_voltage_value;
pv4_voltage_value.Byte[0] = bytes[58];
pv4_voltage_value.Byte[1] = bytes[57];
id(pv4_voltage).publish_state(pv4_voltage_value.UInt16);
delay(50);
TwoByte pv4_current_value;
pv4_current_value.Byte[0] = bytes[60];
pv4_current_value.Byte[1] = bytes[59];
id(pv4_current).publish_state(pv4_current_value.UInt16);
delay(50);
TwoByte boost_temperature_value;
boost_temperature_value.Byte[0] = bytes[64];
boost_temperature_value.Byte[1] = bytes[63];
id(boost_temperature).publish_state(boost_temperature_value.UInt16);
delay(50);
TwoByte inverter_temperature_value;
inverter_temperature_value.Byte[0] = bytes[66];
inverter_temperature_value.Byte[1] = bytes[65];
id(inverter_temperature).publish_state(inverter_temperature_value.UInt16);
delay(50);
TwoByte ambient_temperature_value;
ambient_temperature_value.Byte[0] = bytes[68];
ambient_temperature_value.Byte[1] = bytes[67];
id(ambient_temperature).publish_state(ambient_temperature_value.UInt16);
delay(50);
TwoByte today_yield_value;
today_yield_value.Byte[0] = bytes[70];
today_yield_value.Byte[1] = bytes[69];
id(today_yield).publish_state(today_yield_value.UInt16);
delay(50);
uint32_t generation_total_value = int(
(unsigned char)(bytes[71]) << 24 |
(unsigned char)(bytes[72]) << 16 |
(unsigned char)(bytes[73]) << 8 |
(unsigned char)(bytes[74]));
id(generation_total).publish_state(generation_total_value);
delay(50);
uint32_t FaultMessage1_value = int(
(unsigned char)(bytes[125]) << 24 |
(unsigned char)(bytes[126]) << 16 |
(unsigned char)(bytes[127]) << 8 |
(unsigned char)(bytes[128]));
delay(50);
uint32_t FaultMessage2_value = int(
(unsigned char)(bytes[129]) << 24 |
(unsigned char)(bytes[130]) << 16 |
(unsigned char)(bytes[131]) << 8 |
(unsigned char)(bytes[132]));
delay(50);
uint32_t FaultMessage3_value = int(
(unsigned char)(bytes[133]) << 24 |
(unsigned char)(bytes[134]) << 16 |
(unsigned char)(bytes[135]) << 8 |
(unsigned char)(bytes[136]));
delay(50);
uint32_t FaultMessage4_value = int(
(unsigned char)(bytes[137]) << 24 |
(unsigned char)(bytes[138]) << 16 |
(unsigned char)(bytes[139]) << 8 |
(unsigned char)(bytes[140]));
delay(50);
uint32_t FaultMessage5_value = int(
(unsigned char)(bytes[141]) << 24 |
(unsigned char)(bytes[142]) << 16 |
(unsigned char)(bytes[143]) << 8 |
(unsigned char)(bytes[144]));
delay(50);
uint32_t FaultMessage6_value = int(
(unsigned char)(bytes[145]) << 24 |
(unsigned char)(bytes[146]) << 16 |
(unsigned char)(bytes[147]) << 8 |
(unsigned char)(bytes[148]));
delay(50);
uint32_t FaultMessage7_value = int(
(unsigned char)(bytes[149]) << 24 |
(unsigned char)(bytes[150]) << 16 |
(unsigned char)(bytes[151]) << 8 |
(unsigned char)(bytes[152]));
delay(50);
uint32_t FaultMessage8_value = int(
(unsigned char)(bytes[153]) << 24 |
(unsigned char)(bytes[154]) << 16 |
(unsigned char)(bytes[155]) << 8 |
(unsigned char)(bytes[156]));
delay(50);
if(FaultMessage1_value != 0 || FaultMessage2_value != 0 || FaultMessage3_value != 0 || FaultMessage4_value != 0 || FaultMessage5_value != 0 || FaultMessage6_value != 0 || FaultMessage7_value != 0 || FaultMessage8_value != 0) {
ESP_LOGI("custom", "Fault message 1: %i", FaultMessage1_value);
ESP_LOGI("custom", "Fault message 2: %i", FaultMessage2_value);
ESP_LOGI("custom", "Fault message 3: %i", FaultMessage3_value);
ESP_LOGI("custom", "Fault message 4: %i", FaultMessage4_value);
ESP_LOGI("custom", "Fault message 5: %i", FaultMessage5_value);
ESP_LOGI("custom", "Fault message 6: %i", FaultMessage6_value);
ESP_LOGI("custom", "Fault message 7: %i", FaultMessage7_value);
ESP_LOGI("custom", "Fault message 8: %i", FaultMessage8_value);
inverter_mode = 2; //error
}
else {
inverter_mode = 1; //online
}
//calculate PV power from pv current and pv voltage:
uint32_t pv1_power_value = pv1_current_value.UInt16 * pv1_voltage_value.UInt16 * 0.01;
id(pv1_power).publish_state(pv1_power_value);
delay(50);
uint32_t pv2_power_value = pv2_current_value.UInt16 * pv2_voltage_value.UInt16 * 0.01;
id(pv2_power).publish_state(pv2_power_value);
delay(50);
uint32_t pv3_power_value = pv3_current_value.UInt16 * pv3_voltage_value.UInt16 * 0.01;
id(pv3_power).publish_state(pv3_power_value);
delay(50);
uint32_t pv4_power_value = pv4_current_value.UInt16 * pv4_voltage_value.UInt16 * 0.01;
id(pv4_power).publish_state(pv4_power_value);
delay(50);
id(inverter_state).publish_state(inverter_mode);
bytes.clear();
}
else {
}
}
}
typedef union
{
unsigned char Byte[2];
int16_t Int16;
uint16_t UInt16;
unsigned char UChar;
char Char;
}TwoByte;};