Azure Functions - how to set up IoTHubTrigger for my IoTHub messages?
Asked Answered
B

4

9

How do I setup and configure an IoTHubTrigger correctly to trigger an Azure Function (C#) for my IoTHub messages? Where and how do I plug in my IoTHub's connection string?

Barbrabarbuda answered 10/10, 2018 at 14:58 Comment(0)
B
17

Steps using Visual Studio 2017:

  1. First make sure you have the latest version of the Azure Functions and Web Jobs Tools

enter image description here

  1. Go to File->New->Project->Azure Functions and select "IoT Hub Trigger" enter image description here

  2. Select Functions V1 or V2 (learn about there differences here). And enter an arbitrary name that will serve as key for your connection string configuration.

  3. Open local.settings.json and enter a key/value pair for your connection string:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
        "ConnectionString":  "<your connection string>"
    }
}

IMPORTANT

If using Functions V1, use your IoTHub connection string obtained in the portal from here: enter image description here

If using Functions V2, use your IoTHub's EventHub compatible endpoint obtained in the portal from here: enter image description here

  1. Now set a breakpoint in your function and hit F5. You will see your messages flowing from IoTHub to your Azure Function (assuming you have devices or simulators connected that are sending data) enter image description here

Steps using the Azure Portal

  1. Create a new Function App resource and select the EventHub Trigger template enter image description here

  2. Hit "New" for EventHub Connection and select IotHub and your desired hub enter image description here

  3. Edit and save your function code - you are now up and running!

  4. Switch to "Monitor" see your events flowing in enter image description here

More options to create IoTHub Trigger Azure Functions

a) Using VS Code with the Azure Functions Extension
b) From the command line using Azure Functions Core Tools

Barbrabarbuda answered 10/10, 2018 at 14:58 Comment(0)
D
0

I also needed to install a NuGet package Microsoft.Azure.WebJobs.Extensions.EventHubs

Delmerdelmor answered 20/5, 2020 at 17:41 Comment(0)
S
0

I would like to add, if you want to publish the function on Azure, you must add the connectionstring for the portal side

enter image description here

Studley answered 10/8, 2020 at 9:40 Comment(2)
hi, When I am publishing on VS 2017, I don't see any place to do the way you have shown in your screenshot. Any idea? I added connection string directly on portal. But the function still gives 404. It's working fine on local.Nystagmus
I don't know how to do on 2017, this screenshot is from 2019Studley
D
0

Updating the thread for cloud deployment issue. If you don't find an option to pass the connection string through the publish pop up window in Visual Studio 2022, you can provide the configuration from the Azure portal UI.

Navigate to the Azure function resource on the Azure portal and click on the Configuration under thh Settings section. Click the New Application Setting button on top and add in the connection string parameter name (same as the value set to Connection in the Azure function declaration) and provide the event hub end point connection string as value.

enter image description here

Dumbwaiter answered 7/12, 2022 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.