Save IoT hub messages that contain sensor data to your Azure blob storage
Before you start this tutorial, set up your device. In the article, you set up your Azure IoT device and IoT hub, and you deploy a sample application to run on your device. The application sends collected sensor data to your IoT hub.
What you learn
You learn how to create an Azure storage account and an Azure function app to store IoT hub messages in Azure Blob storage.
What you do
- Create an Azure storage account.
- Set up your IoT hub to route messages to storage.
What you need
- Set up your device to cover the following requirements:
- An active Azure subscription
- An IoT hub under your subscription
- A running application that sends messages to your IoT hub
Create an Azure storage account
In the Azure portal, click Create a resource > Storage > Storage account.
Enter the necessary information for the storage account:
Name: The name of the storage account. The name must be globally unique.
Account Kind: Choose
Storage (general purpose v1).
Location: Choose the same location that your IoT hub uses.
Locally-redundant storage (LRS).
Secure transfer required: Choose
Subscription: Select your Azure subscription.
Resource group: Use the same resource group that your IoT hub uses.
Pin to dashboard: Select this option for easy access to your IoT hub from the dashboard.
Prepare your IoT hub to route messages to storage
IoT Hub natively supports routing messages to Azure storage as blobs. To know more about the Azure IoT Hub custom endpoints, you can refer to List of built-in IoT Hub endpoints.
Add storage as a custom endpoint
Navigate to your IoT hub in the Azure portal.
Click Endpoints > Add.
Name the endpoint and select Azure Storage Container as the endpoint type.
Use the picker to select the storage account you created in the previous section. Create a storage container and select it, then click OK.
Add a route to route data to storage
Click Routes > Add and enter a name for the route.
Select Device Messages as the data source, and select the storage endpoint you just created as the endpoint in the route.
trueas the query string, then click Save.
Add a route for hot path telemetry (optional)
By default, IoT Hub routes all messages which do not match any other routes to the built-in endpoint. Since all telemetry messages now match the rule which routes the messages to storage, you need to add another route for messages to be written to the built-in endpoint. There is no additional charge to route messages to multiple endpoints.
You can skip this step if you are not doing additional processing on your telemetry messages.
Click Add from the Routes pane and enter a name for the route.
Select Device Messages as the data source and events as the endpoint.
trueas the query string, then click Save.
Verify your message in your storage container
Run the sample application on your device to send messages to your IoT hub.
Open Storage Explorer, click Add an Azure Account > Sign in, and then sign in to your Azure account.
Click your Azure subscription > Storage Accounts > your storage account > Blob Containers > your container.
You should see messages sent from your device to your IoT hub logged in the blob container.
Clean up resources
In this tutorial, you added a storage account, and then added routing for messages from the IoT Hub to be written to the storage account. To clean up the resources you created, you remove the routing information and then delete the storage account.
Log into the Azure portal.
Click Resource groups and select the resource group you used. The list of resources in the group is displayed.
If you want to remove all of the resources in the resource group, click Delete to delete the resource group, then follow the directions. This will remove everything in that resource group, so you're finished cleaning up the resources and can skip to the next section.
Click on the IoT hub you used for this tutorial.
In the IoT Hub pane, click Routes. Click the checkbox next to the routing rule you added, then click Delete. When asked if you're sure you want to delete that route, click Yes.
Close the Routing pane. You are returned to the Resource Group pane.
Click on the IoT hub again.
In the IoT Hub pane, click Endpoints. Click the checkbox next to the endpoint you added for the storage container, then click Delete. When asked if you're sure you want to delete the selected endpoint, click Yes.
Close the Endpoints pane. You are returned to the Resource Group pane.
Click on the storage account you set up for this tutorial.
On the Storage account pane, click Delete to remove the storage account. You are taken to the Delete storage account pane.
Type in the storage account name, then click Delete at the bottom of the pane.
You’ve successfully created your Azure storage account and routed messages from IoT Hub to a blob container in that storage account.
To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following:
- Manage cloud device messaging with iothub-explorer
- Save your Azure IoT hub messages to Azure data storage
- Use Power BI to visualize real-time sensor data from your IoT hub
- Use the Web Apps feature of Azure App Service to visualize real-time sensor data from your IoT hub
- Forecast weather by using the sensor data from your IoT hub in Azure Machine Learning
- Manage devices with iothub-explorer
- Use Logic Apps for remote monitoring and notifications