Home > Back-end >  Azure IoT Hub trigger Azure Function
Azure IoT Hub trigger Azure Function

Time:12-15

I am a newbie with Azure and I want to send a message periodically from Raspberry Pi to Azure IoT Hub then trigger Azure Function (instead of using Stream Analystic) and then save the message to database. I have seen some online tutorials but I have found that, these tutorial is old and only using C#. And I want to use Python because I am familiar with this language. Is there any updated Azure Function tutorial using Python? Or could you guy please give me an example?

Thanks in advance to those who can enlighten me!

CodePudding user response:

Based on your requirement you can follow the below steps:

  1. Create Storage Account with your resource group.

  2. Go to IoT Hub and get the connection string.

  3. Create a consumer group for IoT Hub.

  4. Install VS Code and install all the Azure related Extensions.

  5. Go to Function App in VS Code and Create a new function app.

  6. Select the Interpreter as Python.

  7. Select any one of the trigger (EventHubTrigger). Provide required inputs related to event hub.

  8. At input and output of function app, configure it accordingly to table storage as target. Refer to this documentation and below is the sample binding trigger:

     {
       "scriptFile": "__init__.py",
       "bindings": [
         {
           "name": "message",
           "type": "table",
           "tableName": "messages",
           "partitionKey": "message",
           "connection": "AzureWebJobsStorage",
           "direction": "out"
         },
         {
           "authLevel": "function",
           "type": "httpTrigger",
           "direction": "in",
           "name": "req",
           "methods": [
             "get",
             "post"
           ]
         },
         {
           "type": "http",
           "direction": "out",
           "name": "$return"
         }
       ]
     }
    
  9. Add your basic logic python code and test it.

  10. After successful execution, deploy the function to Azure from VS Code.

  11. Add local.settings.json keys and values to Configuration --> Application settings in your function app.

You can achieve your task with these steps, If you find any difficulty in any of the step, you can go through the MS documentation related to it.

  • Related