Unable to create child device via MQTT sometimes

I am using the MQTT protocol to connect with the cumulocity platform and connect it with my application. Once I connected to the platform the parent device is automatically created and but I am facing an issue while creating child devices via MQTT protocol.

Suppose I have a list of devices and I want to add these devices as child devices in my parent device on Cumulocity IoT. So, I used the below command to register the parent device and child device:

To create a Parent device:

this._client.publish("s/us", "100,MyParentDevice");

To create a child device for that Parent device (‘MyParentDevice’):

this._client.publish("s/us", "101, MyChildDevice");

To create a shell in the child device (‘MyChildDevice’):

this._client.publish("s/us/MyChildDevice", "114,c8y_Command");

Now, when I was creating many child devices at the same time, then sometimes the child devices are not created on Cumulocity IoT and once a child device is failed to create then none of the operations happens with it means unable to perform any operation on that device and we have used setTimout as well but it doesn’t create any difference.

Only the parent device is displayed in device management → Devices → All devices.

Can anyone help me with this issue?

Thanks
Anand Bundela

Hi Anand,

did you subscribe on “s/e” to check if there any errors during creation of the child devices?

Also kind of best practice is actually to make sure the devices are created in the platform before performing or sending any additional data to the platform. Otherwise it leads to race conditions and the operation is sent even the device haven’t been created yet. (Most likely results in an “MQTT Device…” created and not the MyChildDevice name is used).
You can achieve that by using QoS 2 and “wait_for_publish”.

So for device creation you need something like this with QoS 2 (exactly once):

this._client.publish("s/us", "101, MyChildDevice", 2).wait_for_publish()

which blocks until the device is created.

Hi Stefan,

it doesn’t help at all, as we are using mqtt NPM in node js and it don’t support this method itself.

if you have any end to end documentation related to implementation of mqtt for cumulocity iot device, will help a lot.

How ever i am attaching our implementation, if possible please have a look and help us to indentify where we are doing wrong.

our device is sending approx 1000 message/sec to cumulocity.

Hi Deepak,

what do you mean with MQTT npm? This package mqtt - npm? In case yes, the package provides the possibilities to subscribe for topics (e.g. s/e) to check for errors. As per documentation, you can define a QoS level for each publish as well. In combination with a callback on your publish, you can wait until your ParentDevice has been created and afterwards create the corresponding child devices. I highly advice to follow @Stefan_Witschel recommendations.

Here you can find the official documentation about device integration using MQTT. A code example for integration via MQTT using nodejs is available here. Looks like the example uses the same library you might be using.

Best regards
Christian

1 Like

Hi @Christian_Guether1 @Stefan_Witschel ,

After subscribing for “s/e” topic the error which we are receiving is “50,100,Error on device creation”.

More error details received as 41,101,Device with serial ‘105’ already existing, where as we don’t see these devices under device management.

how we can clear this device data on platform.

Hi Deepak,

the serial must be an unique identifier which isn’t used by any other device and possibly not in any other tenants.
“105” is not a good serial number and might exist in your tenant already. Might be also a bug that you are sending 101 with wrong parameters and 105 following (to get child devices)

I would prefer to combine uid of parent and child devices to the serial especially when the child device does not have any identifier like ‘105’ e.g.
{parentUniqueIdentifier}_{childUniqueIdentifier} → 23sfhhhsd45f2345_105`

For your client it will look like this:

this._client.publish("s/us", "101,23sfhhhsd45f2345_105,MyChildDevice,MyChildDeviceType");

https://cumulocity.com/guides/reference/smartrest-two/#101

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.