Search code examples
azureazure-iot-central

Azure IoT Central - Display Command execution results


how to display Commands results in IoT Central - Commands page?

enter image description here

Flow:

  • Command sent to Device
  • Device received the command and returns Payload:
{
  "status": 200,
  "payload": {
    "command": "ping",
    "result_code": "ok",
    "result_data": "ping 5 ms"
  }
}

How to display this data in IoT Central?

Now IoT Central shows only default "Sent at ..." message.


Solution

  • If you set a reported property with the same name as the command when you handle the command on the device, then you can set a value that displays in the IoT Central UI. For example, using Node.js you can create a handler for a countdown method like this:

    function onCountdown(request, response) {
      console.log('received a request for Countdown');
      console.log(JSON.stringify(request.payload, null, 2));
      var fakeResponsePayload = {
        key: 'value'
      };
    
      response.send(200, fakeResponsePayload, function (err) {
        if (err) {
          console.error('Unable to send method response: ' + err.toString());
        } else {
          console.log('response to Countdown sent.');
          client.getTwin(function(err, twin) {
            if (err) {
              console.error('could not get twin');
            } else {
              console.log('twin created');
              var patch = {
                countdown:{
                  value: 18
                }
              };
              twin.properties.reported.update(patch, function(err) {
                if (err) {
                  console.error('unable to update twin: ' + err.toString());
                } else {
                  console.log('twin state reported');
                }
              });
            }
          });      
        }
      });
    }
    
    client.onDeviceMethod('countdown', onCountdown);
    

    Note that the name of the field name of the command is countdown.

    enter image description here

    There is also a C++ example here: https://github.com/Azure/iot-central-firmware/blob/master/MXCHIP/mxchip_advanced/src/registeredMethodHandlers.cpp