Azure IoT Hub with plain MQTT: How to correctly send response to Direct Method?

P-6011 20 Reputation points
2024-04-09T15:13:12.8066667+00:00

Hi all,

I'm currently implementing a device application that connects to Azure IoT Hub over MQTT protocol.

All the publish and subscribe functionality with IoT Hub works without issues, except for responding to a Direct Method event/message. The application I made thus far is able to receive plain cloud-to-device messages, promotes itself as an IoT device with a model id and it can publish telemetry messages successfully.

I have done the direct method implementation in both Python, using Paho MQTT library and in Bash using Mosquitto (Pub and Sub). Both libraries/languages work the same way in this regard, when using the same IoT Hub and hub device.

The MQTT connection, for the most interesting parts, is established as follows:

  • password: SAS token (generated by az iot hub generate-sas-token --connection-string $connection_string )
  • username: {iot_hub_name}.azure-devices.net/{device_id}/?api-version=2021-04-12

The problem lies in responding to the Direct Method:

  • my device is able to receive the Direct Method payload with request id (rid), by subscribing to $iothub/methods/POST/#, as instructed here.
  • if I do not send response back, it fails with timeout, as expected.
  • when I respond immediately by publishing message to response topic $iothub/methods/res/200/?rid={rid} (with matching rid, say 1), the IoT Hub throws an error: The operation failed because the requested device isn't online. To learn more, see https://aka.ms/iothub404103

The error seems a bit odd to me, as the device does receive the payload. So to some extent it is online and the direct method callback is also registered, at least as I understand the registration here. IoT Hub Explorer also shows device online.

Open questions on my end:

  • Is there some detail in the protocol/topics that I have overlooked?
  • Can I debug (view) the traffic from IoT Hub side somehow? Log analytics and Metrics did not seem very helpful for this at first glance. I'm already using IoT Hub Explorer and az CLIs monitoring/command functionality.
  • Any other ideas what might be the issue here?

Thanks already in advance and I'm happy to provide more code examples when needed.

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,203 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sander van de Velde | MVP 33,141 Reputation points MVP
    2024-04-09T19:15:35.5366667+00:00

    Hello @P-6011,

    welcome to this moderated Azure community forum.

    I compared your code with the C# example seen in my blog post.

    You mention the response topic:

    $iothub/methods/res/200/?rid={rid}

    As seen in my post and on in the documentation, it seems the $ sign is missing:

    $iothub/methods/res/{status}/?$rid={request id}

    Can you try it again with the $ sign?


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.