1
votes

My IOT Device is connected to IOT central and am able to receive data through export option. Now i want to send some command to My device, For example, Below json data it is expecting to send heartbeat of device on subscribed URL'devices/....ba50621/messages/devicebound/#'

{ "action": "heartBeat", 
  "requestId": "b2c3d4e5-3424-4dca-32dc-12b73290cfed"
}

i created service application by refering below links to send Cloud to device message,

Stackoverlow ans and Sample app

used connection string as,

"HostName=asdasdas.azure-devices.net;DeviceId=asdasdasda50621;SharedAccessKey=nXmyveEPrV....pmo9jULvDtik="

This connection string was received when device got register though dps. i used this on service application to connect. i dont know whether this correct.

When application tried to send message, i got unauthorized error. In the documentation of above link says to get connection string from shared access policy of IOT hub. but i am using iot central and used above connection string.

Kindl help me fix it.

2

2 Answers

1
votes

Cloud to device messaging works a bit different in IoT Central than a plain IoT Hub. Instead of connecting the service client to your IoT Hub, you have to use the IoT Central REST API.

You can either send commands to your device or update a property. The complete documentation including samples are here. You can find the documentation to send commands to your devices here.

1
votes

In the case when your IoTC App is NOT a legacy application, you can use a preview IoTC App (which is a CapabilityModel driven model type) with the REST APIs support for calling your device. Currently preview version doesn't have a feature what you are trying achieved, such as sending a C2D message to the device.

The IoTC preview supports only calling the device method - Execute Command. The following screen snippets demonstrated this feature. For this example, I am using my Azure IoT Hub Tester, where the virtual devices are using the MQTT protocol directly to the underlying IoT Hub of the IoT Central App.

I have created a free IoT Central App with the AzureKit ESP32 template, registered and provisioned for device1 and connected from the Azure IoT Hub Tester:

  1. Showing a command echo PnP model: enter image description here

  2. Invoking a command echo by REST POST call. Note, that the Authorization sastoken must be created in the Administration/API tokens page.

enter image description here

  1. The device1 received the direct call and than published its response (note, that this screen was not snipped for its short response time limit). The following screen snippet shows a complete sync REST call with the device1. Note, that this preview version didn't handle the response payload based on the mentioned document and PnP model.

enter image description here

  1. Finally, this screen snippet shows the history of this command action via the REST:

enter image description here

Note, that there is also missing the response object from the device like is described in the CapabilityModel schema.

UPDATE

I have added the missing screen snippet on the device side when the method echo has been invoked:

enter image description here

As you can see the above picture, the device received the REST POST payload and its published response back to the invoker based on the PnP model schema.

UPDATE 2

The following screen snippets demonstrated invoking a command upgrade for device4. Note, that the device4 is a PnP device created from the template SmartConnect FM-201 IoT Gateway.

enter image description here

The device4 received a message on the direct method upgrade:

enter image description here

The device4 sent the response back to the invoker and IoTC app. The following picture shows its history:

enter image description here

Notice again, there is missing a response object in the REST response and also in the IoTC App. I hope, this is a bug in this preview version and it will fix it soon to follow up the CapabilityModel schema.