IoT Hub and Azure Functions using Python and Visual Studio Code

ChrisU 111 Reputation points
2022-05-03T14:42:50.71+00:00

To set the scene: I am new to coding and in my first coding/software support role having completed a diploma last year to bring about a career change. I am also new to IoT but have been tasked with delivering a IoT proof of concept to my employer. So far, I have managed to setup a couple of gateways and devices on a LoRaWAN network server, output to an Azure IoT Hub and store the data in an Azure SQL DB using a Stream analytics job.

However, the sensor environment data arrives encoded in hexadecimal format and so needs decoding. I have already set up a WebSocket output and a simple app that consumes and decodes the data to variables that you can then use/store. This is within Visual Studio Code and run from the console/terminal; I have not yet been able to create an Azure Function for this. I want to now use this decoding script I have written within an Azure function which will take the uplink data, decode the environment data, and then output these along with other data to an Azure database. The trigger being the receiving of uplink data within the IoT-Hub:

198546-image.png

In this, the decode function would replace the Stream analytics job. Would this work or would I require additional functionality?

From reading the documentation here, I think I am right in that an IoT-Hub is an Event hub? I am using Visual Studio Code and Python and I have found very little documentation on setting up Azure Functions for IoT Hub/Event Hub and those that I have found focus on C# and Visual studio. Is there any other resource that I can be pointed to that may help?

Thanks in advance

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
709 questions
Azure Event Hubs
Azure Event Hubs
An Azure real-time data ingestion service.
363 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
2,636 questions
{count} votes

Accepted answer
  1. Bruno Lucas 3,871 Reputation points MVP
    2022-05-11T10:14:13.273+00:00

    Hi @ChrisU ,

    I cloned your code and it seems correct. I managed to debug it and deploy it.

    First. the debugging locally problem could be 2 things:

    1 - Firewall. the trigger function defaults to AMQP, so, it is trying to reach tpc ports 5671 and 5672. usually I fix this by creating a firewall rule but I see couple months ago Microsoft has finally provided the option to change it to amqpwebsocket. First try adjusting the firewall. if doesn't work, try to change your host.json to this:

    {  
      "version": "2.0",  
      "extensions": {  
        "eventHubs": {  
          "transportType" : "amqpWebSockets"  
        }  
      },  
      "logging": {  
        "applicationInsights": {  
          "samplingSettings": {  
            "isEnabled": true,  
            "excludedTypes": "Request"  
          }  
        }  
      },  
      "extensionBundle": {  
        "id": "Microsoft.Azure.Functions.ExtensionBundle",  
        "version": "[2.*, 3.0.0)"  
      }  
    }  
    

    2 - Did your VS Code generated the python environment? if so, you should have a folder called ".venv"
    200961-image.png

    Regarding deployment, did you use the vs code deploy?
    200972-image.png

    I think part of it is an add-on bug. you can try to deploy without an ide: https://learn.microsoft.com/en-us/azure/azure-functions/deployment-zip-push if you prefer, but back to vs code, for now, after deployment, just make sure the azure function settings (in the cloud) match the ones on the app.seetings.json. the hub connection string must be the same name in Azure. did you grab that here?:

    201023-image.png

    The storage is not mapped in the bindings but seems to be picked as long the name is "AzureWebJobsStorage". just need to make sure the value is correct.

    200914-image.png

    Before moving into testing (and after deploying) remember you need to stop the azure function in azure when testing locally, otherwise the cloud will always grab the messages.

    200992-image.png

    To send messages for testing, I use the iot hub add-on for vs code:

    https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-tools
    https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-toolkit

    200910-image.png
    200936-image.png

    Did you add any devices? the vs code message sending add-on will expect at least one device. If you don't, just add on under the iot hub in azure before using the add-on

    The right place to check if that is running in azure is here:

    Functions > [the function you want to check] > Monitor:

    200968-image.png

    ----------

    200987-image.png

    ----------

    the logs are good for initial troubleshoot. this line writes to that log:
    logging.info('Python EventHub trigger processed an event: %s',
    event.get_body().decode('utf-8'))

    200963-image.png

    to debug locally, make sure the azure function is stopped. hit f5 or click here :

    200930-image.png

    Remember to install this if you want to use the iot hub add-on: https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-tools . It helps testing/debugging

    The confusion in VS Code is where to go to use add-ons. if you were in the devug section, to find the "send message" you need to click in the explorer:

    200944-image.png

    you should see this:

    200966-image.png
    200985-image.png
    201002-image.png

    If everything is good, you should see this:
    200918-image.png

    Would be good to first debug locally to confirm you have the correct connection strings.

    Let me know if that helps.


6 additional answers

Sort by: Newest
  1. ChrisU 111 Reputation points
    2022-05-06T13:36:23.74+00:00

    So I am still struggling on with this. I have watched quite a few tutorials and read some more of the documentation and therefore understand a bit more. I decided to try and follow the documentation for the Azure IoT bindings and triggers and run it to see what will happen and what gets logged. However, I have not got even that far and wondering whether anyone can take a look and point out where I am going wrong.

    The issue is below, I set this within function.json and took the value from that displayed within the built in endpoints:
    199669-image.png

    My code:
    inti.py:
    199680-image.png

    function.json:
    199568-image.png

    host.json:
    199723-image.png

    The connection binding was taken from here:
    199731-image.png

    No comments

  2. Matthijs van der Veer 3,736 Reputation points MVP
    2022-05-04T05:49:24.383+00:00

    You're on the right track! You could definitely replace Stream Analytics with an Azure Function, you could even use both in series if you wanted, but it seems like using the Function might be enough for your use case. Here's a link to an Event-Hub triggered Azure Function for Python. If you're not sure how to set up an Azure Function is VS Code, you can download this extension. The extension has a command to start a new Azure Functions project, which will ask you what binding you need:

    198724-image.png

    You will need to grab the Event Hub endpoint connection string from the portal to connect it up.
    198725-image.png

    You mentioned

    an IoT-Hub is an Event hub?

    That's not exactly right, an IoT Hub does expose a default Event Hub endpoint that you can grab messages off. But that Event Hub is only a small part of IoT Hub. In any case you can definitely use an Event Hub trigger in an Azure Function to retrieve the messages!