IoT Central Direct Method aka Command response

Slawomir Wojtynek 1 Reputation point
2020-06-09T13:40:42.687+00:00

I have an (non-legacy) IoT Central application that can submit the "reboot" command to the device.
And I have an IoT device that accepts that command.
The device communicates with the application using MQTT protocol.

What I'm interested in is to get the response from the device that could be presented in the IoT Central Command History panel.

At first I tried the synchronous commands with a "Response" capability enabled but as I read in the https://learn.microsoft.com/en-us/azure/iot-central/core/tutorial-connect-device-nodejs

> Currently, IoT Central doesn't use the response schema defined in the device capability model. For a synchronous command, the response payload can be any valid JSON.

So now I create an asynchronous command and read the docs further:

> For an asynchronous command, the device should return a 202 response immediately, followed by reported property update when the work is finished.

Now, when my device receives the command, e.g. $iothub/methods/POST/reboot/?$rid=1,
it responds with:
$iothub/methods/res/202/?$rid=1 with the payload: {"status": 202, "payload": {"message": "Rebooting"}}

And after the device is rebooted, it sends the command property update:
$iothub/twin/PATCH/properties/reported/?$rid=7 with the payload: {"reboot": {"value": "Device rebooted"}}

The IoT hub acknowledges the update: $iothub/twin/res/204/?$rid=7&$version=114

But when I open the command history panel I can't see the response - there are SENDING and SENT entries but no RESPONSE.

Where am I making a mistake?

Azure IoT Central
Azure IoT Central
An Azure hosted internet of things (IoT) application platform.
342 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Roman Kiss 2,241 Reputation points
    2020-06-11T18:40:36.33+00:00

    The asynchronously command response is not depended from the invoking a direct device method. The value for this response is taken from the existing reported property with a "hardcoded name" the same as the command name. Note, that the name is a case sensitive.

    In your scenario, be sure that this reported property such as a reboot is existed before executing a command, you can populated with some initialize value. To see this response (the value of the reported property reboot) in the Command history, just make a one command request.

    The following screen snippets show an example of this Reboot command:

    9797-b3.png

    the PnP device has been created by my Azure IoT Hub Tester:

    9809-c1.png

    where the reported property Reboot is:

    9893-d1.png

    and the Command response:

    9780-b1.png

    After this first executing a command, you can change many times the value of the reported property Reboot and refreshing the Command history to see the latest command response.

    Note, that executing a sync command is working well also, where the device sent the response payload to the IoTC App. In this case, the command response is generated by the direct method.

    Update:

    The following screens demonstrated an example of the executing a sync command RebootSync from IoTC App.

    • Adding a new capability (Command RebootSync) to the device template:
      9943-devicetemplates-interface.png
    • the Command response object:

    9924-commandresponseobject.png

    • Run Command RebootSync:

    9839-runrebootsync.png

    • Connected PnP device tester3 received the direct method RebootSync request and sent the response back to the invoker:

    9826-tester-rebbotsyncmethod.png

    • Command response on the IoT Central App:
      9945-commandhistory.png

    The Command history can be obtained also using the REST API, the following screen snippet shows Azure IoT Hub Tester to get this history:

    9946-getcommandhistory.png

    Thanks

    Roman

    1 person found this answer helpful.