-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSource_EnphaseEnvoy.ino
More file actions
412 lines (394 loc) · 15.3 KB
/
Source_EnphaseEnvoy.ino
File metadata and controls
412 lines (394 loc) · 15.3 KB
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
//***********************************
//* Source EnPhase Envoy V5 ou V7 *
//***********************************
uint32_t ipToInt(IPAddress ip) { //SR19
return uint32_t(ip[0] << 24) | uint32_t(ip[1] << 16) | uint32_t(ip[2] << 8) | uint32_t(ip[3]); //SR19
}
void Setup_Enphase() {
//Résolution mDNS de http://envoy.local en adresse IP //SR19
//*************************************************** //SR19
const char* host = "envoy"; //SR19
IPAddress envoyIP; //SR19
if (!MDNS.begin(hostname)) { //Init mDNS //SR19
TelnetPrintln("Erreur : impossible d'initialiser mDNS"); //SR19
return; //SR19
} else { //SR19
envoyIP = MDNS.queryHost(host, 2000); //avec timeout 2s //SR19
} //SR19
if (envoyIP.toString() != "0.0.0.0") { //SR19
StockMessage("IP Enphase : http://" + String(host) + ".local" + " -> " + envoyIP.toString()); //SR19
RMSextIP = ipToInt(envoyIP); //IP -> uint32 //SR19
} else { //SR19
StockMessage("Échec! passerelle Enphase envoy déconnectée"); //SR19
return; //SR19
}
//Obtention Session ID
//********************
const char* server1Enphase = "enlighten.enphaseenergy.com";
String Host = String(server1Enphase);
String adrEnphase = "https://" + Host + "/login/login.json";
String requestBody = "user[email]=" + EnphaseUser + "&user[password]=" + urlEncode(EnphasePwd);
if (EnphaseUser != "" && EnphasePwd != "" && envoyIP.toString() != "0.0.0.0") { // test envoyIP si perte de connexion //SR19
TelnetPrintln("Essai connexion Enlighten server 1 pour obtention session_id!");
clientSecu.setInsecure(); //skip verification
if (!clientSecu.connect(server1Enphase, 443, 3000))
StockMessage("Connection failed to Enlighten server :" + Host);
else {
TelnetPrintln("Connected to Enlighten server:" + Host);
clientSecu.println("POST " + adrEnphase + "?" + requestBody + " HTTP/1.0");
clientSecu.println("Host: " + Host);
clientSecu.println("Connection: close");
clientSecu.println();
String line = "";
while (clientSecu.connected()) {
line = clientSecu.readStringUntil('\n');
if (line == "\r") {
TelnetPrintln("headers 1 Enlighten received");
JsonToken = "";
}
JsonToken += line;
}
// if there are incoming bytes available
// from the server, read them and print them:
while (clientSecu.available()) {
char c = clientSecu.read();
Serial.write(c);
}
clientSecu.stop();
}
Session_id = StringJson("session_id", JsonToken);
TelnetPrintln("session_id :" + Session_id);
} else {
TelnetPrintln("Connexion vers Envoy-S en firmware version 5");
}
//Obtention Token
//********************
if (Session_id != "" && EnphaseSerial != "" && EnphaseUser != "") {
const char* server2Enphase = "entrez.enphaseenergy.com";
Host = String(server2Enphase);
adrEnphase = "https://" + Host + "/tokens";
requestBody = "{\"session_id\":\"" + Session_id + "\", \"serial_num\":" + EnphaseSerial + ", \"username\":\"" + EnphaseUser + "\"}";
TelnetPrintln("Essai connexion Enlighten server 2 pour obtention token!");
clientSecu.setInsecure(); //skip verification
if (!clientSecu.connect(server2Enphase, 443, 3000))
StockMessage("Connection failed to :" + Host);
else {
TelnetPrintln("Connected to :" + Host);
clientSecu.println("POST " + adrEnphase + " HTTP/1.0");
clientSecu.println("Host: " + Host);
clientSecu.println("Content-Type: application/json");
clientSecu.println("content-length:" + String(requestBody.length()));
clientSecu.println("Connection: close");
clientSecu.println();
clientSecu.println(requestBody);
clientSecu.println();
TelnetPrintln("Attente user est connecté");
String line = "";
JsonToken = "";
while (clientSecu.connected()) {
line = clientSecu.readStringUntil('\n');
if (line == "\r") {
TelnetPrintln("headers 2 enlighten received");
JsonToken = "";
}
JsonToken += line;
}
// if there are incoming bytes available
// from the server, read them and print them:
while (clientSecu.available()) {
char c = clientSecu.read();
Serial.write(c);
}
clientSecu.stop();
JsonToken.trim();
TelnetPrintln("Token :" + JsonToken);
if (JsonToken.length() > 50) {
TokenEnphase = JsonToken;
previousTimeRMSMin = 1000;
previousTimeRMSMax = 1;
previousTimeRMSMoy = 1;
previousTimeRMS = millis();
LastRMS_Millis = millis();
PeriodeProgMillis = 1000;
}
}
}
}
void LectureEnphase() { //Lecture des consommations
int Num_portIQ = 443;
String JsonEnPhase = "";
String host = IP2String(RMSextIP);
if (TokenEnphase.length() > 50 && EnphaseUser != "") { //Connexion pour firmware V7
if ((millis() - lastTokenUpdate) > 2592000000) { //Tout les 30 jours on recherche un nouveau Token //SR19
lastTokenUpdate = millis(); // overflow compatible! //SR19
Setup_Enphase();
}
clientSecu.setInsecure(); //skip verification
if (!clientSecu.connect(host.c_str(), Num_portIQ, 3000)) {
StockMessage("Connection failed to Envoy-S server! : " + host);
} else {
//TelnetPrintln("Connected to Envoy-S server!");
clientSecu.println("GET https://" + host + "/ivp/meters/reports/consumption HTTP/1.0");
clientSecu.println("Host: " + host);
clientSecu.println("Accept: application/json");
clientSecu.println("Authorization: Bearer " + TokenEnphase);
clientSecu.println("Connection: close");
clientSecu.println();
String line = "";
while (clientSecu.connected()) {
line = clientSecu.readStringUntil('\n');
if (line == "\r") {
//TelnetPrintln("headers received");
JsonEnPhase = "";
}
JsonEnPhase += line;
}
// if there are incoming bytes available
// from the server, read them and print them:
while (clientSecu.available()) {
char c = clientSecu.read();
Serial.write(c);
}
clientSecu.stop();
}
} else { // Connexion Envoy V5
// Use WiFiClient class to create TCP connections http
WiFiClient clientFirmV5;
if (!clientFirmV5.connect(host.c_str(), 80, 3000)) {
StockMessage("connection to client clientFirmV5 failed (call to Envoy-S)");
delay(200);
return;
}
String url = "/ivp/meters/reports/consumption";
clientFirmV5.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
;
unsigned long timeout = millis();
while (clientFirmV5.available() == 0) {
if (millis() - timeout > 5000) {
TelnetPrintln(">>> client clientFirmV5 Timeout !");
clientFirmV5.stop();
return;
}
}
timeout = millis();
String line;
// Lecture des données brutes distantes
while (clientFirmV5.available() && (millis() - timeout < 5000)) {
line = clientFirmV5.readStringUntil('\n');
if (line == "\r") {
//TelnetPrintln("headers received");
JsonEnPhase = "";
}
JsonEnPhase += line;
}
}
// On utilise pas la librairie ArduinoJson.h, pour décoder message Json, qui crache sur de grosses données
String TotConso = PrefiltreJson("total-consumption", "cumulative", JsonEnPhase);
PactConso_M = int(ValJson("actPower", TotConso));
String NetConso = PrefiltreJson("net-consumption", "cumulative", JsonEnPhase);
float PactReseau = ValJson("actPower", NetConso);
PactReseau = PfloatMax(PactReseau);
if (PactReseau < 0) {
PuissanceS_M_inst = 0;
PuissanceI_M_inst = int(-PactReseau);
} else {
PuissanceI_M_inst = 0;
PuissanceS_M_inst = int(PactReseau);
}
float PvaReseau = ValJson("apprntPwr", NetConso);
PvaReseau = PfloatMax(PvaReseau);
if (PvaReseau < 0) {
PVAS_M_inst = 0;
PVAI_M_inst = int(-PvaReseau);
} else {
PVAI_M_inst = 0;
PVAS_M_inst = int(PvaReseau);
}
Pva_valide = true;
filtre_puissance();
float PowerFactor = 0;
if ((PVA_M_moy) != 0) {
PowerFactor = floor(100 * abs(Puissance_M_moy) / PVA_M_moy) / 1.0;
PowerFactor = min(PowerFactor, float(1));
}
PowerFactor_M = PowerFactor;
long whDlvdCum = LongJson("whDlvdCum", NetConso);
long DeltaWh = 0;
if (whDlvdCum != 0) { // bonne donnée
if (LastwhDlvdCum == 0) {
LastwhDlvdCum = whDlvdCum;
}
DeltaWh = whDlvdCum - LastwhDlvdCum;
LastwhDlvdCum = whDlvdCum;
if (DeltaWh < 0) {
Energie_M_Injectee = Energie_M_Injectee - DeltaWh;
} else {
Energie_M_Soutiree = Energie_M_Soutiree + DeltaWh;
}
}
Tension_M = ValJson("rmsVoltage", NetConso);
Intensite_M = ValJson("rmsCurrent", NetConso);
PactProd = PactConso_M - int(PactReseau);
EnergieActiveValide = true;
if (PactReseau != 0 || PvaReseau != 0) {
PuissanceRecue = true; //Reset du Watchdog à chaque trame reçue de la passerelle Envoy-S metered
}
if (cptLEDyellow > 30) {
cptLEDyellow = 4;
}
}
String PrefiltreJson(String F1, String F2, String Json) {
int p = Json.indexOf(F1);
Json = Json.substring(p);
p = Json.indexOf(F2);
Json = Json.substring(p);
return Json;
}
String SubJson(String F1, String F2, String Json) {
int p = Json.indexOf(F1);
Json = Json.substring(p);
p = Json.indexOf(F2);
Json = Json.substring(0, p + 1);
return Json;
}
float ValJson(String nom, String Json) {
int p = Json.indexOf(nom + "\":");
Json = Json.substring(p);
p = Json.indexOf(":");
Json = Json.substring(p + 1);
int q = Json.indexOf(",");
p = Json.indexOf("}");
p = min(p, q);
float val = 0;
if (p > 0) {
Json = Json.substring(0, p);
val = Json.toFloat();
}
return val;
}
long LongJson(String nom, String Json) { // Pour éviter des problèmes d'overflow
int p = Json.indexOf(nom + "\":");
Json = Json.substring(p);
p = Json.indexOf(":");
Json = Json.substring(p + 1);
int q = Json.indexOf(".");
p = Json.indexOf("}");
p = min(p, q);
long val = 0;
if (p > 0) {
Json = Json.substring(0, p);
val = Json.toInt();
}
return val;
}
long myLongJson(String nom, String Json) { // Alternative a LongJson au dessus pour extraire chez RTE nb jour Tempo https://particulier.RTE.fr/services/rest/referentiel/getNbTempoDays?TypeAlerte=TEMPO
int p = Json.indexOf(nom + "\":");
Json = Json.substring(p);
p = Json.indexOf(":");
Json = Json.substring(p + 1);
int q = Json.indexOf(","); //<==== Recherche d'une virgule et non d'un point
if (q == -1) q = 999; // /<==== Ajout de ces 2 lignes pour que la ligne p = min(p, q); ci dessous donne le bon résultat
p = Json.indexOf("}");
p = min(p, q);
long val = 0;
if (p > 0) {
Json = Json.substring(0, p);
val = Json.toInt();
}
return val;
}
unsigned long ULongJson(String nom, String Json) { // Alternative a LongJson au dessus pour extraire chez RTE nb jour Tempo https://particulier.RTE.fr/services/rest/referentiel/getNbTempoDays?TypeAlerte=TEMPO
int p = Json.indexOf(nom + "\":");
Json = Json.substring(p);
p = Json.indexOf(":");
Json = Json.substring(p + 1);
int q = Json.indexOf(","); //<==== Recherche d'une virgule et non d'un point
if (q == -1) q = 999; // /<==== Ajout de ces 2 lignes pour que la ligne p = min(p, q); ci dessous donne le bon résultat
p = Json.indexOf("}");
p = min(p, q);
unsigned long val = 0;
if (p > 0) {
Json = Json.substring(0, p);
Json = "0000" + Json;
int L = Json.length();
unsigned long y = (Json.substring(0, L - 5)).toInt(); //Problème des valeurs signées dans un unsigned
unsigned long z = (Json.substring(L - 5)).toInt();
val = (y * 100000) + z;
}
return val;
}
int IntJson(String nom, String Json) { // Pour éviter des problèmes d'overflow
int p = Json.indexOf(nom + "\":");
Json = Json.substring(p);
p = Json.indexOf(":");
Json = Json.substring(p + 1);
int q = Json.indexOf(",");
if (q == -1) q = 999;
p = Json.indexOf("}");
p = min(p, q);
int val = 0;
if (p > 0) {
Json = Json.substring(0, p);
val = Json.toInt();
}
return val;
}
byte ByteJson(String nom, String Json) { // Pour éviter des problèmes d'overflow
int p = Json.indexOf(nom + "\":");
Json = Json.substring(p);
p = Json.indexOf(":");
Json = Json.substring(p + 1);
int q = Json.indexOf(",");
if (q == -1) q = 999;
p = Json.indexOf("}");
p = min(p, q);
byte val = 0;
if (p > 0) {
Json = Json.substring(0, p);
val = Json.toInt();
}
return val;
}
unsigned short UShortJson(String nom, String Json) { // Pour éviter des problèmes d'overflow
int p = Json.indexOf(nom + "\":");
Json = Json.substring(p);
p = Json.indexOf(":");
Json = Json.substring(p + 1);
int q = Json.indexOf(",");
if (q == -1) q = 999;
p = Json.indexOf("}");
p = min(p, q);
unsigned short val = 0;
if (p > 0) {
Json = Json.substring(0, p);
val = Json.toInt();
}
return val;
}
short ShortJson(String nom, String Json) { // Pour éviter des problèmes d'overflow
int p = Json.indexOf(nom + "\":");
Json = Json.substring(p);
p = Json.indexOf(":");
Json = Json.substring(p + 1);
int q = Json.indexOf(",");
if (q == -1) q = 999;
p = Json.indexOf("}");
p = min(p, q);
short val = 0;
if (p > 0) {
Json = Json.substring(0, p);
val = Json.toInt();
}
return val;
}
String StringJson(String nom, String Json) {
int p = Json.indexOf(nom + "\":");
Json = Json.substring(p);
p = Json.indexOf(":");
Json = Json.substring(p + 1);
p = Json.indexOf("\"");
Json = Json.substring(p + 1);
p = Json.indexOf("\"");
Json = Json.substring(0, p);
return Json;
}