Skip to content

Commit 6d1b031

Browse files
committed
mqtt.disconnect fix for Heap corruption
Signed-off-by: John Walicki <[email protected]>
1 parent 50cac2b commit 6d1b031

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

WatsonIoT/src/main.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ int networksStored;
9595
static bool bEthConnected = false;
9696
static bool bEthConnecting = false;
9797
static bool bWiFiConnected = false;
98-
98+
static bool bNetworkInterfaceChanged = false;
9999

100100
// --------------------------------------------------------------------------------------------
101101
// ADXL Accelerometer
@@ -508,6 +508,10 @@ bool OpenEEWDeviceActivation() {
508508

509509

510510
void Connect2MQTTbroker() {
511+
if( bNetworkInterfaceChanged ) {
512+
mqtt.disconnect();
513+
bNetworkInterfaceChanged = false;
514+
}
511515
while (!mqtt.connected()) {
512516
Serial.print("Attempting MQTT connection...");
513517
NeoPixelStatus( LED_CONNECT_CLOUD ); // blink cyan
@@ -711,7 +715,7 @@ void NetworkEvent(WiFiEvent_t event) {
711715
// Disconnect the MQTT session
712716
if( mqtt.connected() ){
713717
Serial.println("Previously connected to WiFi, try to switch the MQTT connection to Ethernet");
714-
mqtt.disconnect();
718+
bNetworkInterfaceChanged = true;
715719
// No need to call mqtt.setClient(ETH); because ETH is a ETHClient which is not the same class as WiFi client
716720
// Connect2MQTTbroker(); // The MQTT reconnect will be handled by the main loop()
717721
}
@@ -722,7 +726,7 @@ void NetworkEvent(WiFiEvent_t event) {
722726
// Disconnect the MQTT client
723727
if( mqtt.connected() ){
724728
Serial.println("Previously connected to Ethernet, try to switch the MQTT connection to WiFi");
725-
mqtt.disconnect();
729+
bNetworkInterfaceChanged = true;
726730
}
727731
break;
728732
case SYSTEM_EVENT_ETH_STOP:

0 commit comments

Comments
 (0)