0
votes

Azure IoT HUB. cloud-to-device messages (MQTT, custom topic)

I have an Azure IoT Hub. Here I created a custom device. This device is connected successfully with the Azure IoT Hub. I can also receive the data from this device (device-to -cloud).

But I also want to send a message to this device.

This device uses the "MQTT protocol". I cannot change the subscribe topic and the publish topic in this device, so I have to be able to set this "customtopics" in Azure (Function App).

For this, I have created a function App (IoT Hub (Event Hub)), but I do not know how to implement the "publish and/or subscribe topic" here. All examples are about "messages/events".

run.csx

public static async void Run(EventData myIoTHubMessage, TraceWriter log)
{
    log.Info($"{myIoTHubMessage.SystemProperties["iothub-connection-device-id"]}");
    var deviceId = myIoTHubMessage.SystemProperties["iothub-connection-device-id"].ToString();
    var msg = JsonConvert.SerializeObject("{\"Values\": {\"Slave 8.Channel 1.Output\": false,");
    var c2dmsg = new Microsoft.Azure.Devices.Message(Encoding.ASCII.GetBytes(msg));

    await client.SendAsync(deviceId, c2dmsg);
}

Device configuration Device configuration

Device explorer twin test

1
have a look at the following link social.msdn.microsoft.com/Forums/en-US/… - Roman Kiss
@E75 For custom topics of MQTT you can submit a feature question via azure iot hub feedback site. - Rita Han
@Rita.Thnx, so for now this cannot be realized with an Azure IoT Hub, because it isn't a generic hub broker.. - E75
@E75 Yes, limited topics are supported as you have already found. - Rita Han
@E75 For confirming you scenario, can you use devices/{device_id}/messages/devicebound/# to subscribe and use messages/events to publish message successfully? If you can get both of them working, while do you want to specify your own topics instead of the only two out of box? - Rita Han

1 Answers

0
votes

The Azure IOT Hub is not a generic MQTT Broker. There are predefined topics for device-facing side, see more in details here.

Sending a C2D message to the device is via the service-facing endpoint based on the AMQP protocol. You should use a ServiceClient proxy from the Microsoft Azure IoT Service Client SDK (Microsoft.Azure.Devices). The following code snippet shows this part:

// create proxy
string connectionString = ConfigurationManager.AppSettings["myIoTHub"];
var client = ServiceClient.CreateFromConnectionString(connectionString);

// send AMQP message
await client.SendAsync(deviceId, c2dmsg);

On the device-facing side, a device should subscribe on the following topic filter:

devices/{device_id}/messages/devicebound/#