Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 699
Stefan Wick MSFT Avatar asked Oct 10 '18 14:10

Stefan Wick MSFT


People also ask

How do I create an event hub trigger Azure function?

Azure Functions supports trigger and output bindings for Event Hubs. For information on setup and configuration details, see the overview. Use the function trigger to respond to an event sent to an event hub event stream. You must have read access to the underlying event hub to set up the trigger.

How do I enable Azure function logs?

To turn on the streaming logs for your function app in Azure: Select F1 to open the command palette, and then search for and run the command Azure Functions: Start Streaming Logs. Select your function app in Azure, and then select Yes to enable application logging for the function app. Trigger your functions in Azure.


1 Answers

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

like image 84
Stefan Wick MSFT Avatar answered Sep 19 '22 06:09

Stefan Wick MSFT