@@ -270,7 +270,6 @@ void ArduinoIoTCloudTCP::update()
270
270
case State::ConnectMqttBroker: next_state = handle_ConnectMqttBroker (); break ;
271
271
case State::SendDeviceProperties: next_state = handle_SendDeviceProperties (); break ;
272
272
case State::SubscribeDeviceTopic: next_state = handle_SubscribeDeviceTopic (); break ;
273
- case State::WaitDeviceConfig: next_state = handle_WaitDeviceConfig (); break ;
274
273
case State::CheckDeviceConfig: next_state = handle_CheckDeviceConfig (); break ;
275
274
case State::SubscribeThingTopics: next_state = handle_SubscribeThingTopics (); break ;
276
275
case State::RequestLastValues: next_state = handle_RequestLastValues (); break ;
@@ -366,18 +365,21 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
366
365
DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s announce device to the Cloud %d" , __FUNCTION__, _time_service.getTime ());
367
366
/* TODO check if write fails */
368
367
sendDevicePropertiesToCloud ();
369
- return State::WaitDeviceConfig ;
368
+ return State::SubscribeDeviceTopic ;
370
369
}
371
370
372
- ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig ()
371
+ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic ()
373
372
{
374
373
if (!_mqttClient.connected ())
375
374
{
376
375
return State::Disconnect;
377
376
}
378
377
379
378
bool const is_retry_attempt = (_last_device_subscribe_cnt > 0 );
380
- if (is_retry_attempt && (millis () > _next_device_subscribe_attempt_tick))
379
+ if (is_retry_attempt && (millis () < _next_device_subscribe_attempt_tick))
380
+ return State::SubscribeDeviceTopic;
381
+
382
+ if (is_retry_attempt)
381
383
{
382
384
/* Configuration not received or device not attached to a valid thing. Try to resubscribe */
383
385
if (_mqttClient.unsubscribe (_deviceTopicIn))
@@ -386,19 +388,6 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_WaitDeviceConfig()
386
388
}
387
389
}
388
390
389
- if (!is_retry_attempt || (is_retry_attempt && (millis () > _next_device_subscribe_attempt_tick)))
390
- return State::SubscribeDeviceTopic;
391
-
392
- return State::WaitDeviceConfig;
393
- }
394
-
395
- ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic ()
396
- {
397
- if (!_mqttClient.connected ())
398
- {
399
- return State::Disconnect;
400
- }
401
-
402
391
DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s request device configuration %d" , __FUNCTION__, _time_service.getTime ());
403
392
404
393
if (!_mqttClient.subscribe (_deviceTopicIn))
@@ -423,7 +412,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
423
412
_next_device_subscribe_attempt_tick = millis () + subscribe_retry_delay;
424
413
DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s %d next configuration request in %d ms" , __FUNCTION__, _last_device_subscribe_cnt, subscribe_retry_delay);
425
414
426
- return State::WaitDeviceConfig ;
415
+ return State::SubscribeDeviceTopic ;
427
416
}
428
417
429
418
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig ()
@@ -445,7 +434,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
445
434
_next_device_subscribe_attempt_tick = millis () + attach_retry_delay;
446
435
447
436
DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s device not attached, next configuration request in %d ms" , __FUNCTION__, attach_retry_delay);
448
- return State::WaitDeviceConfig ;
437
+ return State::SubscribeDeviceTopic ;
449
438
}
450
439
451
440
DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s device attached to a new valid thing_id %s %d" , __FUNCTION__, getThingId ().c_str (), _time_service.getTime ());
@@ -657,6 +646,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
657
646
/* Topic for OTA properties and device configuration */
658
647
if (_deviceTopicIn == topic) {
659
648
CBORDecoder::decode (_device_property_container, (uint8_t *)bytes, length);
649
+ _last_device_subscribe_cnt = 0 ;
660
650
_state = State::CheckDeviceConfig;
661
651
}
662
652
0 commit comments