-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwifimanager.cpp
1178 lines (1050 loc) · 37.7 KB
/
wifimanager.cpp
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Wifi Manager
* (c) 2022-2024 Martin Verges
*
* Licensed under CC BY-NC-SA 4.0
* (Attribution-NonCommercial-ShareAlike 4.0 International)
**/
#include "wifimanager.h"
#include "AsyncJson.h"
#include "ArduinoJson.h"
#if ASYNC_WEBSERVER == true
#include <ESPAsyncWebServer.h>
#else
#include <WebServer.h>
#endif
#include <WiFi.h>
#include <Preferences.h>
/**
* @brief Write a message to the Serial interface
* @param msg The message to be written
*
* This function is a simple wrapper around Serial.print() to write a message
* to the serial console. It can be overwritten by a custom implementation for
* enhanced logging.
*/
void WIFIMANAGER::logMessage(String msg) {
Serial.print(msg);
}
/**
* @brief Background Task running as a loop forever
* @param param needs to be a valid WIFIMANAGER instance
*/
void wifiTask(void* param) {
yield();
delay(500); // wait a short time until everything is setup before executing the loop forever
yield();
const TickType_t xDelay = 10000 / portTICK_PERIOD_MS;
WIFIMANAGER * wifimanager = (WIFIMANAGER *) param;
for(;;) {
yield();
wifimanager->loop();
yield();
vTaskDelay(xDelay);
}
}
/**
* @brief Start the background task, which will take care of the Wifi connection
*
* This method will load the configuration from NVS, try to connect to the configured WIFI(s)
* and then start a background task, which will keep monitoring and trying to reconnect
* to the configured WIFI(s) in case the connection drops.
*/
void WIFIMANAGER::startBackgroundTask(String softApName, String softApPass) {
if (softApName.length()) this->softApName = softApName;
if (softApPass.length()) this->softApPass = softApPass;
loadFromNVS();
tryConnect();
BaseType_t taskCreated = xTaskCreatePinnedToCore(
wifiTask,
"WifiManager",
4096, // Stack size in words
this, // Task input parameter
1, // Priority of the task
&WifiCheckTask, // Task handle.
0 // Core where the task should run
);
if (taskCreated != pdPASS) {
logMessage("[ERROR] WifiManager: Error creating background task\n");
}
}
/**
* @brief Construct a new WIFIMANAGER::WIFIMANAGER object
* @details Puts the Wifi mode to AP+STA and registers Wifi Events
* @param ns Namespace for the preferences non volatile storage (NVS)
*/
WIFIMANAGER::WIFIMANAGER(const char * ns) {
NVS = (char *)ns;
// AP on/off
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info) {
logMessage("[WIFI] onEvent() AP mode started!\n");
softApRunning = true;
#if ESP_ARDUINO_VERSION_MAJOR >= 2
}, ARDUINO_EVENT_WIFI_AP_START); // arduino-esp32 2.0.0 and later
#else
}, SYSTEM_EVENT_AP_START); // arduino-esp32 1.0.6
#endif
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info) {
logMessage("[WIFI] onEvent() AP mode stopped!\n");
softApRunning = false;
#if ESP_ARDUINO_VERSION_MAJOR >= 2
}, ARDUINO_EVENT_WIFI_AP_STOP); // arduino-esp32 2.0.0 and later
#else
}, SYSTEM_EVENT_AP_STOP); // arduino-esp32 1.0.6
#endif
// AP client join/leave
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info) {
logMessage("[WIFI] onEvent() new client connected to softAP!\n");
#if ESP_ARDUINO_VERSION_MAJOR >= 2
}, ARDUINO_EVENT_WIFI_AP_STACONNECTED); // arduino-esp32 2.0.0 and later
#else
}, SYSTEM_EVENT_AP_STACONNECTED); // arduino-esp32 1.0.6
#endif
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info) {
logMessage("[WIFI] onEvent() Client disconnected from softAP!\n");
#if ESP_ARDUINO_VERSION_MAJOR >= 2
}, ARDUINO_EVENT_WIFI_AP_STADISCONNECTED); // arduino-esp32 2.0.0 and later
#else
}, SYSTEM_EVENT_AP_STADISCONNECTED); // arduino-esp32 1.0.6
#endif
}
/**
* @brief Destroy the WIFIMANAGER::WIFIMANAGER object
* @details will stop the background task as well but not cleanup the AsyncWebserver
*/
WIFIMANAGER::~WIFIMANAGER() {
vTaskDelete(WifiCheckTask);
// FIXME: get rid of the registered Webserver AsyncCallbackWebHandlers
}
/**
* @brief If no WIFI is available, fallback to create an AP on the ESP32
* @param state boolean true (create AP) or false (don't create an AP)
*/
void WIFIMANAGER::fallbackToSoftAp(const bool state) {
createFallbackAP = state;
}
/**
* @brief Get the current configured fallback state
* @return true
* @return false
*/
bool WIFIMANAGER::getFallbackState() {
return createFallbackAP;
}
/**
* @brief Remove all entries from the current known and configured Wifi list
* @details This only affects memory, not the storage!
* @details If you wan't to persist this, you need to call writeToNVS()
*/
void WIFIMANAGER::clearApList() {
for(uint8_t i=0; i<WIFIMANAGER_MAX_APS; i++) {
apList[i].apName = "";
apList[i].apPass = "";
}
}
/**
* @brief Load last saved configuration from the NVS into the memory
* @return true on success
* @return false on error
*/
bool WIFIMANAGER::loadFromNVS() {
configuredSSIDs = 0;
if (preferences.begin(NVS, true)) {
clearApList();
char tmpKey[10] = { 0 };
for(uint8_t i=0; i<WIFIMANAGER_MAX_APS; i++) {
sprintf(tmpKey, "apName%d", i);
if (preferences.getType(tmpKey) == PT_STR) {
String apName = preferences.getString(tmpKey, "");
if (apName.length() > 0) {
sprintf(tmpKey, "apPass%d", i);
String apPass = preferences.getString(tmpKey);
logMessage(String("[WIFI] Load SSID '") + apName + "' to " + String(i+1) + ". slot.\n");
apList[i].apName = apName;
apList[i].apPass = apPass;
configuredSSIDs++;
}
}
}
preferences.end();
return true;
}
logMessage("[WIFI] Unable to load data from NVS, giving up...\n");
return false;
}
/**
* @brief Write the current in memory configuration to the non volatile storage
* @return true on success
* @return false on error with the NVS
*/
bool WIFIMANAGER::writeToNVS() {
if (!preferences.begin(NVS, false)) {
logMessage("[WIFI] Unable to write data to NVS, giving up...");
return false;
}
preferences.clear();
char tmpKey[10];
for(uint8_t i = 0; i < WIFIMANAGER_MAX_APS; i++) {
if (apList[i].apName.isEmpty()) continue;
snprintf(tmpKey, sizeof(tmpKey), "apName%d", i);
preferences.putString(tmpKey, apList[i].apName);
snprintf(tmpKey, sizeof(tmpKey), "apPass%d", i);
preferences.putString(tmpKey, apList[i].apPass);
}
preferences.end();
return true;
}
/**
* @brief Add a new WIFI SSID to the known credentials list
* @param apName Name of the SSID to connect to
* @param apPass Password (or empty) to connect to the SSID
* @param updateNVS Write the new entry directly to NVS
* @return true on success
* @return false on failure
*/
bool WIFIMANAGER::addWifi(String apName, String apPass, bool updateNVS) {
if(apName.length() < 1 || apName.length() > 31) {
logMessage("[WIFI] No SSID given or ssid too long");
return false;
}
if(apPass.length() > 63) {
logMessage("[WIFI] Passphrase too long");
return false;
}
for(uint8_t i=0; i<WIFIMANAGER_MAX_APS; i++) {
if (apList[i].apName == "") {
logMessage(String("[WIFI] Found unused slot Nr. ") + String(i) + " to store the new SSID '" + apName + "' credentials.\n");
apList[i].apName = apName;
apList[i].apPass = apPass;
configuredSSIDs++;
if (updateNVS) return writeToNVS();
else return true;
}
}
logMessage("[WIFI] No slot available to store SSID credentials");
return false; // max entries reached
}
/**
* @brief Drop a known SSID entry ID from the known list and write change to NVS
* @param apId ID of the SSID within the array
* @return true on success
* @return false on error
*/
bool WIFIMANAGER::delWifi(uint8_t apId) {
if (apId < WIFIMANAGER_MAX_APS) {
apList[apId].apName.clear();
apList[apId].apPass.clear();
return writeToNVS();
}
return false;
}
/**
* @brief Drop a known SSID name from the known list and write change to NVS
* @param apName SSID name
* @return true on success
* @return false on error
*/
bool WIFIMANAGER::delWifi(String apName) {
int num = 0;
for(uint8_t i=0; i<WIFIMANAGER_MAX_APS; i++) {
if (apList[i].apName == apName) {
if (delWifi(i)) num++;
}
}
return num > 0;
}
/**
* @brief Provides information about the current configuration state
* @details When at least 1 SSID is configured, the return value will be true, otherwise false
* @return true if one or more SSIDs stored
* @return false if no configuration is available
*/
bool WIFIMANAGER::configAvailable() {
return configuredSSIDs != 0;
}
/**
* @brief Provides the apList element id of the first configured slot
* @details It's used to speed up connection by getting the first available configuration
* @note only call this function when you have configuredSSIDs > 0, otherwise it will return 0 as well and fail!
* @return uint8_t apList element id
*/
uint8_t WIFIMANAGER::getApEntry() {
for(uint8_t i=0; i<WIFIMANAGER_MAX_APS; i++) {
if (apList[i].apName.length()) return i;
}
logMessage("[WIFI][ERROR] We did not find a valid entry!\n");
logMessage("[WIFI][ERROR] Make sure to not call this function if configuredSSIDs != 1.\n");
return 0;
}
/**
* @brief Background loop function running inside the task
* @details regulary check if the connection is up&running, try to reconnect or create a fallback AP
*/
void WIFIMANAGER::loop() {
if (millis() - lastWifiCheckMillis < intervalWifiCheckMillis) return;
lastWifiCheckMillis = millis();
if(WiFi.waitForConnectResult() == WL_CONNECTED) {
// Check if we are connected to a well known SSID
for(uint8_t i=0; i<WIFIMANAGER_MAX_APS; i++) {
if (WiFi.SSID() == apList[i].apName) {
logMessage(String("[WIFI][STATUS] Connected to known SSID: '") + WiFi.SSID() + "' with IP " + WiFi.localIP().toString() + "\n");
return;
}
}
// looks like we are connected to something else, strange!?
logMessage("[WIFI] We are connected to an unknown SSID ignoring. Connected to: " + WiFi.SSID() + "\n");
} else {
if (softApRunning) {
logMessage("[WIFI] Not trying to connect to a known SSID. SoftAP has " + String(WiFi.softAPgetStationNum()) + " clients connected!\n");
} else {
// let's try to connect to some WiFi in Range
if (!tryConnect()) {
if (createFallbackAP) runSoftAP();
else logMessage("[WIFI] Auto creation of SoftAP is disabled, no starting AP!\n");
}
}
}
if (softApRunning && millis() - startApTimeMillis > timeoutApMillis) {
if (WiFi.softAPgetStationNum() > 0) {
logMessage("[WIFI] SoftAP has " + String(WiFi.softAPgetStationNum()) + " clients connected!\n");
startApTimeMillis = millis(); // reset timeout as someone is connected
return;
}
logMessage("[WIFI] Running in AP mode but timeout reached. Closing AP!\n");
stopSoftAP();
delay(100);
}
}
/**
* @brief Try to connect to one of the configured SSIDs (if available).
* @details If more than 2 SSIDs configured, scan for available WIFIs and connect to the strongest
* @return true on success
* @return false on error or no configuration
*/
bool WIFIMANAGER::tryConnect() {
if (!configAvailable()) {
logMessage("[WIFI] No SSIDs configured in NVS, unable to connect\n");
if (createFallbackAP) runSoftAP();
return false;
}
if (softApRunning) {
logMessage("[WIFI] Not trying to connect. SoftAP has " + String(WiFi.softAPgetStationNum()) + " clients connected!\n");
return false;
}
int choosenAp = INT_MIN;
if (configuredSSIDs == 1) {
// only one configured SSID, skip scanning and try to connect to this specific one.
choosenAp = getApEntry();
} else {
WiFi.mode(WIFI_STA);
int8_t scanResult = WiFi.scanNetworks(false, true);
if(scanResult <= 0) {
logMessage("[WIFI] Unable to find WIFI networks in range to this device!\n");
return false;
}
logMessage(String("[WIFI] Found ") + String(scanResult) + " networks in range\n");
int choosenRssi = INT_MIN; // we want to select the strongest signal with the highest priority if we have multiple SSIDs available
for(int8_t x = 0; x < scanResult; ++x) {
String ssid;
uint8_t encryptionType;
int32_t rssi;
uint8_t* bssid;
int32_t channel;
WiFi.getNetworkInfo(x, ssid, encryptionType, rssi, bssid, channel);
for(uint8_t i=0; i<WIFIMANAGER_MAX_APS; i++) {
if (apList[i].apName.length() == 0 || apList[i].apName != ssid) continue;
if (rssi > choosenRssi) {
if(encryptionType == WIFI_AUTH_OPEN || apList[i].apPass.length() > 0) { // open wifi or we do know a password
choosenAp = i;
choosenRssi = rssi;
}
} // else lower wifi signal
}
}
WiFi.scanDelete();
}
if (choosenAp == INT_MIN) {
logMessage("[WIFI] Unable to find an SSID to connect to!\n");
return false;
} else {
logMessage(String("[WIFI] Trying to connect to SSID ") + apList[choosenAp].apName
+ " with password " + (apList[choosenAp].apPass.length() > 0 ? "'***'" : "''") + "\n"
);
WiFi.begin(apList[choosenAp].apName.c_str(), apList[choosenAp].apPass.c_str());
wl_status_t status = (wl_status_t)WiFi.waitForConnectResult(5000UL);
auto startTime = millis();
// wait for connection, fail, or timeout
while(status != WL_CONNECTED && status != WL_NO_SSID_AVAIL && status != WL_CONNECT_FAILED && (millis() - startTime) <= 10000) {
delay(10);
status = (wl_status_t)WiFi.waitForConnectResult(5000UL);
}
switch(status) {
case WL_IDLE_STATUS:
logMessage("[WIFI] Connecting failed (0): Idle\n");
break;
case WL_NO_SSID_AVAIL:
logMessage("[WIFI] Connecting failed (1): The AP can't be found\n");
break;
case WL_SCAN_COMPLETED:
logMessage("[WIFI] Connecting failed (2): Scan completed\n");
break;
case WL_CONNECTED: // 3
logMessage("[WIFI] Connection successful\n");
logMessage("[WIFI] SSID : " + WiFi.SSID() + "\n");
logMessage("[WIFI] IP : " + WiFi.localIP().toString() + "\n");
stopSoftAP();
return true;
break;
case WL_CONNECT_FAILED:
logMessage("[WIFI] Connecting failed (4): Unknown reason\n");
break;
case WL_CONNECTION_LOST:
logMessage("[WIFI] Connecting failed (5): Connection lost\n");
break;
case WL_DISCONNECTED:
logMessage("[WIFI] Connecting failed (6): Disconnected\n");
break;
case WL_NO_SHIELD:
logMessage("[WIFI] Connecting failed (7): No Wifi shield found\n");
break;
default:
logMessage("[WIFI] Connecting failed (" + String(status) + "): Unknown status code\n");
break;
}
}
return false;
}
void WIFIMANAGER::configueSoftAp(String apName, String apPass) {
this->softApName = apName;
this->softApPass = apPass;
}
/**
* @brief Start a SoftAP for direct client access
* @param apName name of the AP to create (default is ESP_XXXXXXXX)
* @return true on success
* @return false o error or if a SoftAP already runs
*/
bool WIFIMANAGER::runSoftAP(String apName, String apPass) {
if (apName.length()) this->softApName = apName;
if (apPass.length()) this->softApPass = apPass;
if (softApRunning) return true;
startApTimeMillis = millis();
if (this->softApName == "") this->softApName = "ESP_" + String((uint32_t)ESP.getEfuseMac());
logMessage("[WIFI] Starting configuration portal on AP SSID " + this->softApName + "\n");
WiFi.mode(WIFI_AP);
bool state = WiFi.softAP(this->softApName.c_str(), (this->softApPass.length() ? this->softApPass.c_str() : NULL));
if (state) {
IPAddress IP = WiFi.softAPIP();
logMessage("[WIFI] AP created. My IP is: " + String(IP) + "\n");
return true;
} else {
logMessage("[WIFI] Unable to create SoftAP!\n");
return false;
}
}
/**
* @brief Stop/Disconnect a current running SoftAP
*/
void WIFIMANAGER::stopSoftAP() {
WiFi.softAPdisconnect();
WiFi.mode(WIFI_STA);
}
/**
* @brief Stop/Disconnect a current wifi connection
*/
void WIFIMANAGER::stopClient() {
WiFi.disconnect();
}
/**
* @brief Stop/Disconnect all running wifi activies and optionally kill the background task as well
* @param killTask true to kill the background task to prevent reconnects
*/
void WIFIMANAGER::stopWifi(bool killTask) {
if (killTask) vTaskDelete(WifiCheckTask);
stopSoftAP();
stopClient();
}
/**
* @brief Attach the WebServer to the WifiManager to register the RESTful API
* @param srv WebServer object
*/
#if ASYNC_WEBSERVER == true
void WIFIMANAGER::attachWebServer(AsyncWebServer * srv) {
#else
void WIFIMANAGER::attachWebServer(WebServer * srv) {
#endif
webServer = srv; // store it in the class for later use
#if ASYNC_WEBSERVER == true
// not required
#else
// just for debugging
webServer->onNotFound([&]() {
String uri = WebServer::urlDecode(webServer->uri()); // required to read paths with blanks
// Dump debug data
String message;
message.reserve(100);
message = F("Error: File not found\n\nURI: ");
message += uri;
message += F("\nMethod: ");
message += (webServer->method() == HTTP_GET) ? "GET" : "POST";
message += F("\nArguments: ");
message += webServer->args();
message += '\n';
for (uint8_t i = 0; i < webServer->args(); i++) {
message += F(" NAME:");
message += webServer->argName(i);
message += F("\n VALUE:");
message += webServer->arg(i);
message += '\n';
}
message += "path=";
message += webServer->arg("path");
message += '\n';
logMessage(message);
});
#endif
#if ASYNC_WEBSERVER == true
webServer->on((apiPrefix + "/softap/start").c_str(), HTTP_POST, [&](AsyncWebServerRequest * request) {
request->send(200, "application/json", "{\"message\":\"Soft AP stopped\"}");
#else
webServer->on((apiPrefix + "/softap/start").c_str(), HTTP_POST, [&]() {
webServer->send(200, "application/json", "{\"message\":\"Soft AP stopped\"}");
#endif
yield();
delay(250);
runSoftAP();
});
#if ASYNC_WEBSERVER == true
webServer->on((apiPrefix + "/softap/stop").c_str(), HTTP_POST, [&](AsyncWebServerRequest * request) {
request->send(200, "application/json", "{\"message\":\"Soft AP stopped\"}");
#else
webServer->on((apiPrefix + "/softap/stop").c_str(), HTTP_POST, [&]() {
webServer->send(200, "application/json", "{\"message\":\"Soft AP stopped\"}");
#endif
yield();
delay(250); // It's likely that this message won't go trough, but we give it a short time
stopSoftAP();
});
#if ASYNC_WEBSERVER == true
webServer->on((apiPrefix + "/client/stop").c_str(), HTTP_POST, [&](AsyncWebServerRequest * request) {
request->send(200, "application/json", "{\"message\":\"Terminating current Wifi connection\"}");
#else
webServer->on((apiPrefix + "/client/stop").c_str(), HTTP_POST, [&]() {
webServer->send(200, "application/json", "{\"message\":\"Terminating current Wifi connection\"}");
#endif
yield();
delay(500); // It's likely that this message won't go trough, but we give it a short time
stopClient();
});
#if ASYNC_WEBSERVER == true
webServer->on((apiPrefix + "/add").c_str(), HTTP_POST, [&](AsyncWebServerRequest * request){}, NULL,
[&](AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total) {
JsonDocument jsonBuffer;
deserializeJson(jsonBuffer, (const char*)data);
auto resp = request;
#else
webServer->on((apiPrefix + "/add").c_str(), HTTP_POST, [&]() {
if (webServer->args() != 1) {
webServer->send(400, "application/json", "{\"message\":\"Bad Request. Only accepting one json body in request!\"}");
}
JsonDocument jsonBuffer;
deserializeJson(jsonBuffer, webServer->arg(0));
auto resp = webServer;
#endif
if (!jsonBuffer["apName"].is<String>() || !jsonBuffer["apPass"].is<String>()) {
resp->send(422, "application/json", "{\"message\":\"Invalid data\"}");
return;
}
if (!addWifi(jsonBuffer["apName"].as<String>(), jsonBuffer["apPass"].as<String>())) {
resp->send(500, "application/json", "{\"message\":\"Unable to process data\"}");
} else resp->send(200, "application/json", "{\"message\":\"New AP added\"}");
});
#if ASYNC_WEBSERVER == true
webServer->on((apiPrefix + "/id").c_str(), HTTP_DELETE, [&](AsyncWebServerRequest * request){}, NULL,
[&](AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total) {
JsonDocument jsonBuffer;
deserializeJson(jsonBuffer, (const char*)data);
auto resp = request;
#else
webServer->on((apiPrefix + "/id").c_str(), HTTP_DELETE, [&]() {
if (webServer->args() != 1) {
webServer->send(400, "application/json", "{\"message\":\"Bad Request. Only accepting one json body in request!\"}");
}
JsonDocument jsonBuffer;
deserializeJson(jsonBuffer, webServer->arg(0));
auto resp = webServer;
#endif
if (!jsonBuffer["id"].is<uint8_t>() || jsonBuffer["id"].as<uint8_t>() >= WIFIMANAGER_MAX_APS) {
resp->send(422, "application/json", "{\"message\":\"Invalid data\"}");
return;
}
if (!delWifi(jsonBuffer["id"].as<uint8_t>())) {
resp->send(500, "application/json", "{\"message\":\"Unable to delete entry\"}");
} else resp->send(200, "application/json", "{\"message\":\"AP deleted\"}");
});
#if ASYNC_WEBSERVER == true
webServer->on((apiPrefix + "/apName").c_str(), HTTP_DELETE, [&](AsyncWebServerRequest * request){}, NULL,
[&](AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total) {
JsonDocument jsonBuffer;
deserializeJson(jsonBuffer, (const char*)data);
auto resp = request;
#else
webServer->on((apiPrefix + "/apName").c_str(), HTTP_DELETE, [&]() {
if (webServer->args() != 1) {
webServer->send(400, "application/json", "{\"message\":\"Bad Request. Only accepting one json body in request!\"}");
}
JsonDocument jsonBuffer;
deserializeJson(jsonBuffer, webServer->arg(0));
auto resp = webServer;
#endif
if (!jsonBuffer["apName"].is<String>()) {
resp->send(422, "application/json", "{\"message\":\"Invalid data\"}");
return;
}
if (!delWifi(jsonBuffer["apName"].as<String>())) {
resp->send(500, "application/json", "{\"message\":\"Unable to delete entry\"}");
} else resp->send(200, "application/json", "{\"message\":\"AP deleted\"}");
});
#if ASYNC_WEBSERVER == true
webServer->on((apiPrefix + "/configlist").c_str(), HTTP_GET, [&](AsyncWebServerRequest *request) {
AsyncResponseStream *response = request->beginResponseStream("application/json");
#else
webServer->on((apiPrefix + "/configlist").c_str(), HTTP_GET, [&]() {
String buffer;
#endif
JsonDocument jsonDoc;
auto jsonArray = jsonDoc.to<JsonArray>();
for(uint8_t i=0; i<WIFIMANAGER_MAX_APS; i++) {
if (apList[i].apName.length() > 0) {
JsonObject wifiNet = jsonArray.createNestedObject();
wifiNet["id"] = i;
wifiNet["apName"] = apList[i].apName;
wifiNet["apPass"] = apList[i].apPass.length() > 0 ? true : false;
}
}
#if ASYNC_WEBSERVER == true
serializeJson(jsonArray, *response);
response->setCode(200);
response->setContentLength(measureJson(jsonDoc));
request->send(response);
#else
// Improve me: not that efficient without the stream response
serializeJson(jsonArray, buffer);
webServer->send(200, "application/json", (buffer.equals("null") ? "{}" : buffer));
#endif
});
#if ASYNC_WEBSERVER == true
webServer->on((apiPrefix + "/scan").c_str(), HTTP_GET, [&](AsyncWebServerRequest *request) {
AsyncResponseStream *response = request->beginResponseStream("application/json");
#else
webServer->on((apiPrefix + "/scan").c_str(), HTTP_GET, [&]() {
String buffer;
#endif
JsonDocument jsonDoc;
int scanResult;
String ssid;
uint8_t encryptionType;
int32_t rssi;
uint8_t* bssid;
int32_t channel;
scanResult = WiFi.scanComplete();
if (scanResult == WIFI_SCAN_FAILED) {
scanResult = WiFi.scanNetworks(true, true); // FIXME: scanNetworks is disconnecting clients!
jsonDoc["status"] = "scanning";
} else if (scanResult > 0) {
for (int8_t i = 0; i < scanResult; i++) {
WiFi.getNetworkInfo(i, ssid, encryptionType, rssi, bssid, channel);
JsonObject wifiNet = jsonDoc.createNestedObject();
wifiNet["ssid"] = ssid;
wifiNet["encryptionType"] = encryptionType;
wifiNet["rssi"] = rssi;
wifiNet["channel"] = channel;
yield();
}
WiFi.scanDelete();
}
#if ASYNC_WEBSERVER == true
serializeJson(jsonDoc, *response);
response->setCode(200);
response->setContentLength(measureJson(jsonDoc));
request->send(response);
#else
// Improve me: not that efficient without the stream response
serializeJson(jsonDoc, buffer);
webServer->send(200, "application/json", buffer);
#endif
});
#if ASYNC_WEBSERVER == true
webServer->on((apiPrefix + "/status").c_str(), HTTP_GET, [&](AsyncWebServerRequest *request) {
AsyncResponseStream *response = request->beginResponseStream("application/json");
#else
webServer->on((apiPrefix + "/status").c_str(), HTTP_GET, [&]() {
String buffer;
#endif
JsonDocument jsonDoc;
jsonDoc["ssid"] = WiFi.SSID();
jsonDoc["signalStrengh"] = WiFi.RSSI();
jsonDoc["ip"] = WiFi.localIP().toString();
jsonDoc["gw"] = WiFi.gatewayIP().toString();
jsonDoc["nm"] = WiFi.subnetMask().toString();
jsonDoc["hostname"] = WiFi.getHostname();
jsonDoc["chipModel"] = ESP.getChipModel();
jsonDoc["chipRevision"] = ESP.getChipRevision();
jsonDoc["chipCores"] = ESP.getChipCores();
jsonDoc["getHeapSize"] = ESP.getHeapSize();
jsonDoc["freeHeap"] = ESP.getFreeHeap();
#if ASYNC_WEBSERVER == true
serializeJson(jsonDoc, *response);
response->setCode(200);
response->setContentLength(measureJson(jsonDoc));
request->send(response);
#else
// Improve me: not that efficient without the stream response
serializeJson(jsonDoc, buffer);
webServer->send(200, "application/json", buffer);
#endif
});
}
/**
* @brief Attach the WebServer to the WifiManager to register the RESTful API
* @param srv WebServer object
*/
void WIFIMANAGER::attachUI() {
#if ASYNC_WEBSERVER == true
webServer->on((uiPrefix).c_str(), HTTP_GET, [](AsyncWebServerRequest* request) {
#else
webServer->on((uiPrefix).c_str(), HTTP_GET, [&]() {
#endif
String html = R"html(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ESP32 WiFi Manager</title>
<style>
:root {
--primary-color: #2563eb;
--bg-color: #f8fafc;
--card-bg: #ffffff;
--text-color: #1e293b;
--border-color: #e2e8f0;
}
body {
font-family: system-ui, -apple-system, sans-serif;
background: var(--bg-color);
color: var(--text-color);
margin: 0;
padding: 16px;
line-height: 1.5;
}
.container {
max-width: 600px;
margin: 0 auto;
}
.card {
background: var(--card-bg);
border-radius: 8px;
padding: 16px;
margin-bottom: 16px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
border: 1px solid var(--border-color);
}
h1, h2 {
margin: 0 0 16px 0;
color: var(--text-color);
}
.network-list {
list-style: none;
padding: 0;
margin: 0;
}
.network-item {
display: flex;
align-items: center;
padding: 12px;
border-bottom: 1px solid var(--border-color);
cursor: pointer;
transition: background-color 0.2s;
}
.network-item:last-child {
border-bottom: none;
}
.network-item:hover {
background-color: var(--bg-color);
}
.network-info {
flex-grow: 1;
}
.network-info div {
float: left;
width: 70%;
}
.network-info button {
float: right;
width: 30%;
}
.ssid {
font-weight: 500;
margin-bottom: 4px;
}
.signal {
font-size: 0.875rem;
color: #64748b;
}
button {
background: var(--primary-color);
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
font-size: 0.875rem;
transition: opacity 0.2s;
}
button:hover {
opacity: 0.9;
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.status {
padding: 8px;
border-radius: 4px;
margin-bottom: 16px;
display: none;
}
.status.error {
background: #fee2e2;
color: #991b1b;
display: block;
}
.status.success {
background: #dcfce7;
color: #166534;
display: block;
}
.status.info {
background: #e0f2fe;
color: #075985;
display: block;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
align-items: center;
justify-content: center;
}
.modal-content {
background: var(--card-bg);
padding: 24px;
border-radius: 8px;
width: 90%;
max-width: 400px;
}
input {
width: 100%;
padding: 8px;
margin: 8px 0 16px;
border: 1px solid var(--border-color);
border-radius: 4px;
box-sizing: border-box;
}
.button-group {
display: flex;
gap: 8px;
justify-content: flex-end;
}
.button-secondary {
background: var(--bg-color);
color: var(--text-color);
border: 1px solid var(--border-color);
}
.saved-networks {
margin-top: 8px;
padding-top: 8px;
border-top: 1px solid var(--border-color);
}
</style>
</head>
<body>
<div class="container">
<div class="card">
<h1>ESP32 WiFi Manager</h1>
<div id="status"></div>
<button onclick="scanNetworks()">Scan for Networks</button>
<button onclick="showConnectModal()">Manual Connect</button>
</div>
<div class="card">
<h2>Available Networks</h2>
<div id="networkList" class="network-list"></div>
</div>
<div class="card">
<h2>Saved Networks</h2>
<div id="savedNetworks" class="network-list"></div>
</div>
</div>
<div id="connectModal" class="modal">
<div class="modal-content">
<h2>Connect to Network</h2>
<form id="connectForm" onsubmit="connectToNetwork(event)">
<label for="apName">Network Name:</label>
<input type="text" id="apName" required>
<label for="apPass">Password:</label>
<input type="password" id="apPass" required>
<div class="button-group">