Michael Papageorge
Michael Papageorge

Reputation: 67

Azure C2D message for MQTT device to subscribe to

I have successfully connected two actual devices to Azure IoTHub (in the same Iot Hub) and would like the second device to receive the message that the first device sends. So, in a normal MQTT broker the second device just subscribes to that topic but Azure does not have a normal MQTT broker.

What I am now trying to do is write an Azure function that triggers every time a message from the first device is received in IoTHub through the Event Hub Trigger; and sends a C2D message with the received message (string) to the second device. To achieve that the second device subscribes to this topic: devices/secondDevice/messages/devicebound

Here is my function

#r "Microsoft.Azure.EventHubs"


using System;
using System.Text;

using Microsoft.Azure.EventHubs;
using Microsoft.Azure.Devices;

static ServiceClient serviceClient;
static string connectionString = ".........pXH9WI.....";  
static string targetDevice = "secondDevice";

public static async Task Run(EventData[] events, ILogger log)
{
    var exceptions = new List<Exception>();
    serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
    foreach (EventData eventData in events)
    {
        try
        {
            string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);

            // Replace these two lines with your processing logic.
            log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");
            await Task.Yield();
            var commandMessage = new Message(Encoding.ASCII.GetBytes(messageBody));
            await serviceClient.SendAsync(targetDevice, commandMessage);  //  send the message to the second device that the first device sent to IoTHub
        }
        catch (Exception e)
        {
            // We need to keep processing the rest of the batch - capture this exception and continue.
            // Also, consider capturing details of the message that failed processing so it can be processed again later.
            exceptions.Add(e);
        }
    }

    // Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure.
    if (exceptions.Count > 1)
        throw new AggregateException(exceptions);

    if (exceptions.Count == 1)
        throw exceptions.Single();
}

Here's the .json code:

{
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "events",
      "direction": "inout",
      "eventHubName": "samples-workitems",
      "cardinality": "many",
      "connection": "myIoTHub_events_IOTHUB",
      "consumerGroup": "$Default"
    }
  ]
}

I am receiving the first device's message but I am not seeing the message in the topic that the second device subscribes to. enter image description here

Any ideas? I am doing all this through the portal because I am having a lot of issues with VSCode at the moment and would like to solve this C2D thing quickly.

thanks

Upvotes: 1

Views: 284

Answers (1)

Matthijs van der Veer
Matthijs van der Veer

Reputation: 4085

The second device should subscribe with a topic filter of # as per the docs. So the topic will become:

devices/secondDevice/messages/devicebound/#

Upvotes: 1

Related Questions