@@ -29,7 +29,12 @@ static ConnectionManager *getTimeConnection = NULL;
2929
3030static unsigned long getTime () {
3131 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;
3338}
3439
3540ArduinoIoTCloudClass::ArduinoIoTCloudClass () :
@@ -118,7 +123,6 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
118123
119124
120125 // TODO: Find a better way to allow callback into object method
121-
122126 // Begin function for the MQTTClient
123127 mqttClientBegin ();
124128
@@ -184,41 +188,12 @@ void ArduinoIoTCloudClass::update()
184188 update (MAX_RETRIES, RECONNECTION_TIMEOUT);
185189}
186190
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-
209191void ArduinoIoTCloudClass::update (int const reconnectionMaxRetries, int const reconnectionTimeoutMs)
210192{
211193 connectionCheck ();
212194 if (iotStatus != IOT_STATUS_CLOUD_CONNECTED){
213195 return ;
214196 }
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 ;
222197
223198 // MTTQClient connected!, poll() used to retrieve data from MQTT broker
224199 _mqttClient->poll ();
@@ -320,28 +295,18 @@ void ArduinoIoTCloudClass::connectionCheck()
320295
321296
322297 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:
336299 setIoTConnectionState (IOT_STATUS_CLOUD_CONNECTING);
337300 break ;
338- }
339301 case IOT_STATUS_CLOUD_ERROR:
340302 debugMessage (" Cloud Error. Retrying..." , 0 );
341303 setIoTConnectionState (IOT_STATUS_CLOUD_RECONNECTING);
342304 break ;
343305 case IOT_STATUS_CLOUD_CONNECTED:
344306 debugMessage (" ." , 4 , false , true );
307+ if (!_mqttClient->connected ()){
308+ setIoTConnectionState (IOT_STATUS_CLOUD_DISCONNECTED);
309+ }
345310 break ;
346311 case IOT_STATUS_CLOUD_DISCONNECTED:
347312 setIoTConnectionState (IOT_STATUS_CLOUD_RECONNECTING);
0 commit comments