@@ -413,6 +413,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect()
413413 _thing.handleMessage (&message);
414414 _device.handleMessage (&message);
415415
416+ _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
416417 DEBUG_INFO (" Disconnected from Arduino IoT Cloud" );
417418 execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
418419
@@ -444,22 +445,20 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
444445 if (_thing_id_property->isDifferentFromCloud ()) {
445446 _thing_id_property->fromCloudToLocal ();
446447
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 ()) {
458449 /* Send message to device state machine to inform we have received a null thing-id */
459450 _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
451+ Message message;
460452 message = { DeviceRegisteredCmdId };
453+ _device.handleMessage (&message);
454+ } else {
455+ if (_device.isAttached ()) {
456+ detachThing ();
457+ }
458+ if (!_device.isAttached ()) {
459+ attachThing ();
460+ }
461461 }
462- _device.handleMessage (&message);
463462 }
464463 }
465464
@@ -592,6 +591,7 @@ void ArduinoIoTCloudTCP::attachThing()
592591 if (!_mqttClient.subscribe (_dataTopicIn)) {
593592 DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not subscribe to %s" , __FUNCTION__, _dataTopicIn.c_str ());
594593 DEBUG_ERROR (" Check your thing configuration, and press the reset button on your board." );
594+ _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
595595 return ;
596596 }
597597
@@ -600,9 +600,14 @@ void ArduinoIoTCloudTCP::attachThing()
600600 if (!_mqttClient.subscribe (_shadowTopicIn)) {
601601 DEBUG_ERROR (" ArduinoIoTCloudTCP::%s could not subscribe to %s" , __FUNCTION__, _shadowTopicIn.c_str ());
602602 DEBUG_ERROR (" Check your thing configuration, and press the reset button on your board." );
603+ _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
603604 return ;
604605 }
605606
607+ Message message;
608+ message = { DeviceAttachedCmdId };
609+ _device.handleMessage (&message);
610+
606611 DEBUG_INFO (" Connected to Arduino IoT Cloud" );
607612 DEBUG_INFO (" Thing ID: %s" , getThingId ().c_str ());
608613 execCloudEventCallback (ArduinoIoTCloudEvent::CONNECT);
@@ -620,6 +625,11 @@ void ArduinoIoTCloudTCP::detachThing()
620625 return ;
621626 }
622627
628+ Message message;
629+ message = { DeviceDetachedCmdId };
630+ _device.handleMessage (&message);
631+
632+ _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
623633 DEBUG_INFO (" Disconnected from Arduino IoT Cloud" );
624634 execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
625635}
0 commit comments