How do you send data back to device using Azure Functions?

Ayub Lakhani 1 Reputation point


The scenario is this. I am sending data from device001 to the IoT Hub using MQTT. Once the data is in the IoT Hub, it proceed to the Stream Analytics block to be processed. This is where I am confused. Once processed, I have it going to a Service Bus Queue which has a Azure Function Trigger with it. I am trying to use the Azure Function to send data to device002 using MQTT.

I try using the portal to create a Azure Function Service Bus Queue Trigger, but when I try to adjust the code, I can't use any packages like Microsoft.Azure.Devices, etc.. What is the efficient/correct approach to send data from the cloud to the device?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,605 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,602 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sander van de Velde | MVP 31,106 Reputation points MVP

    Hello @Ayub Lakhani ,

    it seems your devices are connected to the IoT Hub without using any Device SDK, just plain MQTT.

    The IoT Hub has you covered, as long as your devices are capable to work with cloud-to-device communication like Cloud messages, Direct Methods, or Device Twin changes (as in desired properties).

    Check this extensive blog post covering all these features.

    Once this is in place, you need to program an Azure function capable to execute a direct method call, a cloud message call, or a desired property update for the second device. The trigger is probably based on some logic inside Stream Analytics (or directly inside the Azure function).

    I'm not sure wht the Service Bus queue is added.

    The flow looks like:

    device (sending over MQTT) -> IoT Hub (ingest) -> Stream Analytics (decision logic) -> Azure function (Starts Direct Method call or Desired property update or cloud message) -> IoT Hub (calls device) -> Device (received command over specific MQTT topic)

    I recommend checking this learning module in advance to see how cloud-to-device communication is done.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. By doing so, all community members who have a similar issue will benefit. Your contribution is highly appreciated.

    0 comments No comments

  2. chbeier 1,866 Reputation points

    Hello @Ayub Lakhani ,

    I understand you want to send a message from device001 to IoT Hub, have the message processed in Azure Stream Analytics and then you want to use Azure Function to send the result/ a message based on the Stream Analytics result to device002.

    When you write "using MQTT", are you using the Azure IoT Device SDK or a native MQTT client (like paho, mosquitto, etc)?

    What type of message do you want to send to device002? Do you know already the expected max payload size, message frequency, expected durability of a message by your solution?
    IoT Hub supports several options to communicate from Cloud to a Device and they have their specific benefits and different use cases, a comparison is available in iot-hub-devguide-c2d-guidance

    The "classic way" (oldest implementation in IoT Hub) is using Cloud-to-device messages. Saying this, I still would recommend considering and selecting the best option for your specific use case. You might even combine several options in one solution.

    Based on the tag I assume you are using a .Net function runtime. To use Microsoft.Azure.Devices package and access the Service Client, you can check the following page functions-bindings-register

    0 comments No comments