-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
ConfigHelper.h
471 lines (388 loc) · 13.7 KB
/
ConfigHelper.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Start as WiFi station
bool startWifiStation(bool stopble, bool startwebserver){
Serial.printf("[INFO]: Connecting to %s", wificonfig.ssid);
if (String(WiFi.SSID()) != String(wificonfig.ssid))
{
WiFi.mode(WIFI_STA);
WiFi.begin(wificonfig.ssid, wificonfig.password);
uint8_t attempts = wificonfig.attempts;
while (WiFi.status() != WL_CONNECTED)
{
if(attempts == 0) {
WiFi.disconnect();
Serial.println("");
return false;
}
delay(wificonfig.attemptdelay);
Serial.print(".");
attempts--;
}
}
if(stopble){
// Delete the task bleKeyboard had create to free memory and to not interfere with AsyncWebServer
bleKeyboard.end();
// Stop BLE from interfering with our WIFI signal
btStop();
esp_bt_controller_disable();
esp_bt_controller_deinit();
esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
Serial.println("");
Serial.println("[INFO]: BLE Stopped");
// Set pageNum to 7 so no buttons are displayed and touches are ignored
pageNum = 7;
}
Serial.print("[INFO]: Connected! IP address: ");
Serial.println(WiFi.localIP());
if(startwebserver){
// Open port 80
MDNS.begin(wificonfig.hostname);
MDNS.addService("http", "tcp", 80);
// Start the webserver
webserver.begin();
Serial.println("[INFO]: Webserver started");
}
return true;
}
// Start as WiFi AP
void startWifiAP(){
WiFi.mode(WIFI_AP);
WiFi.softAP(wificonfig.ssid, wificonfig.password);
Serial.println("");
Serial.print("[INFO]: Access Point Started! IP address: ");
Serial.println(WiFi.softAPIP());
// Delete the task bleKeyboard had create to free memory and to not interfere with AsyncWebServer
bleKeyboard.end();
// Stop BLE from interfering with our WIFI signal
btStop();
esp_bt_controller_disable();
esp_bt_controller_deinit();
esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
Serial.println("");
Serial.println("[INFO]: BLE Stopped");
MDNS.begin(wificonfig.hostname);
MDNS.addService("http", "tcp", 80);
// Set pageNum to 7 so no buttons are displayed and touches are ignored
pageNum = 7;
// Start the webserver
webserver.begin();
Serial.println("[INFO]: Webserver started");
}
// Start the default AP
void startDefaultAP(){
const char* ssid = "FreeTouchDeck";
const char* password = "defaultpass";
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
Serial.print("[INFO]: Access Point Started! IP address: ");
Serial.println(WiFi.softAPIP());
// Delete the task bleKeyboard had create to free memory and to not interfere with AsyncWebServer
bleKeyboard.end();
// Stop BLE from interfering with our WIFI signal
btStop();
esp_bt_controller_disable();
esp_bt_controller_deinit();
esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
Serial.println("[INFO]: BLE Stopped");
MDNS.begin("freetouchdeck");
MDNS.addService("http", "tcp", 80);
// Set pageNum to 7 so no buttons are displayed and touches are ignored
pageNum = 7;
// Start the webserver
webserver.begin();
Serial.println("[INFO]: Webserver started");
}
/**
* @brief This function stops Bluetooth and connects to the given
WiFi network. It the starts mDNS and starts the Async
Webserver.
*
* @param none
*
* @return none
*
* @note none
*/
void configmode()
{
tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 0);
tft.setTextFont(2);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
Serial.println("[INFO]: Entering Config Mode");
tft.println("Connecting to Wifi...");
if (String(wificonfig.ssid) == "YOUR_WIFI_SSID" || String(wificonfig.password) == "YOUR_WIFI_PASSWORD") // Still default
{
tft.println("WiFi Config still set to default! Starting as AP.");
Serial.println("[WARNING]: WiFi Config still set to default! Configurator started as AP.");
startDefaultAP();
tft.println("Started as AP because WiFi settings are still set to default.");
tft.println("To configure, connect to 'FreeTouchDeck' with password 'defaultpass'");
tft.println("Then go to http://freetouchdeck.local");
tft.print("The IP is: ");
tft.println(WiFi.softAPIP());
drawSingleButton(140, 180, 200, 80, generalconfig.menuButtonColour, TFT_WHITE, "Restart");
return;
}
if (String(wificonfig.ssid) == "FAILED" || String(wificonfig.password) == "FAILED" || String(wificonfig.wifimode) == "FAILED") // The wificonfig.json failed to load
{
tft.println("WiFi Config Failed to load! Starting as AP.");
Serial.println("[WARNING]: WiFi Config Failed to load! Configurator started as AP.");
startDefaultAP();
tft.println("Started as AP because WiFi settings failed to load.");
tft.println("To configure, connect to 'FreeTouchDeck' with password 'defaultpass'");
tft.println("Then go to http://freetouchdeck.local");
tft.print("The IP is: ");
tft.println(WiFi.softAPIP());
drawSingleButton(140, 180, 200, 80, generalconfig.menuButtonColour, TFT_WHITE, "Restart");
return;
}
if (strcmp(wificonfig.wifimode, "WIFI_STA") == 0)
{
if(!startWifiStation(true, true)){
startDefaultAP();
Serial.println("[WARNING]: Could not connect to AP, so started as AP.");
tft.println("Started as AP because WiFi connection failed.");
tft.println("To configure, connect to 'FreeTouchDeck' with password 'defaultpass'");
tft.println("Then go to http://freetouchdeck.local");
tft.print("The IP is: ");
tft.println(WiFi.softAPIP());
drawSingleButton(140, 180, 200, 80, generalconfig.menuButtonColour, TFT_WHITE, "Restart");
}
else
{
tft.println("Started as STA and in config mode.");
tft.println("To configure:");
tft.println("http://freetouchdeck.local");
tft.print("The IP is: ");
tft.println(WiFi.localIP());
drawSingleButton(140, 180, 200, 80, generalconfig.menuButtonColour, TFT_WHITE, "Restart");
}
}
else if (strcmp(wificonfig.wifimode, "WIFI_AP") == 0)
{
startWifiAP();
tft.println("Started as AP and in config mode.");
tft.println("To configure:");
tft.println("http://freetouchdeck.local");
tft.print("The IP is: ");
tft.println(WiFi.softAPIP());
drawSingleButton(140, 180, 200, 80, generalconfig.menuButtonColour, TFT_WHITE, "Restart");
}
}
/**
* @brief This function allows for saving (updating) the WiFi SSID
*
* @param String ssid
*
* @return boolean True if succeeded. False otherwise.
*
* @note Returns true if successful. To enable the new set SSID, you must reload the the
configuration using loadMainConfig()
*/
bool saveWifiSSID(String ssid)
{
FILESYSTEM.remove("/config/wificonfig.json");
File file = FILESYSTEM.open("/config/wificonfig.json", "w");
DynamicJsonDocument doc(384);
JsonObject wificonfigobject = doc.to<JsonObject>();
wificonfigobject["ssid"] = ssid;
wificonfigobject["password"] = wificonfig.password;
wificonfigobject["wifimode"] = wificonfig.wifimode;
wificonfigobject["wifihostname"] = wificonfig.hostname;
wificonfigobject["attempts"] = wificonfig.attempts;
wificonfigobject["attemptdelay"] = wificonfig.attemptdelay;
if (serializeJsonPretty(doc, file) == 0)
{
Serial.println("[WARNING]: Failed to write to file");
return false;
}
file.close();
return true;
}
/**
* @brief This function allows for saving (updating) the WiFi Password
*
* @param String password
*
* @return boolean True if succeeded. False otherwise.
*
* @note Returns true if successful. To enable the new set password, you must reload the the
configuration using loadMainConfig()
*/
bool saveWifiPW(String password)
{
FILESYSTEM.remove("/config/wificonfig.json");
File file = FILESYSTEM.open("/config/wificonfig.json", "w");
DynamicJsonDocument doc(384);
JsonObject wificonfigobject = doc.to<JsonObject>();
wificonfigobject["ssid"] = wificonfig.ssid;
wificonfigobject["password"] = password;
wificonfigobject["wifimode"] = wificonfig.wifimode;
wificonfigobject["wifihostname"] = wificonfig.hostname;
wificonfigobject["attempts"] = wificonfig.attempts;
wificonfigobject["attemptdelay"] = wificonfig.attemptdelay;
if (serializeJsonPretty(doc, file) == 0)
{
Serial.println("[WARNING]: Failed to write to file");
return false;
}
file.close();
return true;
}
/**
* @brief This function allows for saving (updating) the WiFi Mode
*
* @param String wifimode "WIFI_STA" of "WIFI_AP"
*
* @return boolean True if succeeded. False otherwise.
*
* @note Returns true if successful. To enable the new set WiFi Mode, you must reload the the
configuration using loadMainConfig()
*/
bool saveWifiMode(String wifimode)
{
if (wifimode != "WIFI_STA" && wifimode != "WIFI_AP")
{
Serial.println("[WARNING]: WiFi Mode not supported. Try WIFI_STA or WIFI_AP.");
return false;
}
FILESYSTEM.remove("/config/wificonfig.json");
File file = FILESYSTEM.open("/config/wificonfig.json", "w");
DynamicJsonDocument doc(384);
JsonObject wificonfigobject = doc.to<JsonObject>();
wificonfigobject["ssid"] = wificonfig.ssid;
wificonfigobject["password"] = wificonfig.password;
wificonfigobject["wifimode"] = wifimode;
wificonfigobject["wifihostname"] = wificonfig.hostname;
wificonfigobject["attempts"] = wificonfig.attempts;
wificonfigobject["attemptdelay"] = wificonfig.attemptdelay;
if (serializeJsonPretty(doc, file) == 0)
{
Serial.println("[WARNING]: Failed to write to file");
return false;
}
file.close();
return true;
}
/**
* @brief This function checks if a file exists and returns a boolean accordingly.
It then prints a debug message to the serial as wel as the tft.
*
* @param filename (const char *)
*
* @return boolean True if succeeded. False otherwise.
*
* @note Pass the filename including a leading /
*/
bool checkfile(const char *filename)
{
if (!FILESYSTEM.exists(filename))
{
tft.fillScreen(TFT_BLACK);
tft.setCursor(1, 3);
tft.setTextFont(2);
tft.setTextSize(2);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.printf("%s not found!\n\n", filename);
tft.setTextSize(1);
tft.printf("If this has happend after confguration, the data on the ESP may \nbe corrupted.");
return false;
}
else
{
return true;
}
}
bool resetconfig(String file){
if (file != "menu1" && file != "menu2" && file != "menu3" && file != "menu4" && file != "menu5" && file != "homescreen" && file != "general")
{
Serial.println("[WARNING]: Invalid reset option. Choose: menu1, menu2, menu3, menu4, menu5, homescreen, or general");
return false;
}
if (file == "menu1" || file == "menu2" || file == "menu3" || file == "menu4" || file == "menu5")
{
// Reset a menu config
// Delete the corrupted json file
String filetoremove = "/config/" + file;
if(!filetoremove.endsWith(".json")){
filetoremove = filetoremove + ".json";
}
FILESYSTEM.remove(filetoremove);
// Copy default.json to the new config file
File defaultfile = FILESYSTEM.open("/config/default.json", "r");
size_t n;
uint8_t buf[64];
if (defaultfile) {
File newfile = FILESYSTEM.open(filetoremove, "w");
if (newfile) {
while ((n = defaultfile.read(buf, sizeof(buf))) > 0) {
newfile.write(buf, n);
}
// Close the newly created file
newfile.close();
}
Serial.println("[INFO]: Done resetting.");
Serial.println("[INFO]: Type \"restart\" to reload configuration.");
// Close the default.json file
defaultfile.close();
return true;
}
}
else if(file == "homescreen")
{
// Reset the homescreen
// For this we do not need to open a default file because we can easily write it ourselfs
String filetoremove = "/config/" + file;
if(!filetoremove.endsWith(".json")){
filetoremove = filetoremove + ".json";
}
FILESYSTEM.remove(filetoremove);
File newfile = FILESYSTEM.open(filetoremove, "w");
newfile.println("{");
newfile.println("\"logo0\": \"question.bmp\",");
newfile.println("\"logo1\": \"question.bmp\",");
newfile.println("\"logo2\": \"question.bmp\",");
newfile.println("\"logo3\": \"question.bmp\",");
newfile.println("\"logo4\": \"question.bmp\",");
newfile.println("\"logo5\": \"settings.bmp\"");
newfile.println("}");
newfile.close();
Serial.println("[INFO]: Done resetting homescreen.");
Serial.println("[INFO]: Type \"restart\" to reload configuration.");
return true;
}
else if(file == "general")
{
// Reset the general config
// For this we do not need to open a default file because we can easily write it ourselfs
String filetoremove = "/config/" + file;
if(!filetoremove.endsWith(".json")){
filetoremove = filetoremove + ".json";
}
FILESYSTEM.remove(filetoremove);
File newfile = FILESYSTEM.open(filetoremove, "w");
newfile.println("{");
newfile.println("\"menubuttoncolor\": \"#009bf4\",");
newfile.println("\"functionbuttoncolor\": \"#00efcb\",");
newfile.println("\"latchcolor\": \"#fe0149\",");
newfile.println("\"background\": \"#000000\",");
newfile.println("\"sleepenable\": true,");
newfile.println("\"sleeptimer\": 10,");
newfile.println("\"beep\": true,");
newfile.println("\"modifier1\": 130,");
newfile.println("\"modifier2\": 129,");
newfile.println("\"modifier3\": 0,");
newfile.println("\"helperdelay\": 500");
newfile.println("}");
newfile.close();
Serial.println("[INFO]: Done resetting general config.");
Serial.println("[INFO]: Type \"restart\" to reload configuration.");
return true;
}
else
{
return false;
}
return false;
}