question

SlawomirWojtynek-2112 avatar image
0 Votes"
SlawomirWojtynek-2112 asked SatishBoddu-MSFT commented

IoT Central Direct Method aka Command response

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://docs.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
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @SlawomirWojtynek-2112, Just checking back, Could you please accept below response by Roman as 'Answer' which will help others as well. If you need more help in this matter please let us know, we are happy to help you.

0 Votes 0 ·

Hello @SlawomirWojtynek-2112 ,Please accept the below response from Roman as 'Answer', which will be helpful to others as well with similar question, so that we can close this thread. Please let us know if you need further help in this matter.

0 Votes 0 ·

Hello @SlawomirWojtynek-2112 , Just checking back, could you please let us know if you need further help in this matter. We are happy to help you further!

0 Votes 0 ·

Hello @SlawomirWojtynek-2112, Just checking back, Could you please accept helpful response by Roman as 'Answer' which will help others as well with similar question. So that we can close this thread. If you need more help in this matter please let us know, we are happy to help you.

0 Votes 0 ·

1 Answer

rkiss avatar image
1 Vote"
rkiss answered SatishBoddu-MSFT commented

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




b3.png (38.1 KiB)
c1.png (272.9 KiB)
d1.png (219.1 KiB)
b1.png (51.6 KiB)
runrebootsync.png (62.2 KiB)
commandhistory.png (51.9 KiB)
getcommandhistory.png (252.6 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @SlawomirWojtynek-2112, Just checking back, Could you please accept helpful response by Roman as 'Answer' which will help others as well with similar question. If you need more help in this matter please let us know, we are happy to help you.

0 Votes 0 ·