-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSource_Externe.ino
More file actions
210 lines (201 loc) · 6.16 KB
/
Copy pathSource_Externe.ino
File metadata and controls
210 lines (201 loc) · 6.16 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
// ***************************************************************
// * Client d'un autre ESP32 en charge de mesurer les puissances *
// ***************************************************************
void CallESP32_Externe() {
String S = "";
String RMSExtDataB = "";
String Gr[4];
String data_[22];
// Use WiFiClient class to create TCP connections
WiFiClient clientESP_RMS;
if (RMS_NbCx[RMSextIdx] < 100) RMS_NbCx[RMSextIdx]++;
String host = IP2String(RMSextIP);
if (!clientESP_RMS.connect(host.c_str(), 80, 3000)) {
delay(500);
if (!clientESP_RMS.connect(host.c_str(), 80, 3000)) {
delay(100);
clientESP_RMS.stop();
StockMessage("Connection to ESP_RMS : " + host + " failed");
if (RMS_Note[RMSextIdx] > 0) RMS_Note[RMSextIdx]--;
delay(100);
return;
}
}
String url = "/ajax_data";
clientESP_RMS.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (clientESP_RMS.available() == 0) {
if (millis() - timeout > 5000) {
clientESP_RMS.stop();
delay(100);
StockMessage("client ESP_RMS Timeout !" + host);
if (RMS_Note[RMSextIdx] > 0) RMS_Note[RMSextIdx]--;
return;
}
}
timeout = millis();
// Lecture des données brutes distantes
while (clientESP_RMS.available() && (millis() - timeout < 5000)) {
RMSExtDataB += clientESP_RMS.readStringUntil('\r');
}
clientESP_RMS.stop();
if (RMSExtDataB.length() > 400) {
RMSExtDataB = "";
}
if (RMSExtDataB.indexOf("Deb") >= 0 && RMSExtDataB.indexOf("Fin") > 0) { //Trame complète reçue
RMSExtDataB = RMSExtDataB.substring(RMSExtDataB.indexOf("Deb") + 4);
RMSExtDataB = RMSExtDataB.substring(0, RMSExtDataB.indexOf("Fin") + 3);
if (RMS_Note[RMSextIdx] < 100) RMS_Note[RMSextIdx]++;
RMS_NbCx[RMSextIdx] = max(RMS_NbCx[RMSextIdx], RMS_Note[RMSextIdx]);
String Sval = "";
int idx = 0;
while (RMSExtDataB.indexOf(GS) > 0) {
Sval = RMSExtDataB.substring(0, RMSExtDataB.indexOf(GS));
RMSExtDataB = RMSExtDataB.substring(RMSExtDataB.indexOf(GS) + 1);
Gr[idx] = Sval;
idx++;
}
Gr[idx] = RMSExtDataB;
idx = 0;
for (int i = 0; i < 3; i++) {
while (Gr[i].indexOf(RS) >= 0) {
Sval = Gr[i].substring(0, Gr[i].indexOf(RS));
Gr[i] = Gr[i].substring(Gr[i].indexOf(RS) + 1);
data_[idx] = Sval;
if (idx<21) idx++;
}
data_[idx] = Gr[i];
if (idx<21) idx++;
}
for (int i = 0; i <= idx; i++) {
switch (i) {
case 0:
if (Horloge == 5) { //Mise à l'heure par ESP externe
int p = data_[i].indexOf(" ");
String New_H=data_[i].substring(p);
String New_J=data_[i].substring(0, p);
MiseAheure(New_H, New_J);
}
break;
case 1:
Source_data = data_[i];
break;
case 2:
if (TempoRTEon == 0) LTARF = data_[i];
break;
case 3:
if (TempoRTEon == 0) STGEt = data_[i];
break;
case 4:
//Temperature non utilisé
break;
case 5:
Pva_valide = data_[i].toInt();
break;
case 6:
PuissanceS_M = PintMax(data_[i].toInt()) + OffsetP; //OffsetP pour des essais uniquement
break;
case 7:
PuissanceI_M = PintMax(data_[i].toInt());
break;
case 8:
PVAS_M = PintMax(data_[i].toInt());
break;
case 9:
PVAI_M = PintMax(data_[i].toInt());
break;
case 10:
EnergieJour_M_Soutiree = data_[i].toInt();
break;
case 11:
EnergieJour_M_Injectee = data_[i].toInt();
break;
case 12:
Energie_M_Soutiree = data_[i].toInt();
break;
case 13:
Energie_M_Injectee = data_[i].toInt();
PuissanceRecue = true; //Reset du Watchdog à chaque trame du RMS reçue
cptLEDyellow = 4;
EnergieActiveValide = true;
break;
case 14: //CAS UxIx2 avec une deuxieme sonde
PuissanceS_T = data_[i].toInt();
break;
case 15:
PuissanceI_T = data_[i].toInt();
break;
case 16:
PVAS_T = data_[i].toInt();
break;
case 17:
PVAI_T = data_[i].toInt();
break;
case 18:
EnergieJour_T_Soutiree = data_[i].toInt();
break;
case 19:
EnergieJour_T_Injectee = data_[i].toInt();
break;
case 20:
Energie_T_Soutiree = data_[i].toInt();
break;
case 21:
Energie_T_Injectee = data_[i].toInt();
break;
}
}
String G = Gr[3];
String val[10];
int p1= G.indexOf(FS);
if (p1>=0){ //Vérifie présence du groupe pour gestion partie triphasé
G= G.substring(p1+1);
G = G.substring(0, G.indexOf(FS));
int j = 0;
// découpe complète
while (G.indexOf(RS) >= 0 && j < 10) {
val[j++] = G.substring(0, G.indexOf(RS));
G = G.substring(G.indexOf(RS) + 1);
}
if (j < 10) {
val[j++] = G; // dernier élément
}
// --- PARTIE FIXE (toujours présente) ---
if (j >= 8) {
Tension_M = val[0].toFloat();
Intensite_M = val[1].toFloat();
Tension_M1 = val[2].toFloat();
Intensite_M1= val[3].toFloat();
Tension_M2 = val[4].toFloat();
Intensite_M2= val[5].toFloat();
Tension_M3 = val[6].toFloat();
Intensite_M3= val[7].toFloat();
}
// --- PARTIE OPTIONNELLE (énergies T) ---
if (j >= 10) {
Tension_T = val[8].toFloat();
Intensite_T = val[9].toFloat();
}
}
RMSExtDataB = "";
}
}
void IndexSource() {
RMSextIdx = 0;
if (RMSextIP > 0 && Source == "Ext") {
for (int8_t i = 1; i < LES_ROUTEURS_MAX; i++) {
if (RMS_IP[i] == RMSextIP) {
RMSextIdx = i;
}
}
if (RMSextIdx == 0) {
for (int8_t i = 1; i < LES_ROUTEURS_MAX; i++) {
if (RMS_IP[i] == 0) {
RMS_IP[i] = RMSextIP;
RMSextIdx = i;
i = 100;
}
}
}
}
}