@@ -95,7 +95,7 @@ int networksStored;
95
95
static bool bEthConnected = false ;
96
96
static bool bEthConnecting = false ;
97
97
static bool bWiFiConnected = false ;
98
-
98
+ static bool bNetworkInterfaceChanged = false ;
99
99
100
100
// --------------------------------------------------------------------------------------------
101
101
// ADXL Accelerometer
@@ -508,6 +508,10 @@ bool OpenEEWDeviceActivation() {
508
508
509
509
510
510
void Connect2MQTTbroker () {
511
+ if ( bNetworkInterfaceChanged ) {
512
+ mqtt.disconnect ();
513
+ bNetworkInterfaceChanged = false ;
514
+ }
511
515
while (!mqtt.connected ()) {
512
516
Serial.print (" Attempting MQTT connection..." );
513
517
NeoPixelStatus ( LED_CONNECT_CLOUD ); // blink cyan
@@ -711,7 +715,7 @@ void NetworkEvent(WiFiEvent_t event) {
711
715
// Disconnect the MQTT session
712
716
if ( mqtt.connected () ){
713
717
Serial.println (" Previously connected to WiFi, try to switch the MQTT connection to Ethernet" );
714
- mqtt. disconnect () ;
718
+ bNetworkInterfaceChanged = true ;
715
719
// No need to call mqtt.setClient(ETH); because ETH is a ETHClient which is not the same class as WiFi client
716
720
// Connect2MQTTbroker(); // The MQTT reconnect will be handled by the main loop()
717
721
}
@@ -722,7 +726,7 @@ void NetworkEvent(WiFiEvent_t event) {
722
726
// Disconnect the MQTT client
723
727
if ( mqtt.connected () ){
724
728
Serial.println (" Previously connected to Ethernet, try to switch the MQTT connection to WiFi" );
725
- mqtt. disconnect () ;
729
+ bNetworkInterfaceChanged = true ;
726
730
}
727
731
break ;
728
732
case SYSTEM_EVENT_ETH_STOP:
0 commit comments