Skip to content

Commit fe9f8de

Browse files
Sid23mattiabertorello
authored andcommitted
Fixed broker reconnection after network drop
1 parent 690af85 commit fe9f8de

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/ArduinoIoTCloud.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,24 @@ void ArduinoIoTCloudClass::poll(int reconnectionMaxRetries, int reconnectionTime
152152
}
153153
}
154154

155-
void ArduinoIoTCloudClass::reconnect(Client& net)
155+
int ArduinoIoTCloudClass::reconnect(Client& net)
156156
{
157+
// check if MQTT client is still connected
158+
if (_mqttClient.connected()) {
159+
while(!_mqttClient.disconnect());
160+
}
161+
162+
// Re-initialize _bearSslClient
163+
if (_bearSslClient) {
164+
delete _bearSslClient;
165+
}
166+
_bearSslClient = new BearSSLClient(net);
167+
_bearSslClient->setEccSlot(keySlot, ECCX08Cert.bytes(), ECCX08Cert.length());
168+
157169
// Initialize again the MQTTClient, otherwise it would not be able to receive messages through its callback
158-
mqttClientBegin(net);
159-
connect();
170+
mqttClientBegin(*_bearSslClient);
171+
// Connect to the broker
172+
return connect();
160173
}
161174

162175
void ArduinoIoTCloudClass::onGetTime(unsigned long(*callback)(void))

src/ArduinoIoTCloud.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ArduinoIoTCloudClass {
4040

4141
int connected();
4242
// Clean up existing Mqtt connection, create a new one and initialize it
43-
void reconnect(Client& net);
43+
int reconnect(Client& net);
4444

4545
#define addProperty( v, ...) addPropertyReal(v, #v, __VA_ARGS__)
4646

0 commit comments

Comments
 (0)