@@ -413,6 +413,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect()
413
413
_thing.handleMessage (&message);
414
414
_device.handleMessage (&message);
415
415
416
+ _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
416
417
DEBUG_INFO (" Disconnected from Arduino IoT Cloud" );
417
418
execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
418
419
@@ -444,22 +445,20 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
444
445
if (_thing_id_property->isDifferentFromCloud ()) {
445
446
_thing_id_property->fromCloudToLocal ();
446
447
447
- Message message;
448
- /* If we are attached we need first to detach */
449
- if (_device.isAttached ()) {
450
- detachThing ();
451
- message = { DeviceDetachedCmdId };
452
- }
453
- /* If received thing id is valid attach to the new thing */
454
- if (_thing_id.length ()) {
455
- attachThing ();
456
- message = { DeviceAttachedCmdId };
457
- } else {
448
+ if (!_thing_id.length ()) {
458
449
/* Send message to device state machine to inform we have received a null thing-id */
459
450
_thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
451
+ Message message;
460
452
message = { DeviceRegisteredCmdId };
453
+ _device.handleMessage (&message);
454
+ } else {
455
+ if (_device.isAttached ()) {
456
+ detachThing ();
457
+ }
458
+ if (!_device.isAttached ()) {
459
+ attachThing ();
460
+ }
461
461
}
462
- _device.handleMessage (&message);
463
462
}
464
463
}
465
464
@@ -592,6 +591,7 @@ void ArduinoIoTCloudTCP::attachThing()
592
591
if (!_mqttClient.subscribe (_dataTopicIn)) {
593
592
DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not subscribe to %s" , __FUNCTION__, _dataTopicIn.c_str ());
594
593
DEBUG_ERROR (" Check your thing configuration, and press the reset button on your board." );
594
+ _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
595
595
return ;
596
596
}
597
597
@@ -600,9 +600,14 @@ void ArduinoIoTCloudTCP::attachThing()
600
600
if (!_mqttClient.subscribe (_shadowTopicIn)) {
601
601
DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not subscribe to %s" , __FUNCTION__, _shadowTopicIn.c_str ());
602
602
DEBUG_ERROR (" Check your thing configuration, and press the reset button on your board." );
603
+ _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
603
604
return ;
604
605
}
605
606
607
+ Message message;
608
+ message = { DeviceAttachedCmdId };
609
+ _device.handleMessage (&message);
610
+
606
611
DEBUG_INFO (" Connected to Arduino IoT Cloud" );
607
612
DEBUG_INFO (" Thing ID: %s" , getThingId ().c_str ());
608
613
execCloudEventCallback (ArduinoIoTCloudEvent::CONNECT);
@@ -620,6 +625,11 @@ void ArduinoIoTCloudTCP::detachThing()
620
625
return ;
621
626
}
622
627
628
+ Message message;
629
+ message = { DeviceDetachedCmdId };
630
+ _device.handleMessage (&message);
631
+
632
+ _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
623
633
DEBUG_INFO (" Disconnected from Arduino IoT Cloud" );
624
634
execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
625
635
}
0 commit comments