@@ -87,7 +87,6 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
87
87
, _shadowTopicIn(" " )
88
88
, _dataTopicOut(" " )
89
89
, _dataTopicIn(" " )
90
- , _deviceSubscribedToThing{false }
91
90
#if OTA_ENABLED
92
91
, _ota_cap{false }
93
92
, _ota_error{static_cast <int >(OTAError::None)}
@@ -376,16 +375,13 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeDeviceTopic()
376
375
if (!_mqttClient.subscribe (_deviceTopicIn))
377
376
{
378
377
DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not subscribe to %s" , __FUNCTION__, _deviceTopicIn.c_str ());
379
- return State::SubscribeDeviceTopic;
380
378
}
381
379
382
380
if (_last_device_subscribe_cnt > AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT)
383
381
{
384
382
_last_device_subscribe_cnt = 0 ;
385
383
_next_device_subscribe_attempt_tick = 0 ;
386
- _mqttClient.stop ();
387
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
388
- return State::ConnectPhy;
384
+ return State::Disconnect;
389
385
}
390
386
391
387
/* No device configuration reply. Wait: 5s -> 10s -> 20s -> 30s */
@@ -428,16 +424,6 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_CheckDeviceConfig()
428
424
return State::Disconnect;
429
425
}
430
426
431
- if (_deviceSubscribedToThing == true )
432
- {
433
- /* Unsubscribe from old things topics and go on with a new subscription */
434
- _mqttClient.unsubscribe (_shadowTopicIn);
435
- _mqttClient.unsubscribe (_dataTopicIn);
436
- _deviceSubscribedToThing = false ;
437
- DEBUG_INFO (" Disconnected from Arduino IoT Cloud" );
438
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
439
- }
440
-
441
427
updateThingTopics ();
442
428
443
429
if (_thing_id.length () == 0 )
@@ -465,7 +451,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
465
451
466
452
if (_thing_id_property->isDifferentFromCloud ())
467
453
{
468
- return State::CheckDeviceConfig ;
454
+ return State::Disconnect ;
469
455
}
470
456
471
457
unsigned long const now = millis ();
@@ -481,9 +467,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
481
467
{
482
468
_last_subscribe_request_cnt = 0 ;
483
469
_last_subscribe_request_tick = 0 ;
484
- _mqttClient.stop ();
485
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
486
- return State::ConnectPhy;
470
+ return State::Disconnect;
487
471
}
488
472
489
473
_last_subscribe_request_tick = now;
@@ -506,7 +490,6 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeThingTopics()
506
490
DEBUG_INFO (" Connected to Arduino IoT Cloud" );
507
491
DEBUG_INFO (" Thing ID: %s" , getThingId ().c_str ());
508
492
execCloudEventCallback (ArduinoIoTCloudEvent::CONNECT);
509
- _deviceSubscribedToThing = true ;
510
493
511
494
/* Add retry wait time otherwise we are trying to reconnect every 250 ms...*/
512
495
return State::RequestLastValues;
@@ -521,7 +504,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
521
504
522
505
if (_thing_id_property->isDifferentFromCloud ())
523
506
{
524
- return State::CheckDeviceConfig ;
507
+ return State::Disconnect ;
525
508
}
526
509
527
510
/* Check whether or not we need to send a new request. */
@@ -542,9 +525,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
542
525
{
543
526
_last_sync_request_cnt = 0 ;
544
527
_last_sync_request_tick = 0 ;
545
- _mqttClient.stop ();
546
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
547
- return State::ConnectPhy;
528
+ return State::Disconnect;
548
529
}
549
530
}
550
531
@@ -564,7 +545,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
564
545
{
565
546
if (_thing_id_property->isDifferentFromCloud ())
566
547
{
567
- return State::CheckDeviceConfig ;
548
+ return State::Disconnect ;
568
549
}
569
550
570
551
/* Check if a primitive property wrapper is locally changed.
@@ -641,9 +622,14 @@ void ArduinoIoTCloudTCP::handle_OTARequest() {
641
622
642
623
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect ()
643
624
{
644
- DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
625
+ if (!_mqttClient.connected ()) {
626
+ DEBUG_ERROR (" ArduinoIoTCloudTCP::%s MQTT client connection lost" , __FUNCTION__);
627
+ } else {
628
+ _mqttClient.unsubscribe (_shadowTopicIn);
629
+ _mqttClient.unsubscribe (_dataTopicIn);
630
+ _mqttClient.stop ();
631
+ }
645
632
DEBUG_INFO (" Disconnected from Arduino IoT Cloud" );
646
- _mqttClient.stop ();
647
633
execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
648
634
return State::ConnectPhy;
649
635
}
0 commit comments