53
53
extern RTC_HandleTypeDef RTCHandle;
54
54
#endif
55
55
56
- /* *****************************************************************************
57
- GLOBAL CONSTANTS
58
- ******************************************************************************/
59
-
60
- static const int TIMEOUT_FOR_LASTVALUES_SYNC = 10000 ;
61
-
62
56
/* *****************************************************************************
63
57
LOCAL MODULE FUNCTIONS
64
58
******************************************************************************/
@@ -74,7 +68,9 @@ extern "C" unsigned long getTime()
74
68
75
69
ArduinoIoTCloudTCP::ArduinoIoTCloudTCP ()
76
70
: _state{State::ConnectPhy}
77
- , _lastSyncRequestTickTime{0 }
71
+ , _next_connection_attempt_tick{0 }
72
+ , _last_connection_attempt_cnt{0 }
73
+ , _last_sync_request_tick{0 }
78
74
, _mqtt_data_buf{0 }
79
75
, _mqtt_data_len{0 }
80
76
, _mqtt_data_request_retransmit{false }
@@ -337,9 +333,13 @@ void ArduinoIoTCloudTCP::printDebugInfo()
337
333
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy ()
338
334
{
339
335
if (_connection->check () == NetworkConnectionState::CONNECTED)
340
- return State::SyncTime;
341
- else
342
- return State::ConnectPhy;
336
+ {
337
+ bool const is_retry_attempt = (_last_connection_attempt_cnt > 0 );
338
+ if (!is_retry_attempt || (is_retry_attempt && (millis () > _next_connection_attempt_tick)))
339
+ return State::SyncTime;
340
+ }
341
+
342
+ return State::ConnectPhy;
343
343
}
344
344
345
345
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime ()
@@ -352,9 +352,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime()
352
352
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker ()
353
353
{
354
354
if (_mqttClient.connect (_brokerAddress.c_str (), _brokerPort))
355
+ {
356
+ _last_connection_attempt_cnt = 0 ;
355
357
return State::SubscribeMqttTopics;
358
+ }
359
+
360
+ _last_connection_attempt_cnt++;
361
+ unsigned long reconnection_retry_delay = (1 << _last_connection_attempt_cnt) * AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms;
362
+ reconnection_retry_delay = min (reconnection_retry_delay, static_cast <unsigned long >(AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms));
363
+ _next_connection_attempt_tick = millis () + reconnection_retry_delay;
356
364
357
365
DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not connect to %s:%d" , __FUNCTION__, _brokerAddress.c_str (), _brokerPort);
366
+ DEBUG_ERROR (" ArduinoIoTCloudTCP::%s %d connection attempt at tick time %d" , __FUNCTION__, _last_connection_attempt_cnt, _next_connection_attempt_tick);
358
367
return State::ConnectPhy;
359
368
}
360
369
@@ -410,11 +419,11 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
410
419
411
420
/* Check whether or not we need to send a new request. */
412
421
unsigned long const now = millis ();
413
- if ((now - _lastSyncRequestTickTime ) > TIMEOUT_FOR_LASTVALUES_SYNC )
422
+ if ((now - _last_sync_request_tick ) > AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms )
414
423
{
415
424
DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s [%d] last values requested" , __FUNCTION__, now);
416
425
requestLastValue ();
417
- _lastSyncRequestTickTime = now;
426
+ _last_sync_request_tick = now;
418
427
}
419
428
420
429
return State::RequestLastValues;
0 commit comments