Skip to content

Commit e517fed

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

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
@@ -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

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

469469
if (millis() > _next_device_subscribe_attempt_tick)
470470
{
471-
/* Configuration not received try to resubscribe */
471+
/* Configuration not received or device not attached to a valid thing. Try to resubscribe */
472472
if (_mqttClient.unsubscribe(_deviceTopicIn))
473473
{
474474
return State::SubscribeDeviceTopic;
@@ -489,7 +489,6 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
489489
/* Unsubscribe from old things topics and go on with a new subsctiption */
490490
_mqttClient.unsubscribe(_shadowTopicIn);
491491
_mqttClient.unsubscribe(_dataTopicIn);
492-
493492
_deviceSubscribedToThing = false;
494493
}
495494

@@ -498,13 +497,16 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
498497
if (deviceNotAttached())
499498
{
500499
/* Configuration received but device not attached. Wait: 40s */
501-
unsigned long subscribe_retry_delay = (1 << _last_device_subscribe_cnt) * AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms * 10;
502-
subscribe_retry_delay = min(subscribe_retry_delay, static_cast<unsigned long>(AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms * 10));
503-
_next_device_subscribe_attempt_tick = millis() + subscribe_retry_delay;
504-
_last_device_subscribe_cnt++;
500+
unsigned long attach_retry_delay = (1 << _last_device_attach_cnt) * AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms;
501+
attach_retry_delay = min(attach_retry_delay, static_cast<unsigned long>(AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms));
502+
_next_device_subscribe_attempt_tick = millis() + attach_retry_delay;
503+
_last_device_attach_cnt++;
505504
return State::WaitDeviceConfig;
506505
}
507506

507+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s Device attached to a new valid Thing %s", __FUNCTION__, getThingId().c_str());
508+
_last_device_attach_cnt = 0;
509+
508510
return State::SubscribeThingTopics;
509511
}
510512

@@ -563,6 +565,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
563565
execCloudEventCallback(ArduinoIoTCloudEvent::CONNECT);
564566
_deviceSubscribedToThing = true;
565567

568+
/*Add retry wait time otherwise we are trying to reconnect every 250ms...*/
566569
return State::RequestLastValues;
567570
}
568571

src/ArduinoIoTCloudTCP.h

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
121121
unsigned int _last_connection_attempt_cnt;
122122
unsigned long _next_device_subscribe_attempt_tick;
123123
unsigned int _last_device_subscribe_cnt;
124+
unsigned int _last_device_attach_cnt;
124125
unsigned long _last_sync_request_tick;
125126
unsigned int _last_sync_request_cnt;
126127
unsigned long _last_subscribe_request_tick;

0 commit comments

Comments
 (0)