@@ -45,6 +45,7 @@ ulong bootTime = millis();
4545ulong statusLastPublish = millis();
4646bool delayedStart = true ; // Delay spa connection for 10sec after boot to allow for external debugging if required.
4747bool autoDiscoveryPublished = false ;
48+ bool wifiRestoredFlag = true ; // Flag to indicate if Wi-Fi has been restored after a disconnect.
4849
4950String mqttBase = " " ;
5051String mqttStatusTopic = " " ;
@@ -57,6 +58,8 @@ String spaSerialNumber = "";
5758// / @brief Flag to indicate that the mqtt configuration has changed and therefore the MQTT
5859// / client needs to be restarted.
5960bool updateMqtt = false ;
61+ // / @brief Flag to indicate that the Wi-Fi configuration has changed and therefore the Wi-Fi
62+ bool updateSoftAP = false ;
6063bool setSpaCallbackReady = false ;
6164String spaCallbackProperty;
6265String spaCallbackValue;
@@ -136,11 +139,17 @@ void configChangeCallbackString(const char* name, String value) {
136139 else if (strcmp (name, " MqttPassword" ) == 0 ) updateMqtt = true ;
137140 else if (strcmp (name, " SpaName" ) == 0 ) { } // TODO - Changing the SpaName currently requires the user to:
138141 // delete the entities in MQTT then reboot the ESP
142+ else if (strcmp (name, " SoftAPPassword" ) == 0 ) updateSoftAP = true ;
139143}
140144
141145void configChangeCallbackInt (const char * name, int value) {
142146 debugD (" %s: %i" , name, value);
143- if (strcmp (name, " UpdateFrequency" ) == 0 ) si.setUpdateFrequency (value);
147+ if (strcmp (name, " SpaPollFrequency" ) == 0 ) si.setSpaPollFrequency (value);
148+ }
149+
150+ void configChangeCallbackBool (const char * name, bool value) {
151+ debugD (" %s: %s" , name, value ? " true" : " false" );
152+ if (strcmp (name, " SoftAPAlwaysOn" ) == 0 ) updateSoftAP = true ;
144153}
145154
146155void mqttHaAutoDiscovery () {
@@ -570,6 +579,23 @@ String sanitizeHostname(const String& input) {
570579 return sanitized;
571580}
572581
582+ void wifiRestored () {
583+ debugI (" Wi-Fi connection restored" );
584+ wifiRestoredFlag = true ;
585+
586+ if (!config.SoftAPAlwaysOn .getValue ()) {
587+ WiFi.softAPdisconnect (true ); // Disable AP mode if reconnected
588+ WiFi.mode (WIFI_STA );
589+ }
590+ MDNS .end (); // Stop mDNS responder (if it was running)
591+ if (!MDNS .begin (WiFi.getHostname ())) {
592+ debugE (" Failed to start mDNS responder" );
593+ } else {
594+ debugI (" mDNS responder restarted" );
595+ }
596+
597+ }
598+
573599#pragma endregion
574600
575601void setup () {
@@ -600,9 +626,31 @@ void setup() {
600626 blinker.setState (STATE_WIFI_NOT_CONNECTED );
601627 WiFi.setHostname (sanitizeHostname (config.SpaName .getValue ()).c_str ());
602628
603- WiFi.mode (WIFI_AP_STA );
629+ if (config.SoftAPAlwaysOn .getValue ()) {
630+ WiFi.mode (WIFI_AP_STA );
631+ WiFi.softAP (WiFi.getHostname (), config.SoftAPPassword .getValue ().c_str ());
632+ } else {
633+ WiFi.mode (WIFI_STA );
634+ }
635+
636+ // WiFi.begin(config.WiFiSSID.getValue().c_str(), config.WiFiPassword.getValue().c_str());
604637 WiFi.begin ();
605- WiFi.softAP (WiFi.getHostname (), " eSPA-Password" ); // Start the AP with the hostname and a default password
638+ if (WiFi.waitForConnectResult () == WL_CONNECTED ) {
639+ debugI (" Connected to Wi-Fi as %s" , WiFi.getHostname ());
640+ int totalTry = 5 ;
641+ while (!MDNS .begin (WiFi.getHostname ()) && totalTry > 0 ) {
642+ debugW (" ." );
643+ delay (1000 );
644+ totalTry--;
645+ }
646+ debugA (" mDNS responder started" );
647+ } else {
648+ debugW (" Failed to connect to Wi-Fi, starting AP mode" );
649+ if (!config.SoftAPAlwaysOn .getValue ()) {
650+ WiFi.mode (WIFI_AP_STA );
651+ WiFi.softAP (WiFi.getHostname (), config.SoftAPPassword .getValue ().c_str ());
652+ }
653+ }
606654
607655 Debug.begin (WiFi.getHostname ()); // Hostname seems to be for display purposes only, no functional impact.
608656 Debug.setResetCmdEnabled (true ); // This seems to be not needed to be in Setup.
@@ -618,10 +666,11 @@ void setup() {
618666
619667 ui.setWifiManagerCallback (startWifiManagerCallback);
620668 ui.setSpaCallback (setSpaCallback);
621- si.setUpdateFrequency (config.UpdateFrequency .getValue ());
669+ si.setSpaPollFrequency (config.SpaPollFrequency .getValue ());
622670
623671 config.setCallback (configChangeCallbackString);
624672 config.setCallback (configChangeCallbackInt);
673+ config.setCallback (configChangeCallbackBool);
625674
626675}
627676
@@ -639,23 +688,32 @@ void loop() {
639688
640689 if (WiFi.status () != WL_CONNECTED ) {
641690 blinker.setState (STATE_WIFI_NOT_CONNECTED );
691+ wifiRestoredFlag = false ;
642692
643- if (millis ()- wifiLastConnect > 1000 ) { // Reconnect every second if not connected
693+ if (millis () - wifiLastConnect > 10000 ) { // Reconnect every 10 seconds if not connected
644694 debugI (" Wifi reconnecting..." );
645695 wifiLastConnect = millis ();
646- if (WiFi.reconnect ()) {
696+ WiFi.disconnect ();
697+ delay (100 ); // Short delay to ensure disconnect
698+ WiFi.begin ();
699+ if (WiFi.waitForConnectResult () == WL_CONNECTED ) {
647700 debugI (" Wifi reconnected" );
648- MDNS .end (); // Stop mDNS responder (if it was running)
649- if (!MDNS .begin (WiFi.getHostname ())) {
650- debugE (" Failed to start mDNS responder" );
651- } else {
652- debugI (" mDNS responder restarted" );
653- }
701+ wifiRestored ();
654702 } else {
655703 debugW (" Wifi reconnect failed" );
704+ if (WiFi.getMode () == WIFI_STA && !config.SoftAPAlwaysOn .getValue ()) {
705+ debugW (" Failed to connect to Wi-Fi, starting AP mode" );
706+ WiFi.mode (WIFI_AP_STA );
707+ WiFi.softAP (WiFi.getHostname (), config.SoftAPPassword .getValue ().c_str ()); // Start the AP with the hostname and password
708+ } else {
709+ debugE (" Failed to connect to Wi-Fi, but already in AP mode" );
710+ }
656711 };
657712 }
658713 } else {
714+ if (!wifiRestoredFlag) {
715+ wifiRestored ();
716+ }
659717 if (delayedStart) {
660718 delayedStart = !(bootTime + 10000 < millis ());
661719 } else {
@@ -725,5 +783,20 @@ void loop() {
725783 updateMqtt = false ;
726784 }
727785
786+ if (updateSoftAP) {
787+ debugD (" Changing SoftAP settings..." );
788+
789+ if (config.SoftAPAlwaysOn .getValue ()) {
790+ WiFi.mode (WIFI_AP_STA );
791+ WiFi.softAP (config.SpaName .getValue ().c_str (), config.SoftAPPassword .getValue ().c_str ());
792+ debugI (" Soft AP enabled" );
793+ } else {
794+ WiFi.softAPdisconnect (true );
795+ WiFi.mode (WIFI_STA );
796+ debugI (" Soft AP disabled" );
797+ }
798+ updateSoftAP = false ;
799+ }
800+
728801 mqttClient.loop ();
729802}
0 commit comments