pythonazureiotazure-iot-hubazure-iot-sdk

How to send C2D message feedback response from IoT device in Python


I have a python script running on an IoT device which receives messages from Azure IoT hub and does stuff. I want to get the feedback process working but am missing some understanding.

How do I send the feedback response from the device? Is it simply sending a cloud-to-device message using device_client.send_message formatted somehow with the message_id? Or is there some other send-feedback method that I'm unaware of? I dont see anything related to feedback responses here.

I am using a Function App (written in js) to send the C2D message and it has a function that looks like it just logs the response on the console:

function printResultFor(op) {
    return function printResult(err, res) {
      if (err) console.log(op + " error: " + err.toString());
      if (res) console.log(op + " status: " + res.constructor.name);
    };
  }

When the feedback is sent from the device I assume it will be caught here. Do I then need to send this back to the IoT Hub somehow in order to utilize the retry functionality?

Thanks!


Solution

  • The C2D mechanism is one-way, the device doesn't send any response. If you want the device to send a response to the request, you should use a direct method instead. For more information, see https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-c2d-guidance.

    To ensure once only delivery of C2D messages, IoT Hub uses a per device queue - to understand how this queue works, see, https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-c2d

    According to the readme files in the C, C#, and Java device SDKs: "Receive cloud-to-device messages and read associated custom and system properties from IoT Hub, with the option to complete/reject/abandon C2D messages. IoT Hub supports the option to complete/reject/abandon C2D messages over HTTPS and AMQP only at the moment." The Python SDK currently completes all C2D messages automatically - it's not possible to reject or abandon C2D messages.