-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAppWebServer.cpp
More file actions
402 lines (318 loc) · 12.2 KB
/
AppWebServer.cpp
File metadata and controls
402 lines (318 loc) · 12.2 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
/*************************************************
*************************************************
AppWebServer.cpp is part of of lib AppWebServer - Arduino lib for easy wifi interface with http and css
Copyright 2020 Pierre HENRY net23@frdev.com All - right reserved.
This file is part of AppWebServer.
AppWebServer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
AppWebServer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with AppWebServer. If not, see <https://www.gnu.org/licenses/gpl.txt>.
*/
#include "AppWebServer.h"
//#include <ESP8266WiFi.h>
// pointeur vers l'instance utilisateur
AppWebServer* AppWebPtr = NULL;
#include "AppWebConfig.h" //include LittleFS
FileConfig TWConfig;
#include <ESP8266WebServer.h> // web server to answer WEB request and EEDOMUS BOX request
#include <ESP8266mDNS.h> // not used here we stay with IP on this side (Wifimanageg need id on his side)
#include <DNSServer.h> // only used by captive to redirect web request to main page
// ====== DO NOT REMOVE ===========================
// Server Instance
//#include "user_interface.h"
ESP8266WebServer Server(SERVER_PORT); // Serveur HTTP
DNSServer dnsServer;
namespace TWS {
// Out of instance variables
String redirectUri; // uri to redirect after an onSubmit (used by AppWebHttp)
String localIp;
String TryStatus;
// Out of instance function
#include "AppWebCaptive.h" //out of instance functions
#include "AppWebHTTP.h" //out of instance functions
}
using namespace TWS;
// Objet AppWeb
//// constructeur
AppWebServer::AppWebServer() {
if (AppWebPtr != NULL) {
D1_print(F("tws - Error: Only one instance for AppWebServer"));
while (true) delay(100);
}
AppWebPtr = this;
}
// Destructor
AppWebServer::~AppWebServer() {
if (AppWebPtr == NULL) return;
this->end();
AppWebPtr = NULL;
}
void AppWebServer::end() {
// WiFi.mode(WIFI_OFF);
// // softAP = false;
// delay(10);
// WiFi.SleepBegin();
// delay(10);
Server.close();
//// delay(10);
}
void AppWebServer::begin(const String devicename ,const int debuglevel ) {
// FS
if (!TWFS.begin()) {
D1_println(F("TW: FS en erreur !!!!!"));
} else {
D_println(F("TW: FS Ok"));
}
// init random seed
randomSeed(micros());
createRandom(); //fill up first _random;
// Recuperation du fichier de config
TWConfig.read();
_defaultWebFolder = TWConfig.defaultWebFolder;
if (_defaultWebFolder.length() == 0) _defaultWebFolder = F("/web");
_captiveWebFolder = TWConfig.captiveWebFolder;
if (_captiveWebFolder.length() == 0) _captiveWebFolder = F("/web/wifisetup"); // todo: should be "/captive" ??
_captiveAP = true; //
if (TWConfig.deviceName.length() == 0) TWConfig.deviceName = devicename;
_debugLevel=debuglevel;
Serial.setDebugOutput(_debugLevel > 2);
// grab WiFi actual status
D_println(F("tws: Read wifi current status "));
D_print(F("tws: WIFI Mode "));
D_println(WiFi.getMode());
D_print(F("tws: SoftAP SSID "));
D_println(WiFi.softAPSSID());
D_print(F("tws: SoftAP IP "));
D_println(WiFi.softAPIP());
// controle de la configuration du WiFi et de la configuration demandée
//reconfig eventuelle de l'ip AP
//if ( (WiFi.getMode() & WIFI_AP) && (WiFi.softAPIP() != IPAddress(10, 10, 10, 10)) ) {
if ( WiFi.softAPIP() != IPAddress(10, 10, 10, 10) ) {
WiFiMode_t mode = WiFi.getMode();
D_print(F("WS: reconfig APIP 10.10.10.10"));
IPAddress local_IP(10, 10, 10, 10);
IPAddress mask(255, 255, 255, 0);
bool result = WiFi.softAPConfig(local_IP, local_IP, mask);
D_print(F(" ==> ")); D_println(result);
D_print(F("SW: SoftAP IP = "));
D_println(WiFi.softAPIP());
WiFi.mode(mode);
}
if ( TWConfig.bootForceAP > 0 && !(WiFi.getMode() & WIFI_AP) ) {
D_println(F("TWS: Force mode Captive AP !!!"));
WiFi.persistent(false);
WiFi.enableAP(true); // wifi est non persistant
WiFi.persistent(true);
timerCaptivePortal = TWConfig.bootForceAP * 60;
}
_deviceName = WiFi.softAPSSID(); //device name from WiFi
if ( TWConfig.deviceName != _deviceName) {
D_print(F("SW: need to init WiFi same as config !!!!! "));
setDeviceName(TWConfig.deviceName); //check devicename validity
WiFi.softAP(_deviceName);
//WiFi.persistent(true);
if (TWConfig.deviceName != WiFi.softAPSSID()) {
D_print(F("SW: need to need to rewrite config !!!!! "));
TWConfig.deviceName = WiFi.softAPSSID(); //put back devicename in config if needed
TWConfig.changed = true;
TWConfig.save();
}
}
D_print(F("TW: softap="));
D_println(WiFi.softAPSSID());
// mise en place des call back web
Server.onNotFound(HTTP_HandleRequests);
D_println(F("TW: Serveur.begin"));
Server.begin();
D_print(F("TW: AP WIFI Mode "));
D_println(WiFi.getMode());
IPAddress myIP = WiFi.softAPIP();
D_print(F("TW: AP IP address: "));
D_println(myIP);
// if (WiFi.getMode() != WIFI_OFF ) {
// bool result = MDNS.begin(_deviceName);
// Serial.print(F("TWS: MS DNS ON : "));
// Serial.print(_deviceName);
// Serial.print(F(" r="));
// Serial.println(result);
// }
return ;
}
// set device name
// Check for valid name otherwhise DEFAULT_DEVICENAME "*" is used
// if device name terminate with * we add some mac adresse number
// usefull if you setup different device at the same place
// device name is used as APname and as DNSname
void AppWebServer::setDeviceName(const String devicename) {
_deviceName = devicename;
// Check a valid hostname
// configation du nom du reseau AP : LITTLEWEB_XXYY avec les 2 dernier chifre hexa de la mac adresse
if (_deviceName.length() > 30) _deviceName.remove(30); // chop at 30char
_deviceName.trim();
if (_deviceName.length() < 4 ) _deviceName = F(DEFAULT_DEVICENAME "*");
if (_deviceName.endsWith(F("*"))) {
_deviceName.remove(_deviceName.length() - 1);
_deviceName += WiFi.macAddress().substring(12, 14);
_deviceName += WiFi.macAddress().substring(15, 17);
}
_deviceName.replace(' ', '_');
}
void AppWebServer::handleEvent() {
// Check if mode changed
WiFiMode_t wifimode = WiFi.getMode();
if ( _WiFiMode != wifimode) {
_WiFiMode = wifimode;
// grab WiFi actual mode
D1_print(F("WF: Wifi mode change to "));
D1_println(_WiFiMode);
D_println(F("SW: Read wifi current mode and config "));
D_print(F("SW: SoftAP SSID "));
D_println(WiFi.softAPSSID());
D_print(F("SW: SoftAP IP "));
D_println(WiFi.softAPIP());
D_print(F("SW: Station SSID "));
D_println(WiFi.SSID());
// D_print(F("SW: Station password "));
// D_println(WiFi.psk());
D_print(F("SW: Station IP "));
D_println(WiFi.localIP());
//wifi_softap_dhcps_stop();
captiveDNSStop();
//delay(5); // to allow MSDNS and Captive to stop
if (_WiFiMode & WIFI_AP) {
if (WiFi.softAPIP() != IPAddress(10, 10, 10, 10) ) {
// if (_WiFiMode == WIFI_AP) {
// WiFi.softAP(_deviceName);
// }
// delay(100);
D1_println(F("WS: need reconfig APIP 10.10.10.10 !!!!!!!!!!"));
D1_println(F("reset."));
delay(3000);
ESP.reset();
// IPAddress local_IP(10, 10, 10, 10);
// IPAddress mask(255, 255, 255, 0);
// bool result = WiFi.softAPConfig(local_IP, local_IP, mask);
// D_print(F("SW: softapconfig = ")); D_println(result);
// D_print(F("SW: SoftAP IP = "));
// D_println(WiFi.softAPIP());
}
D_println(F("SW: Captive start"));
captiveDNSStart();
}
// ETS_UART_INTR_DISABLE();
// WiFi.disconnect(); // this alone is not enough to stop the autoconnecter
// ETS_UART_INTR_ENABLE();
D_println(F("SW: -- end Wifi mode change"));
}
static int oldStatus = -1;
int status = WiFi.status();
// 0 : WL_IDLE_STATUS when Wi-Fi is in process of changing between statuses
// 1 : WL_NO_SSID_AVAILin case configured SSID cannot be reached
// 3 : WL_CONNECTED after successful connection is established
// 4 : WL_CONNECT_FAILED if password is incorrect
// 6 : WL_DISCONNECTED if module is not configured in station mode
if (status != oldStatus) {
TryStatus = status; // chaine pour reporting
D1_print(F("WF: Status : "));
D1_println(status);
// D_println(F("SW: Read wifi current mode and config "));
// D_print(F("SW: SoftAP SSID "));
// D_println(WiFi.softAPSSID());
// D_print(F("SW: SoftAP IP "));
// D_println(WiFi.softAPIP());
//
// D_print(F("SW: Station SSID "));
// D_println(WiFi.SSID());
//
// D_print(F("SW: Station password "));
// D_println(WiFi.psk());
//
// D_print(F("SW: Station IP "));
// D_println(WiFi.localIP());
oldStatus = status;
if (status == WL_CONNECTED) {
//MDNS.addService("http", "tcp", 80);
TWS::localIp = WiFi.localIP().toString(); // recuperation de l'ip locale
if (trySetupPtr && trySetupPtr->isTrying ) {
WiFi.enableSTA(false);
WiFi.persistent(true);
D1_println(F("WF: Moving to STATION"));
WiFi.enableAP(false); // stop AP
WiFi.begin(trySetupPtr->SSID, trySetupPtr->PASS); // save STATION setup in flash
if ( trySetupPtr->deviceName != _deviceName) {
D_print(F("SW: need to init WiFi same as new config !!!!! "));
D_print(trySetupPtr->deviceName);
D_print(F("!="));
D_println(_deviceName);
setDeviceName(trySetupPtr->deviceName); //check devicename validity
WiFi.softAP(_deviceName); // save in flash
if (TWConfig.deviceName != WiFi.softAPSSID()) {
D_print(F("SW: need to need to rewrite flah config !!!!! "));
D_print(TWConfig.deviceName);
D_print(F("!="));
D_println(WiFi.softAPSSID());
TWConfig.deviceName = WiFi.softAPSSID(); //put back devicename in config if needed
TWConfig.changed = true;
TWConfig.save();
}
WiFi.enableAP(false); // stop AP config is done
}
delete trySetupPtr;
trySetupPtr = nullptr;
}
bool result = MDNS.begin(_deviceName);
D_print(F("STA: MS DNS ON : "));
D_print(_deviceName);
D_print(F(" r="));
D_println(result);
} else {
MDNS.end(); // only if connected
}
if (trySetupPtr && status == WL_CONNECT_FAILED ) {
// bad password
D1_println(F("Bad Password"));
D_println(F("Remove try config"));
//set back old credential if any
if (trySetupPtr->SSID.length() > 0) {
WiFi.enableSTA(false);
WiFi.begin(trySetupPtr->oldSSID, trySetupPtr->oldPASS); // put back STA old credential if any
}
WiFi.persistent(true);
delete trySetupPtr;
trySetupPtr = nullptr;
}
}
MDNS.update();
Server.handleClient();
handleCaptivePortal();
}
String AppWebServer::getWebName() {
return TWConfig.webName;
}
void AppWebServer::setCallBack_OnTranslateKey(void (*ontranslatekey)(String & key)) {
onTranslateKeyPtr = ontranslatekey;
}
void AppWebServer::setCallBack_OnStartRequest(void (*onstartrequest)(const String & filename, const String & submitValue)) {
onStartRequestPtr = onstartrequest;
}
void AppWebServer::setCallBack_OnRefreshItem(bool (*onrefreshitem)(const String & keyname, String & key)) {
onRefreshItemPtr = onrefreshitem;
}
//
//
//void AppWebServer::setCallBack_OnRepeatLine(bool (*onrepeatline)(const int num)) { // call back pour gerer les Repeat
// onRepeatLinePtr = onrepeatline;
//}
bool AppWebServer::razConfig() { // efface la config enregistree
return (TWConfig.erase());
}
String AppWebServer::createRandom() {
_random = random(1000000, 9999999);
return (_random);
}