Message routes and custom endpoints for device-to-cloud-messages

IoT Hub enables you to route device-to-cloud messages to IoT Hub service-facing endpoints based on message properties. Routing rules give you the flexibility to send messages where you need to go without the need for additional services or custom code. Each routing rule you configure has the following properties:

Property description
Surname The unique name that identifies the rule.
source The origin of the data stream to be acted upon. For example, device telemetry.
Condition The query expression for the routing rule that is against the message’s headers and body and determines if it is a match for the endpoint. For more information about constructing a route condition, see the  Reference – query language for device twins and jobs .
Endpoint The name of the endpoint where IoT Hub sends messages that match the condition. Endpoints should be in the same region as the IoT hub, otherwise you may be charged for cross-region writes.

Endpoints and routing (to Azure Storage Container)

IoT hub has a default built-in endpoint. You can create custom endpoints to link to other services in your subscription to the hub. IoT Hub currently supports Azure storage containers, event hubs, service bus queues, and service bus topics as custom endpoints.

When you use routing and custom endpoints, messages are only delivered to the built-in endpoint. To deliver messages to the built-in endpoint as well as to a custom endpoint, add a route did sends messages to the  events  endpoint.

This means you have to create a route and a custom endpoint in the IoT Hub to send a message to a specific destination.

In this project we used Azure Storage Container as an endpoint. The purpose of this storage container is to send the message to the end user of the Raspberry Pi Container.

Azure Storage Container / Account

Azure storage account provides a unique namespace to store and access your Azure Storage data objects.

General-purpose storage accounts

A general-purpose storage account gives you access to Azure storage services such as tables, queues, files, blobs and azure virtual machines disks under a single account.

Blob Storage Accounts

A Blob storage account is a specialized storage account for storing your unstructured data as blobs (objects) in Azure Storage. Blob storage accounts are similar to existing general-purpose storage accounts and share all the great durability, availability, scalability, and performance features that you use today including 100% API consistency for block blobs and append blobs. For applications requiring only block or append blob storage, it is recommend using Blob storage accounts.

In this project it does not matter which type of storage account you use.

Creating a Storage Account

  1. In the Azure portal, expand the menu on the left side to open the menu of services, and choose More Services. Then, scroll down to Storage, and choose Storage accounts. On the Storage Accounts window that appears, choose Add.
  2. Enter a name for your storage account.
  3. Specify the deployment model to be used: Resource Manager or ClassicResource Manager is the recommended deployment model.
  4. Select the type of storage account: General purpose or Blob storageGeneral purpose is the default.
  5. Select the replication option for the storage account: LRSGRSRA-GRS, or ZRS. The default is RA-GRS. If RA-GRS does not work, try LRS. LRS worked in this project.
  6. Select the subscription in which you want to create the new storage account.
  7. Specify a new resource group or select an existing resource group.You should use your existing resource group.
  8. Select the geographic location for your storage account. Here you should use Europe-West/ West Europe.
  9. Click Create to create the storage account.

Set up message routing

You are going to route messages to different resources based on properties attached to the message by the simulated device. Messages that are not custom routed are sent to the default endpoint (messages/events). This means in this project it was necessary to create a custom endpoint for the storage container.

To create this route to the storage account it is necessary to define an endpoint, and then set up a route for that endpoint. This is done like this:

  • In the Azure portal, click Resource Groups, then select your resource group. This tutorial uses ContosoResources. Click the IoT hub under the list of resources. This tutorial uses ContosoTestHub. Click Endpoints. In the Endpointspane, click +Add. Enter the following information:
  • Name: Enter a name for the endpoint. This tutorial uses StorageContainer.
  • Endpoint Type: Select Azure Storage Container from the dropdown list.
  • Click Pick a container to see the list of storage accounts. Select your storage account. In the following example picture the name of the storage account is contosostorage. Next, select the container. This tutorial uses contosoresults. Click Select, which returns you to the Add endpoint pane

After these steps the endpoint is created and links to the storage account.

Now a route to this endpoint had to be created. To create a route to the following:

Click Routes on your IoT hub. You’re going to create a routing rule that routes messages to the storage container you just added as an endpoint. Click +Add at the top of the Routes pane. Fill in the fields on the screen.

Name: Enter a name for your routing rule. This tutorial uses StorageRule.

Data source: Select Device Messages (in this project these are the Raspberry Pi messages) from the dropdown list.

As Query string there are following possibilities:

value Result
level=“storage“ Write to Azure Storage.
level=“critical“ Write to a Service Bus queue. A Logic App can access a Service Bus queue.
default Display this data using Power BI.

 

This means in this case we have to choose “storage” as Query string to create a route to an storage account.

Click Save. When it finishes, it returns to the Routes pane, where you can see your new routing rule for storage.

The route is now finished and whenever a message is sent from the chosen device the message will be sent to the storage account and is stored there. On the picture above you can see the blob file name format in which the blob will be saved and how it will be named.

The sent files can be found in your Microsoft Azure. Expand the menu on the left and click on Storage Accounts . Select your storage account and click on blobs on the now opened main page. A new page will open with your created storage container. Click on your container and you will find a directory in the form of the blob file name format. There are all the files saved and you can download them to see the content. Alternatively you can download the Microsoft Azure Storage Explorer and open the files with this tool.

 

Schreibe einen Kommentar