@@ -344,13 +344,15 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
344
344
return State::SendDeviceProperties;
345
345
}
346
346
347
+ /* Can't connect to the broker. Wait: 2s -> 4s -> 8s -> 16s -> 32s -> 32s ... */
347
348
_last_connection_attempt_cnt++;
348
349
unsigned long reconnection_retry_delay = (1 << _last_connection_attempt_cnt) * AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms;
349
350
reconnection_retry_delay = min (reconnection_retry_delay, static_cast <unsigned long >(AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms));
350
351
_next_connection_attempt_tick = millis () + reconnection_retry_delay;
351
352
352
353
DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not connect to %s:%d" , __FUNCTION__, _brokerAddress.c_str (), _brokerPort);
353
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s %d connection attempt at tick time %d" , __FUNCTION__, _last_connection_attempt_cnt, _next_connection_attempt_tick);
354
+ DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s %d next connection attempt in %d ms" , __FUNCTION__, _last_connection_attempt_cnt, reconnection_retry_delay);
355
+ /* Go back to ConnectPhy and retry to get time from network (invalid time for SSL handshake?)*/
354
356
return State::ConnectPhy;
355
357
}
356
358
@@ -361,6 +363,8 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
361
363
return State::Disconnect;
362
364
}
363
365
366
+ DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s announce device to the Cloud %d" , __FUNCTION__, _time_service.getTime ());
367
+ /* TODO check if write fails */
364
368
sendDevicePropertiesToCloud ();
365
369
return State::WaitDeviceConfig;
366
370
}
@@ -372,22 +376,30 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
372
376
return State::Disconnect;
373
377
}
374
378
379
+ DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s request device configuration %d" , __FUNCTION__, _time_service.getTime ());
380
+
375
381
if (!_mqttClient.subscribe (_deviceTopicIn))
376
382
{
383
+ /* If device_id is wrong the board can't connect to the broker so this condition
384
+ * should never happen.
385
+ */
377
386
DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not subscribe to %s" , __FUNCTION__, _deviceTopicIn.c_str ());
378
387
}
379
388
389
+ /* Max retry than disconnect */
380
390
if (_last_device_subscribe_cnt > AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT)
381
391
{
382
392
_last_device_subscribe_cnt = 0 ;
383
393
_next_device_subscribe_attempt_tick = 0 ;
384
394
return State::Disconnect;
385
395
}
386
396
397
+ /* No device configuration received. Wait: 4s -> 8s -> 16s -> 32s -> 32s ...*/
387
398
_last_device_subscribe_cnt++;
388
399
unsigned long subscribe_retry_delay = (1 << _last_device_subscribe_cnt) * AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms;
389
400
subscribe_retry_delay = min (subscribe_retry_delay, static_cast <unsigned long >(AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms));
390
401
_next_device_subscribe_attempt_tick = millis () + subscribe_retry_delay;
402
+ DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s %d next configuration request in %d ms" , __FUNCTION__, _last_device_subscribe_cnt, subscribe_retry_delay);
391
403
392
404
return State::WaitDeviceConfig;
393
405
}
@@ -405,7 +417,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
405
417
/* Configuration not received or device not attached to a valid thing. Try to resubscribe */
406
418
if (_mqttClient.unsubscribe (_deviceTopicIn))
407
419
{
408
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s device waiting for valid thing_id" , __FUNCTION__);
420
+ DEBUG_ERROR (" ArduinoIoTCloudTCP::%s device waiting for valid thing_id %d " , __FUNCTION__, _time_service. getTime () );
409
421
}
410
422
}
411
423
@@ -432,10 +444,12 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
432
444
unsigned long attach_retry_delay = (1 << _last_device_subscribe_cnt) * AIOT_CONFIG_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms;
433
445
attach_retry_delay = min (attach_retry_delay, static_cast <unsigned long >(AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms));
434
446
_next_device_subscribe_attempt_tick = millis () + attach_retry_delay;
447
+
448
+ DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s device not attached, next configuration request in %d ms" , __FUNCTION__, attach_retry_delay);
435
449
return State::WaitDeviceConfig;
436
450
}
437
451
438
- DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s Device attached to a new valid Thing %s" , __FUNCTION__, getThingId ().c_str ());
452
+ DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s device attached to a new valid thing_id %s %d " , __FUNCTION__, getThingId ().c_str (), _time_service. getTime ());
439
453
440
454
/* Received valid thing_id reset counters and go on */
441
455
_last_device_subscribe_cnt = 0 ;
0 commit comments