Search code examples
jsonazureazure-functionsazure-blob-storageazure-iot-hub

Writing a code to receive messages from iot hub and store them in a container in BlobStorage


I would like to write a function using function app in microsoft azure to receive messages from an iot hub convert them from base64 format to string and store them in a container in BlobStorage. Could you please help me to do this?

Thanks in advance.

Br Masoud


Solution

  • Create the Azure Function IoT Hub Trigger to receive the messages and this template comes by default when you create it. enter image description here

    1. Connection value should be provided in the local.settings.json file, which is IoT Hub Connection string, can get it from the Azure Portal > IoT Hub Resource > Buit-in endpoints under Hub Settings.
    2. Runt the function where you will see your messages flowing from IoTHub to your Azure Functions (assuming you have devices or simulators connected that are sending data).

    enter image description here

    Refer to this article for step-by-step information on receiving the messages of IoT Hub to Azure Function.

    To save those messages came from Sensors in Azure, you can use Azure Storage Account either Blob Container or Table Storage.

    Step 1: Go to IoT Hub resource > Message routing > Custom Endpoints > Add the New Endpoint for the Storage Service.

    Step 2: Give an endpoint name, pick a container created for storing the data (messages), encoding format either JSON or AVRO, File Name Format, Authentication Type, etc. in this window:

    enter image description here

    Now, we added Storage Account endpoint to route messages received by IoT Hub to the Azure Blob Container Storage.

    Final Step is to add the route for routing data to the storage account.

    Please visit these sources for detailed information:

    1. Saving IoT Hub messages to Azure Blob Storage.
    2. Saving IoT Hub Sensor Data to Azure Table Storage
    3. Official Documentation of Azure Functions IoT Hub Trigger

    Note: Azure Blob Storage is cost-effective (High Price). Basically, it is recommended this variant only for proof-of-concepts or very small, simple projects. Please refer this article for more information on using which storage account for IoT Hub Trigger to optimize cost.