@@ -29,7 +29,12 @@ static ConnectionManager *getTimeConnection = NULL;
29
29
30
30
static unsigned long getTime () {
31
31
if (!getTimeConnection) return 0 ;
32
- return getTimeConnection->getTime ();
32
+ unsigned long time = getTimeConnection->getTime ();
33
+ if (!NTPUtils::isTimeValid (time )) {
34
+ debugMessage (" Bogus NTP time from API, fallback to UDP method" , 0 );
35
+ time = NTPUtils (getTimeConnection->getUDP ()).getTime ();
36
+ }
37
+ return time ;
33
38
}
34
39
35
40
ArduinoIoTCloudClass::ArduinoIoTCloudClass () :
@@ -118,7 +123,6 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
118
123
119
124
120
125
// TODO: Find a better way to allow callback into object method
121
-
122
126
// Begin function for the MQTTClient
123
127
mqttClientBegin ();
124
128
@@ -184,41 +188,12 @@ void ArduinoIoTCloudClass::update()
184
188
update (MAX_RETRIES, RECONNECTION_TIMEOUT);
185
189
}
186
190
187
- bool ArduinoIoTCloudClass::mqttReconnect (int const maxRetries, int const timeout)
188
- {
189
- // Counter for reconnection retries
190
- int retries = 0 ;
191
- unsigned long start = millis ();
192
-
193
- // Check for MQTT broker connection, of if maxReties limit is reached
194
- // if MQTTClient is connected , simply do nothing and retun true
195
- while (!_mqttClient->connected () && (retries++ < maxRetries) && (millis () - start < timeout)) {
196
- // int connectError = _mqttClient->connectError();
197
-
198
- // try establish the MQTT broker connection
199
- connect ();
200
- }
201
-
202
- // It was impossible to establish a connection, return
203
- if ((retries == maxRetries) || (millis () - start >= timeout))
204
- return false ;
205
-
206
- return true ;
207
- }
208
-
209
191
void ArduinoIoTCloudClass::update (int const reconnectionMaxRetries, int const reconnectionTimeoutMs)
210
192
{
211
193
connectionCheck ();
212
194
if (iotStatus != IOT_STATUS_CLOUD_CONNECTED){
213
195
return ;
214
196
}
215
- // Method's argument controls
216
- int const maxRetries = (reconnectionMaxRetries > 0 ) ? reconnectionMaxRetries : MAX_RETRIES;
217
- int const timeout = (reconnectionTimeoutMs > 0 ) ? reconnectionTimeoutMs : RECONNECTION_TIMEOUT;
218
-
219
- // If the reconnect() culd not establish the connection, return the control to the user sketch
220
- if (!mqttReconnect (maxRetries, timeout))
221
- return ;
222
197
223
198
// MTTQClient connected!, poll() used to retrieve data from MQTT broker
224
199
_mqttClient->poll ();
@@ -320,28 +295,18 @@ void ArduinoIoTCloudClass::connectionCheck()
320
295
321
296
322
297
switch (iotStatus) {
323
- case IOT_STATUS_IDLE:
324
- {
325
- int connectionAttempt;
326
- if (connection == NULL ){
327
- connectionAttempt = begin (*_net, _brokerAddress, _brokerPort);
328
- }else {
329
- connectionAttempt = begin (connection, _brokerAddress, _brokerPort);
330
- }
331
- if (!connectionAttempt){
332
- debugMessage (" Error Starting Arduino Cloud\n Trying again in a few seconds" , 0 );
333
- setIoTConnectionState (IOT_STATUS_CLOUD_ERROR);
334
- return ;
335
- }
298
+ case IOT_STATUS_CLOUD_IDLE:
336
299
setIoTConnectionState (IOT_STATUS_CLOUD_CONNECTING);
337
300
break ;
338
- }
339
301
case IOT_STATUS_CLOUD_ERROR:
340
302
debugMessage (" Cloud Error. Retrying..." , 0 );
341
303
setIoTConnectionState (IOT_STATUS_CLOUD_RECONNECTING);
342
304
break ;
343
305
case IOT_STATUS_CLOUD_CONNECTED:
344
306
debugMessage (" ." , 4 , false , true );
307
+ if (!_mqttClient->connected ()){
308
+ setIoTConnectionState (IOT_STATUS_CLOUD_DISCONNECTED);
309
+ }
345
310
break ;
346
311
case IOT_STATUS_CLOUD_DISCONNECTED:
347
312
setIoTConnectionState (IOT_STATUS_CLOUD_RECONNECTING);
0 commit comments