0

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

Community
  • 1
  • 1
E75
  • 153
  • 3
  • 13
  • have a look at the following link https://social.msdn.microsoft.com/Forums/en-US/668f0752-fc86-4c66-bac5-e1603c99cff2/sending-d2c-messages-on-azure-iot-hub-via-android-using-android-studio?forum=azureiothub – Roman Kiss Aug 31 '18 at 10:16
  • @E75 For custom topics of MQTT you can submit a feature question via [azure iot hub feedback site](https://feedback.azure.com/forums/321918-azure-iot). – Rita Han Sep 03 '18 at 08:58
  • @Rita.Thnx, so for now this cannot be realized with an Azure IoT Hub, because it isn't a generic hub broker.. – E75 Sep 03 '18 at 11:11
  • @E75 Yes, limited topics are supported as you have already found. – Rita Han Sep 04 '18 at 01:44
  • @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 Sep 05 '18 at 10:09

1 Answers1

0

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/#
Roman Kiss
  • 7,925
  • 1
  • 8
  • 21
  • if I understand correctly, the device subscribe topic must be changed (which in my case is not possible). if I understand correctly, the device subscribe topic must be changed in the device itself (which in my case isn't possible). So I cannot implement a custom subscribe/ publish topic in Azure IoT Hub? – E75 Aug 31 '18 at 11:16
  • document https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#using-the-mqtt-protocol-directly give you all details for using a direct MQTT protocol. – Roman Kiss Aug 31 '18 at 12:32
  • @E75 The topic to send D2C message is `devices/{deivceid}/messages/events/` and `devices/{deviceid}/messages/devicebound/#` topic is used for receiving the C2D message. At present, Azure IoT Hub doesn't support creating custom topic. Would you mind sharing the reason why you not able to change the subscribe topic on the device side? – Fei Xue Sep 06 '18 at 02:36
  • @FeiXue-MSFT in my case i am trying to use a Tasmota (sonoff) switch which use some specific topics – farooq Nov 30 '20 at 12:03