@@ -362,7 +362,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
362
362
}
363
363
364
364
sendDevicePropertiesToCloud ();
365
- return State::SubscribeDeviceTopic ;
365
+ return State::WaitDeviceConfig ;
366
366
}
367
367
368
368
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic ()
@@ -384,11 +384,10 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
384
384
return State::Disconnect;
385
385
}
386
386
387
- /* No device configuration reply. Wait: 5s -> 10s -> 20s -> 30s */
387
+ _last_device_subscribe_cnt++;
388
388
unsigned long subscribe_retry_delay = (1 << _last_device_subscribe_cnt) * AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms;
389
389
subscribe_retry_delay = min (subscribe_retry_delay, static_cast <unsigned long >(AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms));
390
390
_next_device_subscribe_attempt_tick = millis () + subscribe_retry_delay;
391
- _last_device_subscribe_cnt++;
392
391
393
392
return State::WaitDeviceConfig;
394
393
}
@@ -400,20 +399,19 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
400
399
return State::Disconnect;
401
400
}
402
401
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))
409
404
{
410
405
/* Configuration not received or device not attached to a valid thing. Try to resubscribe */
411
406
if (_mqttClient.unsubscribe (_deviceTopicIn))
412
407
{
413
408
DEBUG_ERROR (" ArduinoIoTCloudTCP::%s device waiting for valid thing_id" , __FUNCTION__);
414
- return State::SubscribeDeviceTopic;
415
409
}
416
410
}
411
+
412
+ if (!is_retry_attempt || (is_retry_attempt && (millis () > _next_device_subscribe_attempt_tick)))
413
+ return State::SubscribeDeviceTopic;
414
+
417
415
return State::WaitDeviceConfig;
418
416
}
419
417
@@ -428,16 +426,19 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
428
426
429
427
if (_thing_id.length () == 0 )
430
428
{
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;
433
433
attach_retry_delay = min (attach_retry_delay, static_cast <unsigned long >(AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms));
434
434
_next_device_subscribe_attempt_tick = millis () + attach_retry_delay;
435
- _last_device_attach_cnt++;
436
435
return State::WaitDeviceConfig;
437
436
}
438
437
439
438
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 ;
441
442
442
443
return State::SubscribeThingTopics;
443
444
}
@@ -652,8 +653,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
652
653
/* Topic for OTA properties and device configuration */
653
654
if (_deviceTopicIn == topic) {
654
655
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;
657
657
}
658
658
659
659
/* Topic for user input data */
0 commit comments