Skip to content

Commit 105409e

Browse files
committed
Use incremental long delay before retry subscribtion when the device is not attached to a thing
1 parent 047540a commit 105409e

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/AIoTC_Config.h

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
#define AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms (32000UL)
145145
#define AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms (5*1000UL)
146146
#define AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms (32000UL)
147+
#define AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms (1280000UL)
147148
#define AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms (30000UL)
148149
#define AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT (10UL)
149150

src/ArduinoIoTCloudTCP.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
474474

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

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

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

@@ -548,6 +550,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
548550
execCloudEventCallback(ArduinoIoTCloudEvent::CONNECT);
549551
_deviceSubscribedToThing = true;
550552

553+
/*Add retry wait time otherwise we are trying to reconnect every 250ms...*/
551554
return State::RequestLastValues;
552555
}
553556

src/ArduinoIoTCloudTCP.h

+1
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
String _brokerAddress;

0 commit comments

Comments
 (0)