@@ -79,6 +79,8 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
79
79
, _last_connection_attempt_cnt{0 }
80
80
, _last_sync_request_tick{0 }
81
81
, _last_sync_request_cnt{0 }
82
+ , _last_subscribe_request_tick{0 }
83
+ , _last_subscribe_request_cnt{0 }
82
84
, _mqtt_data_buf{0 }
83
85
, _mqtt_data_len{0 }
84
86
, _mqtt_data_request_retransmit{false }
@@ -404,34 +406,58 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
404
406
return State::ConnectPhy;
405
407
}
406
408
407
- if (!_mqttClient.subscribe (_dataTopicIn))
409
+ unsigned long const now = millis ();
410
+ bool const is_subscribe_retry_delay_expired = (now - _last_subscribe_request_tick) > AIOT_CONFIG_SUBSCRIBE_RETRY_DELAY_ms;
411
+ bool const is_first_subscribe_request = (_last_subscribe_request_cnt == 0 );
412
+ if (is_first_subscribe_request || is_subscribe_retry_delay_expired)
408
413
{
409
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not subscribe to %s" , __FUNCTION__, _dataTopicIn.c_str ());
410
- #if !defined(__AVR__)
411
- DEBUG_ERROR (" Check your thing configuration, and press the reset button on your board." );
412
- #endif
413
- return State::SubscribeMqttTopics;
414
- }
414
+ if (_last_subscribe_request_cnt > AIOT_CONFIG_SUBSCRIBE_MAX_RETRY_CNT)
415
+ {
416
+ _last_subscribe_request_cnt = 0 ;
417
+ _last_subscribe_request_tick = 0 ;
418
+ _mqttClient.stop ();
419
+ execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
420
+ return State::ConnectPhy;
421
+ }
415
422
416
- if (_shadowTopicIn != " " )
417
- {
418
- if (!_mqttClient.subscribe (_shadowTopicIn))
423
+ _last_subscribe_request_tick = now;
424
+ _last_subscribe_request_cnt++;
425
+
426
+ if (!_mqttClient.subscribe (_dataTopicIn))
419
427
{
420
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not subscribe to %s" , __FUNCTION__, _shadowTopicIn .c_str ());
428
+ DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not subscribe to %s" , __FUNCTION__, _dataTopicIn .c_str ());
421
429
#if !defined(__AVR__)
422
430
DEBUG_ERROR (" Check your thing configuration, and press the reset button on your board." );
423
431
#endif
424
432
return State::SubscribeMqttTopics;
425
433
}
426
- }
427
434
428
- DEBUG_INFO (" Connected to Arduino IoT Cloud" );
429
- execCloudEventCallback (ArduinoIoTCloudEvent::CONNECT);
435
+ if (_shadowTopicIn != " " )
436
+ {
437
+ if (!_mqttClient.subscribe (_shadowTopicIn))
438
+ {
439
+ DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not subscribe to %s" , __FUNCTION__, _shadowTopicIn.c_str ());
440
+ #if !defined(__AVR__)
441
+ DEBUG_ERROR (" Check your thing configuration, and press the reset button on your board." );
442
+ #endif
443
+ return State::SubscribeMqttTopics;
444
+ }
445
+ }
430
446
431
- if (_shadowTopicIn != " " )
432
- return State::RequestLastValues;
433
- else
434
- return State::Connected;
447
+ _last_subscribe_request_cnt = 0 ;
448
+ _last_subscribe_request_tick = 0 ;
449
+
450
+ DEBUG_INFO (" Connected to Arduino IoT Cloud" );
451
+ execCloudEventCallback (ArduinoIoTCloudEvent::CONNECT);
452
+
453
+ if (_shadowTopicIn != " " )
454
+ return State::RequestLastValues;
455
+ else
456
+ return State::Connected;
457
+
458
+ }
459
+
460
+ return State::SubscribeMqttTopics;
435
461
}
436
462
437
463
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues ()
0 commit comments