Is it possible to subscribe to Child device Mqtt message using parent device name as clientId

We have one parent device in cumulocity having 5 child device under it, from codesys we are trying to subscribe the child device operation using parent device credentials and client ID(as parent device name) but we are not able get the message.
Its not possible to receive child device message using parent device id as client id in mqtt script, that’s where we are looking for any workaround

Please refer to script in comment

Can you provide more details how your devices are connected to Cumulocity? MQTT using SmartREST?
Assuming MQTT using SmartREST:
All operations (also of child devices) are delivered to the agent of a device. So if your parent is subscribed to s/ds it should also arrive operations for its managed children.

For example Restart Operation created within cumulocity results in a message:

510,childDevice123

While childDevice123 is the external ID of your child device.

Hello,

Thanks for information, We are sending cumulocity DeviceControl operation using REST API

and try to subscribe in codesys using IIoT library, but for simulation we created python script to subscribe the message

please refer to the below python script

import paho.mqtt.client as mqtt

# Replace with your Cumulocity tenant details
C8Y_BASE_URL = '<>'
C8Y_TENANT = '<>'
C8Y_USERNAME = 'device_parent_device'
C8Y_PASSWORD = '<>'

# Device cliendevice_test-device1t ID
CLIENT_ID = 'parent_device'

# Topic to subscribe to
TOPIC = 's/ds'

# Callback when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print(f"Connected with result code {rc}")
    client.subscribe(TOPIC)

# Callback when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(f"Topic: {msg.topic}\nMessage: {msg.payload.decode()}")

# Create an MQTT client instance
client = mqtt.Client(client_id=CLIENT_ID)

# Set the username and password for the MQTT client
client.username_pw_set(username=f'{C8Y_TENANT}/{C8Y_USERNAME}', password=C8Y_PASSWORD)

# Assign the on_connect and on_message callbacks
client.on_connect = on_connect
client.on_message = on_message

# Connect to the Cumulocity MQTT broker
client.connect(C8Y_BASE_URL, 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks, and handles reconnecting.
client.loop_forever()

So in above script, If we are using client id as parent device name and sending operation to parent device, we received it. Perfect!

when we are sending operation to the child device keeping client id as parent device name only in script, we are not able to get the message, that’s we are expecting

But when we change client id to child device name then we are able recive the message.

please provide the solution if possible, so we can subscribe to the child device message, using parent device name as client id

Hi,

It seems you did not create your devices using SmartREST.
You will therefore have to do some tweaking for things to work:

  • ensure the parent device has the fragment com_cumulocity_model_Agent
  • ensure all devices have an external Id of type c8y_Serial

In my test case it was working so I guess Cyril is right and your devices + child devices were not properly created using SmartREST.
This lead to inconsistencies and operations cannot received correctly.

You can easily reproduce that it is working the following way:

  1. Create a device PUB s/us Payload 100,myTestDevice,testDevice
  2. Create a child device PUB s/us Payload 101,childDevice123,myChildDevice,myChildType
  3. Subscribe to s/ds
  4. Create Operation on child device childDevice123 via REST
  5. Receive the operation 510,childDevice123 in your parent client.

Review the created devices and compare them with your devices. Most likely either the Agent fragment is not set, devices are not assigned as child devices correctly or c8y_Serial is missing in child devices as Cyril already stated.

Perfect, thanks @Cyril_Poder and @Stefan_Witschel, after adding c8y_Serial and externalId, it’s start working as expected

1 Like