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.