@@ -362,7 +362,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
362362 }
363363
364364 sendDevicePropertiesToCloud ();
365- return State::SubscribeDeviceTopic ;
365+ return State::WaitDeviceConfig ;
366366}
367367
368368ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic ()
@@ -384,11 +384,10 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
384384 return State::Disconnect;
385385 }
386386
387- /* No device configuration reply. Wait: 5s -> 10s -> 20s -> 30s */
387+ _last_device_subscribe_cnt++;
388388 unsigned long subscribe_retry_delay = (1 << _last_device_subscribe_cnt) * AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms;
389389 subscribe_retry_delay = min (subscribe_retry_delay, static_cast <unsigned long >(AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms));
390390 _next_device_subscribe_attempt_tick = millis () + subscribe_retry_delay;
391- _last_device_subscribe_cnt++;
392391
393392 return State::WaitDeviceConfig;
394393}
@@ -400,20 +399,19 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
400399 return State::Disconnect;
401400 }
402401
403- if (_thing_id_property->isDifferentFromCloud ())
404- {
405- return State::CheckDeviceConfig;
406- }
407-
408- if (millis () > _next_device_subscribe_attempt_tick)
402+ bool const is_retry_attempt = (_last_device_subscribe_cnt > 0 );
403+ if (is_retry_attempt && (millis () > _next_device_subscribe_attempt_tick))
409404 {
410405 /* Configuration not received or device not attached to a valid thing. Try to resubscribe */
411406 if (_mqttClient.unsubscribe (_deviceTopicIn))
412407 {
413408 DEBUG_ERROR (" ArduinoIoTCloudTCP::%s device waiting for valid thing_id" , __FUNCTION__);
414- return State::SubscribeDeviceTopic;
415409 }
416410 }
411+
412+ if (!is_retry_attempt || (is_retry_attempt && (millis () > _next_device_subscribe_attempt_tick)))
413+ return State::SubscribeDeviceTopic;
414+
417415 return State::WaitDeviceConfig;
418416}
419417
@@ -428,16 +426,19 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
428426
429427 if (_thing_id.length () == 0 )
430428 {
431- /* Configuration received but device not attached. Wait: 40s */
432- unsigned long attach_retry_delay = (1 << _last_device_attach_cnt) * AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms;
429+ /* Device configuration received, but invalid thing_id. Do not increase counter, but recompute delay.
430+ * Device not attached. Wait: 40s -> 80s -> 160s -> 320s -> 640s -> 1280s -> 1280s ...
431+ */
432+ unsigned long attach_retry_delay = (1 << _last_device_subscribe_cnt) * AIOT_CONFIG_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms;
433433 attach_retry_delay = min (attach_retry_delay, static_cast <unsigned long >(AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms));
434434 _next_device_subscribe_attempt_tick = millis () + attach_retry_delay;
435- _last_device_attach_cnt++;
436435 return State::WaitDeviceConfig;
437436 }
438437
439438 DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s Device attached to a new valid Thing %s" , __FUNCTION__, getThingId ().c_str ());
440- _last_device_attach_cnt = 0 ;
439+
440+ /* Received valid thing_id reset counters and go on */
441+ _last_device_subscribe_cnt = 0 ;
441442
442443 return State::SubscribeThingTopics;
443444}
@@ -652,8 +653,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
652653 /* Topic for OTA properties and device configuration */
653654 if (_deviceTopicIn == topic) {
654655 CBORDecoder::decode (_device_property_container, (uint8_t *)bytes, length);
655- _last_device_subscribe_cnt = 0 ;
656- _next_device_subscribe_attempt_tick = 0 ;
656+ _state = State::CheckDeviceConfig;
657657 }
658658
659659 /* Topic for user input data */
0 commit comments