forked from F1ATB/Solar-Router-F1ATB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommonFx.ino
More file actions
220 lines (204 loc) · 6.07 KB
/
commonFx.ino
File metadata and controls
220 lines (204 loc) · 6.07 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
#include <Arduino.h>
void SplitS(String Str, String &Before, String Separ, String &After) {
int p = Str.indexOf(Separ);
Before = Str.substring(0, p);
After = Str.substring(p + 1);
}
// Conversion des adresses IP suivant le coeur
String IP2String(unsigned long IP) {
byte arr[4];
arr[0] = IP & 0xFF; // 0x78
arr[1] = (IP >> 8) & 0xFF; // 0x56
arr[2] = (IP >> 16) & 0xFF; // 0x34
arr[3] = (IP >> 24) & 0xFF; // 0x12
for (int i = 0; i < 4; i++) { //Pour eviter corruption données entre coeur 0 et coeur1 (arr en variable local)
arrIP[i] = arr[i]; //Pour le WIFI au setup
}
return String(arr[3]) + "." + String(arr[2]) + "." + String(arr[1]) + "." + String(arr[0]);
}
unsigned long String2IP(String S) {
unsigned long IP = 0;
for (int i = 0; i < 3; i++) {
int p = S.indexOf(".");
String s = S.substring(0, p);
S = S.substring(p + 1);
IP += s.toInt();
IP = IP * 256;
}
IP += S.toInt();
return IP;
}
//Gestion couleurs
String ULtoHex(unsigned long x) {
char buffer[15];
ltoa(x, buffer, 16);
String S = "000000" + String(buffer);
int p = S.length();
S = "#" + S.substring(p - 6); //Format pour HTML color
return S;
}
unsigned long ConvCouleur(String V) { //Notation CSS en UL
return strtoul(V.c_str(), NULL, 16);
}
//Telnet et Serial
//****************
// --- Fonction de sortie partagée ---
void TelnetPrint(const String &message) {
Serial.print(message); // Sortie sur port série
if (telnetClient && telnetClient.connected() && TelnetOn) {
telnetClient.print(message); // Sortie sur Telnet
}
}
void TelnetPrintln(const String &message) {
Serial.println(message);
if (telnetClient && telnetClient.connected() && TelnetOn) {
telnetClient.println(message);
}
}
// PORT SERIE ou TELNET
void LireSerial() {
int inbyte;
//Port Serie
while (Serial.available() > 0) {
inbyte = Serial.read();
if ((inbyte == 10) || (inbyte == 13)) {
DecodeSerial();
} else {
SerialIn += String(char(inbyte));
}
}
//Telnet
if (telnetClient && telnetClient.connected() && telnetClient.available() && TelnetOn) {
inbyte = telnetClient.read(); // Lire caractère
if ((inbyte == 10) || (inbyte == 13)) { // Fin de ligne → commande complète
DecodeSerial();
} else {
if (inbyte >= 32) {
SerialIn += String(char(inbyte)); // Ajouter caractère au buffer
}
}
}
}
void DecodeSerial() {
String sw;
String valeur = "";
int p;
SerialIn.trim();
p = SerialIn.indexOf(":");
if (p > 0) {
sw = SerialIn.substring(0, p + 1);
valeur = SerialIn.substring(p + 1);
sw.trim();
valeur.trim();
} else {
sw = SerialIn;
}
if (sw.indexOf("restart") >= 0) {
ESP.restart();
}
if (sw.indexOf("ssid:") >= 0) {
ssid = valeur;
ModeReseau = 0; // A priori
dhcpOn = 1; //Au cas ou l'on change de mapping des adresses LAN
if (ESP32_Type >= 10) { //Carte Ethernet
ESP32_Type = 0;
}
EcritureEnROM();
}
if (sw.indexOf("password:") >= 0) {
password = valeur;
EcritureEnROM();
}
if (sw.indexOf("ETH01") >= 0) {
ESP32_Type = 10;
EcritureEnROM();
}
if (sw.indexOf("dispPw") >= 0) {
dispPw = !dispPw;
}
if (sw.indexOf("dispAct") >= 0) {
dispAct = !dispAct;
}
if (sw.indexOf("P:") >= 0 || sw.indexOf("T:") >= 0) {
if (sw.indexOf("T:") >= 0) {
testTrame = valeur.toInt();
}
if (sw.indexOf("P:") >= 0) {
testPulse = valeur.toInt();
}
if (testPulse > testTrame) testPulse = testTrame;
TelnetPrintln("Rapport cyclique Pulse " + String(testPulse * 10) + " ms / Trame " + String(testTrame * 10) + " ms : P=" + String(testPulse) + " T=" + String(testTrame));
}
if (sw.indexOf("R:") >= 0) {
if (valeur == "") {
RetardVx = -1;
} else {
RetardVx = valeur.toInt();
}
}
if (sw.indexOf("Offset:") >= 0) { //Decalage mesure Puissance pour essais
OffsetP = valeur.toInt();
}
if ((sw.indexOf("H") >= 0 || sw.indexOf("?") >= 0) && p == -1) {
MessageCommandes();
}
if (SerialIn != "") TelnetPrintln(">>" + SerialIn);
SerialIn = "";
}
// commandes disponibles par port serie ou Telnet
const char *helpSerial = R"====(
**************
commandes pour configuration par port série ou Telnet (respect majuscules, ponctuation et terminer par touche 'Enter'):
ssid:xxx | Pour définir le nom xxx du Wifi à utiliser
password:yyy | Pour définir le mot de passe yyy du Wifi
restart | Pour redémarrer l'ESP32 sans appui sur le bouton EN
dispPw | Pour afficher les mesures de puissance Pw
dispAct | Pour afficher les ouvertures des Actions
ETH01 | Bascule sur la config ethernet avec bus RMII et LAN8720
T:xxx | En mode Train de Sinus force la longeur de Trame.
| T:xxx = xxx*10ms, T:0 retourne en Train de sinus normal
P:yyy | En mode Train de Sinus force la longueur des Pulses.
| P:yyy = yyy*10ms. P<=T . Evitez P impaire et Trame paire.
R:x | Affiche pour le Triac (x=0) ou les Relais (1,2..) ,
| le Retard en% somme de| Propor | Integral | Dérivé.
| R: pour annuler
H ou ? | pour avoir cette aide
**************
)====";
void MessageCommandes() {
String M = helpSerial;
String Before;
while (M.indexOf("\n") >= 0) {
SplitS(M, Before, "\n", M);
TelnetPrintln(Before);
}
}
//*************
//* Test Pmax *
//*************
float PfloatMax(float Pin) {
return constrain(Pin, float(-1.0F * PmaxReseau), float(PmaxReseau));
}
int PintMax(int Pin) {
return constrain(Pin, int(-1 * PmaxReseau), int(PmaxReseau));
}
// Decodage URL
String urlDecode(const String &src) {
String result;
result.reserve(src.length());
for (int i = 0; i < src.length(); i++) {
char c = src[i];
if (c == '%') {
if (i + 2 < src.length()) {
char hex[3] = { src[i+1], src[i+2], 0 };
result += (char) strtol(hex, nullptr, 16);
i += 2;
}
} else if (c == '+') {
result += ' '; // Le + représente un espace dans les URL
} else {
result += c;
}
}
return result;
}