2
votes

Am working on Azure resources like Azure Service Bus, Azure Functions, IOT Hub. Here am trying to send queue messages from Azure Service Bus to IOT Hub using Azure functions and then display that messages in my local device (Cloud-To-Device). Am able to read my messages in Azure function using Service Bus Queue Trigger and tried to send them to IOT Hub as output of function. Once, when I run the Azure function "Its can sending the messages to IOT Hub as output",but it unable to send them to client device. Can you please suggest me to "How to solve this situation"

2

2 Answers

2
votes

As far as I know there is currently no way to select a Cloud to Device Message(C2D) as a Azure Functions Output. You also cannot use the Event Hub Output as it does not support C2D messages as described here.

I can think of 2 methods to accomplish C2D messaging in Azure functions:

  1. Use the Azure IoT SDK as described in this answer and shown in this channel9 video from 2017 (might be out of date).
  2. Use the Azure IoT Hub REST API. You can find general configuration options here and the API endpoint to use would be senddevicecommand.
1
votes

Unfortunately there is current no output binding for IoT Hub from Functions (you could write a new custom binding, though ;) )

To talk from a Function to your devices, you need the Azure Device Service SDK of the IoT Hub. Then you can either use Cloud-to-Device messages (asynchronous) or Direct Methods (synchronous). You can find a example of the latter in my GitHub repo here: https://github.com/sebader/iotedge-end2end/blob/master/CloudFunctions/DirectMethodCaller.cs

The important pieces being:

ServiceClient _iothubServiceClient = ServiceClient.CreateFromConnectionString(config["iothubowner_cs"]);
var methodRequest = new CloudToDeviceMethod("YourDirectMethodName", TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10));
var result = await _iothubServiceClient.InvokeDeviceMethodAsync(device, module, methodRequest).ConfigureAwait(false);

An implementation for C2D messages will look pretty much the same.