17
17
#include < Preferences.h>
18
18
19
19
20
- void logMsg (String msg) {
20
+ /* *
21
+ * @brief Write a message to the Serial interface
22
+ * @param msg The message to be written
23
+ *
24
+ * This function is a simple wrapper around Serial.print() to write a message
25
+ * to the serial console. It can be overwritten by a custom implementation for
26
+ * enhanced logging.
27
+ */
28
+ void WIFIMANAGER::logMessage (String msg) {
21
29
Serial.print (msg);
22
30
}
23
31
@@ -64,7 +72,7 @@ void WIFIMANAGER::startBackgroundTask(String softApName, String softApPass) {
64
72
);
65
73
66
74
if (taskCreated != pdPASS) {
67
- Serial. println (" [ERROR] WifiManager: Error creating background task" );
75
+ logMessage (" [ERROR] WifiManager: Error creating background task\n " );
68
76
}
69
77
}
70
78
@@ -78,15 +86,15 @@ WIFIMANAGER::WIFIMANAGER(const char * ns) {
78
86
79
87
// AP on/off
80
88
WiFi.onEvent ([&](WiFiEvent_t event, WiFiEventInfo_t info) {
81
- Serial. println ( F ( " [WIFI] onEvent() AP mode started!" ) );
89
+ logMessage ( " [WIFI] onEvent() AP mode started!\n " );
82
90
softApRunning = true ;
83
91
#if ESP_ARDUINO_VERSION_MAJOR >= 2
84
92
}, ARDUINO_EVENT_WIFI_AP_START); // arduino-esp32 2.0.0 and later
85
93
#else
86
94
}, SYSTEM_EVENT_AP_START); // arduino-esp32 1.0.6
87
95
#endif
88
96
WiFi.onEvent ([&](WiFiEvent_t event, WiFiEventInfo_t info) {
89
- Serial. println ( F ( " [WIFI] onEvent() AP mode stopped!" ) );
97
+ logMessage ( " [WIFI] onEvent() AP mode stopped!\n " );
90
98
softApRunning = false ;
91
99
#if ESP_ARDUINO_VERSION_MAJOR >= 2
92
100
}, ARDUINO_EVENT_WIFI_AP_STOP); // arduino-esp32 2.0.0 and later
@@ -95,14 +103,14 @@ WIFIMANAGER::WIFIMANAGER(const char * ns) {
95
103
#endif
96
104
// AP client join/leave
97
105
WiFi.onEvent ([&](WiFiEvent_t event, WiFiEventInfo_t info) {
98
- Serial. println ( F ( " [WIFI] onEvent() New client connected to softAP!" ) );
106
+ logMessage ( " [WIFI] onEvent() new client connected to softAP!\n " );
99
107
#if ESP_ARDUINO_VERSION_MAJOR >= 2
100
108
}, ARDUINO_EVENT_WIFI_AP_STACONNECTED); // arduino-esp32 2.0.0 and later
101
109
#else
102
110
}, SYSTEM_EVENT_AP_STACONNECTED); // arduino-esp32 1.0.6
103
111
#endif
104
112
WiFi.onEvent ([&](WiFiEvent_t event, WiFiEventInfo_t info) {
105
- Serial. println ( F ( " [WIFI] onEvent() Client disconnected from softAP!" ) );
113
+ logMessage ( " [WIFI] onEvent() Client disconnected from softAP!\n " );
106
114
#if ESP_ARDUINO_VERSION_MAJOR >= 2
107
115
}, ARDUINO_EVENT_WIFI_AP_STADISCONNECTED); // arduino-esp32 2.0.0 and later
108
116
#else
@@ -165,7 +173,7 @@ bool WIFIMANAGER::loadFromNVS() {
165
173
if (apName.length () > 0 ) {
166
174
sprintf (tmpKey, " apPass%d" , i);
167
175
String apPass = preferences.getString (tmpKey);
168
- Serial. printf ( " [WIFI] Load SSID '%s ' to %d . slot.\n " , apName. c_str (), i+ 1 );
176
+ logMessage ( String ( " [WIFI] Load SSID '" ) + apName + " ' to " + String (i+ 1 ) + " . slot.\n " );
169
177
apList[i].apName = apName;
170
178
apList[i].apPass = apPass;
171
179
configuredSSIDs++;
@@ -175,7 +183,7 @@ bool WIFIMANAGER::loadFromNVS() {
175
183
preferences.end ();
176
184
return true ;
177
185
}
178
- Serial. println ( F ( " [WIFI] Unable to load data from NVS, giving up..." ) );
186
+ logMessage ( " [WIFI] Unable to load data from NVS, giving up...\n " );
179
187
return false ;
180
188
}
181
189
@@ -186,7 +194,7 @@ bool WIFIMANAGER::loadFromNVS() {
186
194
*/
187
195
bool WIFIMANAGER::writeToNVS () {
188
196
if (!preferences.begin (NVS, false )) {
189
- Serial. println ( F ( " [WIFI] Unable to write data to NVS, giving up..." ) );
197
+ logMessage ( " [WIFI] Unable to write data to NVS, giving up..." );
190
198
return false ;
191
199
}
192
200
@@ -216,26 +224,26 @@ bool WIFIMANAGER::writeToNVS() {
216
224
*/
217
225
bool WIFIMANAGER::addWifi (String apName, String apPass, bool updateNVS) {
218
226
if (apName.length () < 1 || apName.length () > 31 ) {
219
- Serial. println ( F ( " [WIFI] No SSID given or ssid too long" ) );
220
- return false ;
227
+ logMessage ( " [WIFI] No SSID given or ssid too long" );
228
+ return false ;
221
229
}
222
230
223
231
if (apPass.length () > 63 ) {
224
- Serial. println ( F ( " [WIFI] Passphrase too long" ) );
225
- return false ;
232
+ logMessage ( " [WIFI] Passphrase too long" );
233
+ return false ;
226
234
}
227
235
228
236
for (uint8_t i=0 ; i<WIFIMANAGER_MAX_APS; i++) {
229
237
if (apList[i].apName == " " ) {
230
- Serial. printf ( " [WIFI] Found unused slot Nr. %d to store the new SSID '%s ' credentials.\n " , i, apName. c_str () );
238
+ logMessage ( String ( " [WIFI] Found unused slot Nr. " ) + String (i) + " to store the new SSID '" + apName + " ' credentials.\n " );
231
239
apList[i].apName = apName;
232
240
apList[i].apPass = apPass;
233
241
configuredSSIDs++;
234
242
if (updateNVS) return writeToNVS ();
235
243
else return true ;
236
244
}
237
245
}
238
- Serial. println ( F ( " [WIFI] No slot available to store SSID credentials" ) );
246
+ logMessage ( " [WIFI] No slot available to store SSID credentials" );
239
247
return false ; // max entries reached
240
248
}
241
249
@@ -290,8 +298,8 @@ uint8_t WIFIMANAGER::getApEntry() {
290
298
for (uint8_t i=0 ; i<WIFIMANAGER_MAX_APS; i++) {
291
299
if (apList[i].apName .length ()) return i;
292
300
}
293
- Serial. print ( F ( " [WIFI][ERROR] We did not find a valid entry!" ) );
294
- Serial. print ( F ( " [WIFI][ERROR] Make sure to not call this function if configuredSSIDs != 1." ) );
301
+ logMessage ( " [WIFI][ERROR] We did not find a valid entry!\n " );
302
+ logMessage ( " [WIFI][ERROR] Make sure to not call this function if configuredSSIDs != 1.\n " );
295
303
return 0 ;
296
304
}
297
305
@@ -307,35 +315,31 @@ void WIFIMANAGER::loop() {
307
315
// Check if we are connected to a well known SSID
308
316
for (uint8_t i=0 ; i<WIFIMANAGER_MAX_APS; i++) {
309
317
if (WiFi.SSID () == apList[i].apName ) {
310
- Serial.printf (" [WIFI][STATUS] Connected to known SSID: '%s' with IP %s.\n " ,
311
- WiFi.SSID ().c_str (),
312
- WiFi.localIP ().toString ().c_str ()
313
- );
318
+ logMessage (String (" [WIFI][STATUS] Connected to known SSID: '" ) + WiFi.SSID () + " ' with IP " + WiFi.localIP ().toString () + " \n " );
314
319
return ;
315
320
}
316
321
}
317
322
// looks like we are connected to something else, strange!?
318
- Serial.print (F (" [WIFI] We are connected to an unknown SSID ignoring. Connected to: " ));
319
- Serial.println (WiFi.SSID ());
323
+ logMessage (" [WIFI] We are connected to an unknown SSID ignoring. Connected to: " + WiFi.SSID () + " \n " );
320
324
} else {
321
325
if (softApRunning) {
322
- Serial. printf (" [WIFI] Not trying to connect to a known SSID. SoftAP has %d clients connected!\n " , WiFi. softAPgetStationNum () );
326
+ logMessage (" [WIFI] Not trying to connect to a known SSID. SoftAP has " + String (WiFi. softAPgetStationNum ()) + " clients connected!\n " );
323
327
} else {
324
328
// let's try to connect to some WiFi in Range
325
329
if (!tryConnect ()) {
326
330
if (createFallbackAP) runSoftAP ();
327
- else Serial. println ( F ( " [WIFI] Auto creation of SoftAP is disabled, no starting AP!" ) );
331
+ else logMessage ( " [WIFI] Auto creation of SoftAP is disabled, no starting AP!\n " );
328
332
}
329
333
}
330
334
}
331
335
332
336
if (softApRunning && millis () - startApTimeMillis > timeoutApMillis) {
333
337
if (WiFi.softAPgetStationNum () > 0 ) {
334
- Serial. printf (" [WIFI] SoftAP has %d clients connected!\n " , WiFi. softAPgetStationNum () );
338
+ logMessage (" [WIFI] SoftAP has " + String (WiFi. softAPgetStationNum ()) + " clients connected!\n " );
335
339
startApTimeMillis = millis (); // reset timeout as someone is connected
336
340
return ;
337
341
}
338
- Serial. println ( F ( " [WIFI] Running in AP mode but timeout reached. Closing AP!" ) );
342
+ logMessage ( " [WIFI] Running in AP mode but timeout reached. Closing AP!\n " );
339
343
stopSoftAP ();
340
344
delay (100 );
341
345
}
@@ -349,13 +353,13 @@ void WIFIMANAGER::loop() {
349
353
*/
350
354
bool WIFIMANAGER::tryConnect () {
351
355
if (!configAvailable ()) {
352
- Serial. println ( F ( " [WIFI] No SSIDs configured in NVS, unable to connect. " ) );
356
+ logMessage ( " [WIFI] No SSIDs configured in NVS, unable to connect\n " );
353
357
if (createFallbackAP) runSoftAP ();
354
358
return false ;
355
359
}
356
360
357
361
if (softApRunning) {
358
- Serial. printf (" [WIFI] Not trying to connect. SoftAP has %d clients connected!\n " , WiFi. softAPgetStationNum () );
362
+ logMessage (" [WIFI] Not trying to connect. SoftAP has " + String (WiFi. softAPgetStationNum ()) + " clients connected!\n " );
359
363
return false ;
360
364
}
361
365
@@ -367,11 +371,10 @@ bool WIFIMANAGER::tryConnect() {
367
371
WiFi.mode (WIFI_STA);
368
372
int8_t scanResult = WiFi.scanNetworks (false , true );
369
373
if (scanResult <= 0 ) {
370
- Serial. println ( F ( " [WIFI] Unable to find WIFI networks in range to this device!" ) );
374
+ logMessage ( " [WIFI] Unable to find WIFI networks in range to this device!\n " );
371
375
return false ;
372
376
}
373
- Serial.print (F (" [WIFI] Found networks: " ));
374
- Serial.println (scanResult);
377
+ logMessage (String (" [WIFI] Found " ) + String (scanResult) + " networks in range\n " );
375
378
int choosenRssi = INT_MIN; // we want to select the strongest signal with the highest priority if we have multiple SSIDs available
376
379
for (int8_t x = 0 ; x < scanResult; ++x) {
377
380
String ssid;
@@ -395,12 +398,11 @@ bool WIFIMANAGER::tryConnect() {
395
398
}
396
399
397
400
if (choosenAp == INT_MIN) {
398
- Serial. println ( F ( " [WIFI] Unable to find an SSID to connect to!" ) );
401
+ logMessage ( " [WIFI] Unable to find an SSID to connect to!\n " );
399
402
return false ;
400
403
} else {
401
- Serial.printf (" [WIFI] Trying to connect to SSID %s with password %s.\n " ,
402
- apList[choosenAp].apName .c_str (),
403
- (apList[choosenAp].apPass .length () > 0 ? " '***'" : " ''" )
404
+ logMessage (String (" [WIFI] Trying to connect to SSID " ) + apList[choosenAp].apName
405
+ + " with password " + (apList[choosenAp].apPass .length () > 0 ? " '***'" : " ''" ) + " \n "
404
406
);
405
407
406
408
WiFi.begin (apList[choosenAp].apName .c_str (), apList[choosenAp].apPass .c_str ());
@@ -414,36 +416,35 @@ bool WIFIMANAGER::tryConnect() {
414
416
}
415
417
switch (status) {
416
418
case WL_IDLE_STATUS:
417
- Serial. println ( F ( " [WIFI] Connecting failed (0): Idle" ) );
419
+ logMessage ( " [WIFI] Connecting failed (0): Idle\n " );
418
420
break ;
419
421
case WL_NO_SSID_AVAIL:
420
- Serial. println ( F ( " [WIFI] Connection failed (1): The AP can't be found. " ) );
422
+ logMessage ( " [WIFI] Connecting failed (1): The AP can't be found\n " );
421
423
break ;
422
424
case WL_SCAN_COMPLETED:
423
- Serial. println ( F ( " [WIFI] Connecting failed (2): Scan completed" ) );
425
+ logMessage ( " [WIFI] Connecting failed (2): Scan completed\n " );
424
426
break ;
425
427
case WL_CONNECTED: // 3
426
- Serial.println (F (" [WIFI] Connection successful." ));
427
- Serial.printf (" [WIFI] SSID : %s\n " , WiFi.SSID ().c_str ());
428
- Serial.printf (" [WIFI] IP : %s\n " , WiFi.localIP ().toString ().c_str ());
429
-
428
+ logMessage (" [WIFI] Connection successful\n " );
429
+ logMessage (" [WIFI] SSID : " + WiFi.SSID () + " \n " );
430
+ logMessage (" [WIFI] IP : " + WiFi.localIP ().toString () + " \n " );
430
431
stopSoftAP ();
431
432
return true ;
432
433
break ;
433
434
case WL_CONNECT_FAILED:
434
- Serial. println ( F ( " [WIFI] Connecting failed (4): Unknown reason" ) );
435
+ logMessage ( " [WIFI] Connecting failed (4): Unknown reason\n " );
435
436
break ;
436
437
case WL_CONNECTION_LOST:
437
- Serial. println ( F ( " [WIFI] Connecting failed (5): Connection lost" ) );
438
+ logMessage ( " [WIFI] Connecting failed (5): Connection lost\n " );
438
439
break ;
439
440
case WL_DISCONNECTED:
440
- Serial. println ( F ( " [WIFI] Connecting failed (6): Disconnected" ) );
441
+ logMessage ( " [WIFI] Connecting failed (6): Disconnected\n " );
441
442
break ;
442
443
case WL_NO_SHIELD:
443
- Serial. println ( F ( " [WIFI] Connecting failed (255 ): No Wifi shield found" ) );
444
+ logMessage ( " [WIFI] Connecting failed (7 ): No Wifi shield found\n " );
444
445
break ;
445
446
default :
446
- Serial. printf (" [WIFI] Connecting Failed (Status: %d). \n " , status);
447
+ logMessage (" [WIFI] Connecting failed ( " + String (status) + " ): Unknown status code \n " );
447
448
break ;
448
449
}
449
450
}
@@ -469,17 +470,16 @@ bool WIFIMANAGER::runSoftAP(String apName, String apPass) {
469
470
startApTimeMillis = millis ();
470
471
471
472
if (this ->softApName == " " ) this ->softApName = " ESP_" + String ((uint32_t )ESP.getEfuseMac ());
472
- Serial. printf (" [WIFI] Starting configuration portal on AP SSID %s \n " , this ->softApName . c_str () );
473
+ logMessage (" [WIFI] Starting configuration portal on AP SSID " + this ->softApName + " \n " );
473
474
474
475
WiFi.mode (WIFI_AP);
475
476
bool state = WiFi.softAP (this ->softApName .c_str (), (this ->softApPass .length () ? this ->softApPass .c_str () : NULL ));
476
477
if (state) {
477
478
IPAddress IP = WiFi.softAPIP ();
478
- Serial.print (F (" [WIFI] AP created. My IP is: " ));
479
- Serial.println (IP);
479
+ logMessage (" [WIFI] AP created. My IP is: " + String (IP) + " \n " );
480
480
return true ;
481
481
} else {
482
- Serial. println ( F ( " [WIFI] Unable to create soft AP! " ) );
482
+ logMessage ( " [WIFI] Unable to create SoftAP! \n " );
483
483
return false ;
484
484
}
485
485
}
@@ -547,7 +547,7 @@ void WIFIMANAGER::attachWebServer(WebServer * srv) {
547
547
message += " path=" ;
548
548
message += webServer->arg (" path" );
549
549
message += ' \n ' ;
550
- Serial. print (message);
550
+ logMessage (message);
551
551
});
552
552
#endif
553
553
0 commit comments