1

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.

Raj
  • 735
  • 1
  • 7
  • 24

2 Answers2

1

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.

Matthijs van der Veer
  • 3,865
  • 2
  • 12
  • 22
  • If i try to call any rest api, i am getting, below error { "error": { "code": "InvalidRequest", "message": "This API cannot be called for the application abcd.azureiotcentral.com. If you contact support, please include the following information. Request ID: 8yb2u4iz, Time: Mon, 24 Feb 2020 12:53:21 GMT.", "requestId": "8yb2u4iz", "time": "Mon, 24 Feb 2020 12:53:21 GMT" } } How to fix this – Raj Feb 24 '20 at 12:55
1

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.

Roman Kiss
  • 7,925
  • 1
  • 8
  • 21
  • IOT Central does not support cloud to device messages ?, if so i cannot use IOT central? My device subscribed to 'devices/....ba50621/messages/devicebound/#' and expects a json data which i mentioned in question. Is there any API where i can use it to send this json data ? – Raj Feb 24 '20 at 18:32
  • The REST APIs for current IoTC preview version doesn't have an API for sending C2D message to the device. The Execute Command REST API either for async or sync will invoke a direct method on the device. Please, write the feedback to the IoTC team, https://feedback.azure.com/forums/911455-azure-iot-central – Roman Kiss Feb 24 '20 at 19:32
  • Hi, I just created new IotC app, while creating new template i found that it has preconfigured template for my gateway device exactly same. my device is in azure certified iot plug and play device catlog. when i see properties and command all exactly matching. am so happy to see that. So REST APIs will work perfectly? – Raj Feb 25 '20 at 18:44
  • Yes, it should be worked. What's your device template? – Roman Kiss Feb 25 '20 at 19:10
  • this is the template 'SmartConnect FM-201 IoT Gateway' – Raj Feb 26 '20 at 05:39
  • See my Update 2 – Roman Kiss Feb 26 '20 at 08:32
  • Hi, I am trying to do the same with Azure-IoT-Hub-Tester, Where connection is successful but REST APIs are not populated like ur screenshot, also if i try to load capabilities json downloaded from IotC, it throws input string was not in correct format error. – Raj Feb 27 '20 at 06:51
  • I need one clarification, Template shows multiple commands like reboot and firmware upgrade. How these commands are arrived to real devices connected via MQTT?, Is there any topic which device show subscribe to receive command?. Azure IOT Mqtt document says that Device should subscribe to devices/{deviceID}/messages/devicebound/#, publish to devices/{deviceID}/messages/events/ . Is it different for IotC plug and play device? – Raj Feb 27 '20 at 07:36
  • The *IoTC Plug and Play* is still in preview and my plan is to update the Tester in couple months. If you are interesting for patch, email me, where I can upload it. Anyway, in this error case, you can use the device as a *non PnP* device. Secondly, the tester REST-API feature is based on the template file, which must be populated manually based on the needs or directly in the tester and then manually updated in the template file. Note, that in the REST-API template can be anything what we need it for the specific case, it is a generic way for REST client. – Roman Kiss Feb 27 '20 at 08:31
  • does IotC Rest APIs work only for Plug and play devices?, Still in confusion that how these commands will be received by Device connected via MqTT? – Raj Feb 27 '20 at 08:36
  • IoTC app (all versions) using for *Command* invoking a direct device method pattern, see https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#respond-to-a-direct-method, see also my screen snippets. The PnP model represents a "glue" for request/response payloads, also notice, that the device must used a connection oriented protocol to the IoTC app. In other words, the device connected with https protocol will not work for IoTC app commands. – Roman Kiss Feb 27 '20 at 09:29