Skip to content

Commit 5d3b90c

Browse files
committed
Also check if connection to the MQTT server is still established during topic subscription and request of last values. Otherwise it can happen that the connection is ripped out under you and you won't even notice.
1 parent 0f70b68 commit 5d3b90c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
328328

329329
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
330330
{
331+
if (!_mqttClient.connected())
332+
{
333+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s MQTT client connection lost", __FUNCTION__);
334+
_mqttClient.stop();
335+
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
336+
return State::ConnectPhy;
337+
}
338+
331339
if (!_mqttClient.subscribe(_dataTopicIn))
332340
{
333341
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not subscribe to %s", __FUNCTION__, _dataTopicIn.c_str());
@@ -360,6 +368,14 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
360368

361369
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
362370
{
371+
if (!_mqttClient.connected())
372+
{
373+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s MQTT client connection lost", __FUNCTION__);
374+
_mqttClient.stop();
375+
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
376+
return State::ConnectPhy;
377+
}
378+
363379
/* Check whether or not we need to send a new request. */
364380
unsigned long const now = millis();
365381
if ((now - _lastSyncRequestTickTime) > TIMEOUT_FOR_LASTVALUES_SYNC)

0 commit comments

Comments
 (0)