Share via

How do I trigger a function on IoT Hub message arrival?

Haris Papageorge 251 Reputation points
2023-07-05T07:29:31.8366667+00:00

Hi,

In the past when I wanted to use a function, I just created one and the messages were shown in the terminal in the Code and Test page of that function.

When I try to do the same now, no messages are shown. I can see with the IoT Explorer that my messages are arriving fine?

So what are the steps for using the function?

thanks

Azure IoT Hub
Azure IoT Hub

An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.


2 answers

Sort by: Most helpful
  1. Sander van de Velde | MVP 37,066 Reputation points MVP
    2023-07-05T22:55:06.26+00:00

    Hello @Haris Papageorge ,

    the same question is answered here too.

    Please compare your setup with the steps seen there.

    There are two common pitfalls:

    1. An Azure Function listening to IoT Hub messages uses the 'eventhub compatible endpoint' of the IoT Hub. This endpoint will turn by default into a fallback route once custom routes are added to the IoT Hub. Notice a separate endpoint is defined to route all messages to that 'eventhub compatible endpoint'. Otherwise, typically not a single message is sent to the endpoint (because there are no fallback messages).
    2. An Azure Function listening to IoT Hub messages uses the 'eventhub compatible endpoint' of the IoT Hub. Please add a separate consumergroup (like 'AF') in the IoT Hub and use that in the Azure Function (other than $default).

    If this is setup correctly, all messages will arrive both in the IoT Explorer and the Azure Function.

    The terminal logging should still work. It's advised to activate Application Insights for more detailed debug scenarios.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    Was this answer helpful?

    0 comments No comments

  2. AshokPeddakotla-MSFT 36,021 Reputation points Moderator
    2023-07-05T15:39:25.0933333+00:00

    Haris Papageorge Greetings & Welcome to Microsoft Q&A forum!

    When I try to do the same now, no messages are shown. I can see with the IoT Explorer that my messages are arriving fine? So what are the steps for using the function?

    Did you consider using the IoT Hub trigger in Azure Functions?

    The IoT Hub trigger listens for messages sent to an IoT Hub-compatible endpoint (such as Azure IoT Hub) and automatically triggers the function when a message arrives.

    Here are the steps to create an Azure Function with an IoT Hub trigger.

    • Create a new Azure Function app in the Azure portal.
    • Add a new function to the app and select "IoT Hub (Event Hub)" as the trigger type.
    • Configure the IoT Hub connection string and other settings for the trigger.
    • Write the code for your function to process the incoming message. For more detailed instructions and examples, you can refer the documentation on Azure IoT Hub bindings for Azure Functions

    Also, see Azure IoT Hub trigger for Azure Functions

    Hope this helps.

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.