Skip to content

Commit 71ee279

Browse files
authored
Track the number of unsuccessful request-last-values. (#249)
In the current implementation the number the board would continously attempt to obtain the last values from the cloud. If, however, those values never arrive - then the board would never move on and become operational. It's therefore better to count the number of failed attempts and reset the connection to allow a clean re-connection from the ground up.
1 parent 10d35e4 commit 71ee279

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/AIoTC_Config.h

+1
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,6 @@
141141
#define AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms (1000UL)
142142
#define AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms (32000UL)
143143
#define AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms (30000UL)
144+
#define AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT (10UL)
144145

145146
#endif /* ARDUINO_AIOTC_CONFIG_H_ */

src/ArduinoIoTCloudTCP.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
7171
, _next_connection_attempt_tick{0}
7272
, _last_connection_attempt_cnt{0}
7373
, _last_sync_request_tick{0}
74+
, _last_sync_request_cnt{0}
7475
, _mqtt_data_buf{0}
7576
, _mqtt_data_len{0}
7677
, _mqtt_data_request_retransmit{false}
@@ -424,6 +425,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
424425
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values requested", __FUNCTION__, now);
425426
requestLastValue();
426427
_last_sync_request_tick = now;
428+
/* Track the number of times a get-last-values request was sent to the cloud.
429+
* If no data is received within a certain number of retry-requests it's a better
430+
* strategy to disconnect and re-establish connection from the ground up.
431+
*/
432+
_last_sync_request_cnt++;
433+
if (_last_sync_request_cnt > AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT)
434+
{
435+
_last_sync_request_cnt = 0;
436+
_mqttClient.stop();
437+
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
438+
return State::ConnectPhy;
439+
}
427440
}
428441

429442
return State::RequestLastValues;
@@ -517,6 +530,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
517530
CBORDecoder::decode(_property_container, (uint8_t*)bytes, length, true);
518531
sendPropertiesToCloud();
519532
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
533+
_last_sync_request_cnt = 0;
520534
_state = State::Connected;
521535
}
522536
}

src/ArduinoIoTCloudTCP.h

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
9999
unsigned long _next_connection_attempt_tick;
100100
unsigned int _last_connection_attempt_cnt;
101101
unsigned long _last_sync_request_tick;
102+
unsigned int _last_sync_request_cnt;
102103
String _brokerAddress;
103104
uint16_t _brokerPort;
104105
uint8_t _mqtt_data_buf[MQTT_TRANSMIT_BUFFER_SIZE];

0 commit comments

Comments
 (0)