Skip to content

Commit 1602ad2

Browse files
committed
Use incremental long delay before retry subscribtion when the device is not attached to a thing
1 parent defe4dd commit 1602ad2

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/AIoTC_Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
#define AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms (32000UL)
147147
#define AIOT_CONFIG_SUBSCRIBE_RETRY_DELAY_ms (1000UL)
148148
#define AIOT_CONFIG_SUBSCRIBE_MAX_RETRY_CNT (10UL)
149+
#define AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms (1280000UL)
149150
#define AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms (30000UL)
150151
#define AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT (10UL)
151152

src/ArduinoIoTCloudTCP.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
475475

476476
if (millis() > _next_device_subscribe_attempt_tick)
477477
{
478-
/* Configuration not received try to resubscribe */
478+
/* Configuration not received or device not attached to a valid thing. Try to resubscribe */
479479
if (_mqttClient.unsubscribe(_deviceTopicIn))
480480
{
481481
return State::SubscribeDeviceTopic;
@@ -496,7 +496,6 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
496496
/* Unsubscribe from old things topics and go on with a new subsctiption */
497497
_mqttClient.unsubscribe(_shadowTopicIn);
498498
_mqttClient.unsubscribe(_dataTopicIn);
499-
500499
_deviceSubscribedToThing = false;
501500
}
502501

@@ -505,13 +504,16 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
505504
if (deviceNotAttached())
506505
{
507506
/* Configuration received but device not attached. Wait: 40s */
508-
unsigned long subscribe_retry_delay = (1 << _last_device_subscribe_cnt) * AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms * 10;
509-
subscribe_retry_delay = min(subscribe_retry_delay, static_cast<unsigned long>(AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms * 10));
510-
_next_device_subscribe_attempt_tick = millis() + subscribe_retry_delay;
511-
_last_device_subscribe_cnt++;
507+
unsigned long attach_retry_delay = (1 << _last_device_attach_cnt) * AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms;
508+
attach_retry_delay = min(attach_retry_delay, static_cast<unsigned long>(AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms));
509+
_next_device_subscribe_attempt_tick = millis() + attach_retry_delay;
510+
_last_device_attach_cnt++;
512511
return State::WaitDeviceConfig;
513512
}
514513

514+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s Device attached to a new valid Thing %s", __FUNCTION__, getThingId().c_str());
515+
_last_device_attach_cnt = 0;
516+
515517
return State::SubscribeThingTopics;
516518
}
517519

@@ -570,6 +572,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
570572
execCloudEventCallback(ArduinoIoTCloudEvent::CONNECT);
571573
_deviceSubscribedToThing = true;
572574

575+
/*Add retry wait time otherwise we are trying to reconnect every 250ms...*/
573576
return State::RequestLastValues;
574577
}
575578

src/ArduinoIoTCloudTCP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
123123
unsigned int _last_connection_attempt_cnt;
124124
unsigned long _next_device_subscribe_attempt_tick;
125125
unsigned int _last_device_subscribe_cnt;
126+
unsigned int _last_device_attach_cnt;
126127
unsigned long _last_sync_request_tick;
127128
unsigned int _last_sync_request_cnt;
128129
unsigned long _last_subscribe_request_tick;

0 commit comments

Comments
 (0)