Skip to content

Commit c43aa15

Browse files
committed
Fix thing reconfiguration after wifi connection loss
1 parent 40bf5f7 commit c43aa15

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -391,24 +391,21 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
391391
case CommandId::ThingUpdateCmdId:
392392
{
393393
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] device configuration received", __FUNCTION__, millis());
394-
if ( _thing_id != String(command.thingUpdateCmd.params.thing_id)) {
395-
_thing_id = String(command.thingUpdateCmd.params.thing_id);
394+
String new_thing_id = String(command.thingUpdateCmd.params.thing_id);
395+
396+
if (!new_thing_id.length()) {
397+
/* Send message to device state machine to inform we have received a null thing-id */
398+
_thing_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
396399
Message message;
397-
/* If we are attached we need first to detach */
398-
if (_device.isAttached()) {
400+
message = { DeviceRegisteredCmdId };
401+
_device.handleMessage(&message);
402+
} else {
403+
if (_device.isAttached() && _thing_id != new_thing_id) {
399404
detachThing();
400-
message = { DeviceDetachedCmdId };
401405
}
402-
/* If received thing id is valid attach to the new thing */
403-
if (_thing_id.length()) {
404-
attachThing();
405-
message = { DeviceAttachedCmdId };
406-
} else {
407-
/* Send message to device state machine to inform we have received a null thing-id */
408-
_thing_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
409-
message = { DeviceRegisteredCmdId };
406+
if (!_device.isAttached()) {
407+
attachThing(new_thing_id);
410408
}
411-
_device.handleMessage(&message);
412409
}
413410
}
414411
break;
@@ -491,17 +488,23 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
491488
}
492489
}
493490

494-
void ArduinoIoTCloudTCP::attachThing()
491+
void ArduinoIoTCloudTCP::attachThing(String thingId)
495492
{
493+
_thing_id = thingId;
496494

497495
_dataTopicIn = getTopic_datain();
498496
_dataTopicOut = getTopic_dataout();
499497
if (!_mqttClient.subscribe(_dataTopicIn)) {
500498
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not subscribe to %s", __FUNCTION__, _dataTopicIn.c_str());
501499
DEBUG_ERROR("Check your thing configuration, and press the reset button on your board.");
500+
_thing_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
502501
return;
503502
}
504503

504+
Message message;
505+
message = { DeviceAttachedCmdId };
506+
_device.handleMessage(&message);
507+
505508
DEBUG_INFO("Connected to Arduino IoT Cloud");
506509
DEBUG_INFO("Thing ID: %s", getThingId().c_str());
507510
execCloudEventCallback(ArduinoIoTCloudEvent::CONNECT);
@@ -514,6 +517,12 @@ void ArduinoIoTCloudTCP::detachThing()
514517
return;
515518
}
516519

520+
Message message;
521+
message = { DeviceDetachedCmdId };
522+
_device.handleMessage(&message);
523+
524+
_thing_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
525+
517526
DEBUG_INFO("Disconnected from Arduino IoT Cloud");
518527
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
519528
}

src/ArduinoIoTCloudTCP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
170170
void sendMessage(Message * msg);
171171
void sendPropertyContainerToCloud(String const topic, PropertyContainer & property_container, unsigned int & current_property_index);
172172

173-
void attachThing();
173+
void attachThing(String thingId);
174174
void detachThing();
175175
int write(String const topic, byte const data[], int const length);
176176

0 commit comments

Comments
 (0)