@@ -55,7 +55,8 @@ static unsigned long wifiDisplayTimer;
55
55
// DNS server for Captive Portal
56
56
static DNSServer dnsServer;
57
57
58
- static bool wifiRunning;
58
+ static bool wifiStationRunning;
59
+ static bool wifiApRunning;
59
60
60
61
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
61
62
// WiFi Routines
@@ -154,7 +155,8 @@ bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout)
154
155
if (startWiFiAP && WiFi.getMode () == WIFI_AP)
155
156
return (true ); // There is nothing needing to be changed
156
157
157
- wifiRunning = false ; // Mark it as offline while we mess about
158
+ wifiStationRunning = false ; // Mark it as offline while we mess about
159
+ wifiApRunning = false ; // Mark it as offline while we mess about
158
160
159
161
wifi_mode_t wifiMode = WIFI_OFF;
160
162
wifi_interface_t wifiInterface = WIFI_IF_STA;
@@ -248,8 +250,6 @@ bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout)
248
250
systemPrintf (" WiFi AP '%s' started with IP: " , softApSsid);
249
251
systemPrintln (WiFi.softAPIP ());
250
252
251
- networkMarkOnline (NETWORK_WIFI); // Regardless of WiFi STA success of failure, we have some network access
252
-
253
253
// Start DNS Server
254
254
if (dnsServer.start (53 , " *" , WiFi.softAPIP ()) == false )
255
255
{
@@ -262,12 +262,11 @@ bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout)
262
262
systemPrintln (" DNS Server started" );
263
263
}
264
264
265
+ wifiApRunning = true ;
266
+
265
267
// If we're only here to start the AP, then we're done
266
268
if (wifiMode == WIFI_AP)
267
- {
268
- wifiRunning = true ;
269
269
return true ;
270
- }
271
270
}
272
271
273
272
systemPrintln (" Connecting to WiFi... " );
@@ -288,7 +287,7 @@ bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout)
288
287
if (wifiStatus == WL_CONNECTED)
289
288
{
290
289
wifiResetTimeout (); // If we successfully connected then reset the throttling timeout
291
- wifiRunning = true ;
290
+ wifiStationRunning = true ;
292
291
return true ;
293
292
}
294
293
if (wifiStatus == WL_DISCONNECTED)
@@ -297,7 +296,7 @@ bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout)
297
296
{
298
297
systemPrintf (" WiFi failed to connect: error #%d - %s\r\n " , wifiStatus, wifiPrintState ((wl_status_t )wifiStatus));
299
298
}
300
- wifiRunning = false ;
299
+ wifiStationRunning = false ;
301
300
return false ;
302
301
}
303
302
@@ -306,10 +305,10 @@ bool wifiConnect(bool startWiFiStation, bool startWiFiAP, unsigned long timeout)
306
305
// ----------------------------------------
307
306
void wifiDisplayState ()
308
307
{
309
- systemPrintf (" WiFi: %s\r\n " , networkIsInterfaceOnline (NETWORK_WIFI) ? " Online" : " Offline" );
308
+ systemPrintf (" WiFi: %s\r\n " , networkInterfaceHasInternet (NETWORK_WIFI) ? " Online" : " Offline" );
310
309
systemPrintf (" MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\r\n " , wifiMACAddress[0 ], wifiMACAddress[1 ],
311
310
wifiMACAddress[2 ], wifiMACAddress[3 ], wifiMACAddress[4 ], wifiMACAddress[5 ]);
312
- if (networkIsInterfaceOnline (NETWORK_WIFI))
311
+ if (networkInterfaceHasInternet (NETWORK_WIFI))
313
312
{
314
313
// Get the DNS addresses
315
314
IPAddress dns1 = WiFi.STA .dnsIP (0 );
@@ -351,7 +350,7 @@ void wifiEvent(arduino_event_id_t event, arduino_event_info_t info)
351
350
if (WiFi.getMode () == WIFI_MODE_STA)
352
351
{
353
352
// Take the network offline if necessary
354
- if (networkIsInterfaceOnline (NETWORK_WIFI) && (event != ARDUINO_EVENT_WIFI_STA_GOT_IP) &&
353
+ if (networkInterfaceHasInternet (NETWORK_WIFI) && (event != ARDUINO_EVENT_WIFI_STA_GOT_IP) &&
355
354
(event != ARDUINO_EVENT_WIFI_STA_GOT_IP6))
356
355
{
357
356
if (settings.debugWifiState )
@@ -422,13 +421,13 @@ void wifiEvent(arduino_event_id_t event, arduino_event_info_t info)
422
421
ipAddress = WiFi.localIP ();
423
422
systemPrint (" WiFi STA Got IPv4: " );
424
423
systemPrintln (ipAddress);
425
- networkMarkOnline (NETWORK_WIFI);
424
+ networkMarkHasInternet (NETWORK_WIFI);
426
425
break ;
427
426
428
427
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
429
428
systemPrint (" WiFi STA Got IPv6: " );
430
429
systemPrintln (ipAddress);
431
- networkMarkOnline (NETWORK_WIFI);
430
+ networkMarkHasInternet (NETWORK_WIFI);
432
431
break ;
433
432
434
433
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
@@ -449,11 +448,29 @@ bool wifiIsConnected()
449
448
}
450
449
451
450
// ----------------------------------------
452
- // Determine if WIFI is running
451
+ // Determine if WiFi Station is running
452
+ // ----------------------------------------
453
+ bool wifiStationIsRunning ()
454
+ {
455
+ return wifiStationRunning;
456
+ }
457
+
458
+ // ----------------------------------------
459
+ // Determine if WiFi AP is running
460
+ // ----------------------------------------
461
+ bool wifiApIsRunning ()
462
+ {
463
+ return wifiApRunning;
464
+ }
465
+
466
+ // ----------------------------------------
467
+ // Determine if either Station or AP is running
453
468
// ----------------------------------------
454
469
bool wifiIsRunning ()
455
470
{
456
- return wifiRunning;
471
+ if (wifiStationRunning || wifiApRunning)
472
+ return true ;
473
+ return false ;
457
474
}
458
475
459
476
// ----------------------------------------
@@ -511,54 +528,59 @@ bool wifiStart()
511
528
{
512
529
int wifiStatus;
513
530
514
- // Determine if WiFi is already running
515
- if (!wifiRunning)
516
- {
517
- // Determine which parts of WiFi need to be started
518
- bool startWiFiStation = false ;
519
- bool startWiFiAP = false ;
531
+ // Determine which parts of WiFi need to be started
532
+ bool startWiFiStation = false ;
533
+ bool startWiFiAp = false ;
520
534
521
- uint16_t consumerTypes = networkGetConsumerTypes ();
535
+ uint16_t consumerTypes = networkGetConsumerTypes ();
522
536
523
- // The consumers need station
524
- if (consumerTypes & (1 << NETCONSUMER_WIFI_STA))
525
- startWiFiStation = true ;
537
+ // The consumers need station
538
+ if (consumerTypes & (1 << NETCONSUMER_WIFI_STA))
539
+ startWiFiStation = true ;
526
540
527
- // The consumers need AP
528
- if (consumerTypes & (1 << NETCONSUMER_WIFI_AP))
529
- startWiFiAP = true ;
541
+ // The consumers need AP
542
+ if (consumerTypes & (1 << NETCONSUMER_WIFI_AP))
543
+ startWiFiAp = true ;
530
544
531
- if (startWiFiStation == false && startWiFiAP == false )
545
+ if (startWiFiStation == false && startWiFiAp == false )
546
+ {
547
+ systemPrintln (" wifiStart() requested without any NETCONSUMER combination" );
548
+ WIFI_STOP ();
549
+ return (false );
550
+ }
551
+
552
+ // Determine if WiFi is already running
553
+ if (startWiFiStation == wifiStationRunning && startWiFiAp == wifiApRunning)
554
+ {
555
+ if (settings.debugWifiState == true )
556
+ systemPrintln (" WiFi is already running with requested setup" );
557
+ return (true );
558
+ }
559
+
560
+ // Handle special cases if no networks have been entered
561
+ if (wifiNetworkCount () == 0 )
562
+ {
563
+ if (startWiFiStation == true && startWiFiAp == false )
532
564
{
533
- systemPrintln (" wifiStart() requested without any NETCONSUMER combination" );
565
+ systemPrintln (" Error: Please enter at least one SSID before using WiFi" );
566
+ displayNoSSIDs (2000 );
534
567
WIFI_STOP ();
535
- return ( false ) ;
568
+ return false ;
536
569
}
537
-
538
- if (wifiNetworkCount () == 0 )
570
+ else if (startWiFiStation == true && startWiFiAp == true )
539
571
{
540
- if (startWiFiStation == true && startWiFiAP == false )
541
- {
542
- systemPrintln (" Error: Please enter at least one SSID before using WiFi" );
543
- displayNoSSIDs (2000 );
544
- WIFI_STOP ();
545
- return false ;
546
- }
547
- else if (startWiFiStation == true && startWiFiAP == true )
548
- {
549
- systemPrintln (" Error: No SSID available to start WiFi Station during AP" );
550
- // Allow the system to continue in AP only mode
551
- startWiFiStation = false ;
552
- }
572
+ systemPrintln (" Error: No SSID available to start WiFi Station during AP" );
573
+ // Allow the system to continue in AP only mode
574
+ startWiFiStation = false ;
553
575
}
554
-
555
- // Start WiFi
556
- wifiConnect (startWiFiStation, startWiFiAP, settings.wifiConnectTimeoutMs );
557
576
}
558
577
578
+ // Start WiFi
579
+ wifiConnect (startWiFiStation, startWiFiAp, settings.wifiConnectTimeoutMs );
580
+
559
581
// If we are in AP only mode, as long as the AP is started, return true
560
582
if (WiFi.getMode () == WIFI_MODE_AP)
561
- return (true );
583
+ return (wifiApIsRunning );
562
584
563
585
// If we are in STA or AP+STA mode, return if the station connected successfully
564
586
wifiStatus = WiFi.status ();
@@ -690,7 +712,8 @@ void wifiStop()
690
712
691
713
// Display the heap state
692
714
reportHeapNow (settings.debugWifiState );
693
- wifiRunning = false ;
715
+ wifiStationRunning = false ;
716
+ wifiApRunning = false ;
694
717
}
695
718
696
719
// Needed for wifiStopSequence
0 commit comments