0

I want to publish a message to IoT Hub and also receive the message via the same topic. I connected to IoT Hub and then subscribed to the same topic to which i send the messages, but I don't receive any message. As Client I am using MQTTX windows. Messages are sent and received correctly at the cloud endpoint (telemetry data).

Here is a screenshot from my MQTT client: MQTT Client

ricktauss
  • 27
  • 4

2 Answers2

0

The Azure IoT Hub is not a generic MQTT broker, see more details here.

I do recommend to read also the doc for Azure IoT Edge MQTT broker.

UPDATE:

Based on your needs, such as a round trip time test, the following is an example how can be achieved this loopback in the Azure IoT Central. Note, that the Iot Central App has a built-in great features to simplify your test without using additional azure resource such as an Azure function.

The concept of this example is based on the following:

  • REST API for invoking a durable command (Queuing command) to generate a C2D Message.
  • Using the Data Transformation for exporting a message to the webhook destination

First of all, we have to create a device template (in my example loopback) for our test:

{
"@id": "dtmi:rk2022iotcfree:loopback_24v;1",
"@type": "Interface",
"contents": [
    {
        "@id": "dtmi:rk2022iotcfree:loopback_24v:message;1",
        "@type": "Command",
        "displayName": {
            "en": "Message"
        },
        "name": "message",
        "request": {
            "@type": "CommandPayload",
            "displayName": {
                "en": "Info"
            },
            "name": "Info",
            "schema": {
                "@type": "Object",
                "displayName": {
                    "en": "Object"
                },
                "fields": [
                    {
                        "displayName": {
                            "en": "ts"
                        },
                        "name": "ts",
                        "schema": "dateTime"
                    },
                    {
                        "displayName": {
                            "en": "msg"
                        },
                        "name": "msg",
                        "schema": "string"
                    }
                ]
            }
        },
        "durable": true
    },
    {
        "@id": "dtmi:rk2022iotcfree:loopback_24v:counter;1",
        "@type": "Telemetry",
        "displayName": {
            "en": "Counter"
        },
        "name": "counter",
        "schema": "double"
    },
    {
        "@id": "dtmi:rk2022iotcfree:loopback_24v:time;1",
        "@type": "Telemetry",
        "displayName": {
            "en": "Timestamp"
        },
        "name": "time",
        "schema": "double"
    }
],
"displayName": {
    "en": "loopback"
},
"@context": [
    "dtmi:iotcentral:context;2",
    "dtmi:dtdl:context;2"
]

}

Once we have the device template, we can create a device (in my example device1) with assigning to this device template (loopback).

Now, we need to declare a destination webhook endpoint:

enter image description here

where the Callback URL is:

https://2a92220d-42e3-4a73-b700-2cb858c9c5e7.azureiotcentral.com/api/devices/device1/commands/message?api-version=1.2-preview

and the Authorization token is generated as the IoT Central Api token

Next step is declaring a Data transformation for this destination endpoint:

 import "iotc" as iotc;
 if .device.id == "device1" then
 { 
  request: {
    Info:{
      ts:.enqueuedTime,
      msg:("Feedback from device: id=" + .device.id + ", counter=" + (.telemetry | iotc::find(.name == "counter").value | tostring) + ", time=" + (.telemetry | iotc::find(.name == "time").value | tostring))
    }
  }
}
else
  empty
end  

That's all at the IoT Central App. Based on the above Data transformation, any received telemetry data message from the device1 is exported to the webhook endpoint for invoking a C2D message.

In my example, for the device side is used my tool for virtual MQTT device (either the Azure IoT Hub Tester or Azure IoT Central Tester), but it can be used any other similar tool or MQTT client.

enter image description here

As you can see the above screen snippet, the device1 sent the telemetry data such as a counter and time and the device received the C2D message:

enter image description here

As the above screen snippet shows, there are three timestamps. The two highlighted timestamps are represented a round trip time (from the device to the IoT Central and back to the device). The third one is represented a timestamp at the IoT Central app:

Start device:     2022-05-19T13:50:06.4213024Z
IoT Central App:  2022-05-19T13:50:07.107Z
End device:       2022-05-19T13:50:13.6934397Z
Roman Kiss
  • 7,925
  • 1
  • 8
  • 21
  • I know the article. But there is nothing described about subscribing for IoT Hub oder IoT Edge which is helpful. I also tried to subscribe to following topic as stated in the documentation: devices/{device_id}/messages/devicebound/# Generally i want to measure the roundtrip time from sending a mqtt message until receiving the reply – ricktauss May 18 '22 at 14:24
  • @ricktauss, Have a look at my update – Roman Kiss May 19 '22 at 18:26
  • I will try to implement it that way that you explained and then respnd How did you get the subdomain for the IoT Central domain "2a92220d.......c9c5e7.azureiotcentral? Thank you! – ricktauss May 19 '22 at 21:36
  • @ricktauss, it's the Application ID, you can get it from the portal/Application/Management page or from the Api token (SharedAccessSignature sr=2a92220d-42e3-4a73-b700-2cb858c9c5e7&sig=wy...) – Roman Kiss May 20 '22 at 04:51
  • i have tried now your solution and it is exactly working with your explanation. Many thanks! – ricktauss May 21 '22 at 18:39
0

I see you are using MQTTX as a leaf device to IoT Edge in a transparent gateway pattern, where edge will just pass through messages to IoTHub.

IoTHub does not support custom topic or respond back on the same topic so this "devices/plc2/messages/events/topic" will not work

Subscription to devices/{device_id}/messages/devicebound/# will work but you will need to explicitly send C2D message for that device_id as a response

humblejay
  • 337
  • 1
  • 2
  • Ok. I also made some further testing. Publishing and Subscribing to topics which are defined in the edge manifest works well - But of course that is then only local communication. How is it possible to create a C2D message as a reply to an incoming message? The rule engine does not provide this at first glance - The solution is made with Azure IoT Central – ricktauss May 18 '22 at 20:37
  • Also for my test it does not matter if i communicate through the IoT Edge as transparent gateway, or if i communicate directly with the IoT Hub. I just want to test the round trip time with one of these communication models – ricktauss May 18 '22 at 20:45