Skip to content

Setup Azure Cloud for Digital Twins

If you are using Digital Twins, they must be set up in addition to the set up described in Set Up Azure Cloud".

If you only want to use Edge Xrt with minimum Azure resources, you do not need to complete any of the sections describing Digital Twins.

The Azure Cloud infrastructure when using Digital Twins is illustrated below:

XRTArch

To setup Azure Cloud for Digital Twins, you must do the following

Note

The configuring of a Cloud instance of an Azure IoT Hub to support Xrt Digital Twin integration is a one-off configuration exercise. This configuration is usually done by the Azure account manager, and not by individual Twin developers.

Create a Function App

The Azure Function App includes DeviceToTwin and TwinToDevice Azure functions, which are required to transform data and send events between the IoT Hub and Digital Twins. IOTech provides an Azure Function App in the twins/functions directory of the xrt-examples repository, which must be cloned or downloaded before deploying. The Azure Function App can be deployed in the following ways:

IOTech provides an example Azure Function App, as described in Use the Example Code.

Deploy Using Visual Studio Code

To deploy using Visual Studio Code, you must install the following:

To deploy using Visual Studio Code, complete the following steps:

  1. Open the AzureSphere/twin/functions Azure Function App Project, available from the examples, with Visual Studio Code
  2. Select the Azure menu icon, which is located on the left of the Visual Studio Code editor
  3. Select the Functions sub-menu
  4. Select Deploy Function App
  5. Select Create New Function App in Azure from the drop-down menu
  6. Enter a unique name for the new Azure Function App
  7. Select the .NET Core 3.1 runtime stack
  8. Select a location

Once the Azure Function App has been created, Azure produces the following Azure Resources with the same name as the Azure Function App:

  • Storage Account Resource
  • Application Insights Resource

For future deployments, you can select the Azure Function App that you have just created to upload the Project again, instead of creating a new Azure Function App.

Deploy Using the Azure CLI

To deploy using the Azure CLI, you must install the .Net 3.1 Core SDK.

To deploy using the Azure CLI, complete the following steps:

  1. Create a storage account, using the following command, where <storage-name> is the name of the storage account, <resource-group-name> is the name of the resource group and <location> is the Region:

    az storage account create \
    --name <storage-name> \
    --resource-group <resource-group-name> \
    --kind StorageV2 \
    --location <location>
    
  2. Create a Function App with a unique name, using the following command, where <location> is the Region, <function-app-name> is the name of the Function App, <resource-group-name> is the name of the resource group and <storage-name> is the name of the storage account:

    az functionapp create \
    --consumption-plan-location <location> \
    --name <function-app-name> \
    --os-type Windows \
    --resource-group <resource-group-name> \
    --runtime dotnet \
    --functions-version 3 \
    --storage-account <storage-name>
    
  3. Change your current directory to the one used in the example, using the following command:

    cd xrt-examples/AzureSphere/twin/functions/
    
  4. Push the Azure Function App Project tiles to the Azure Function App in the Cloud,using the following command, where <function-app-name> is the name of the Function App:

    func azure functionapp publish <function-app-name>
    
  5. Create an environment variable, named ADT_SERVICE_URL, to allow the function app to locate the Digital Twin service, using the following command, where <function-app-name> is the name of the Function App, <resource-group-name> is the name of the resource group, and <digital-twin-url> is the location of the Digital Twin:

    az functionapp config appsettings set \
    --name <function-app-name> \
    --resource-group <resource-group-name> \
    --settings "ADT_SERVICE_URL=<digital-twin-url>"
    
  6. Create an environment variable, named ADT_CACHE_DURATION, to represent the number of minutes for which the Digital Twin model is stored in the function app cache before being wiped, using the following command, where <function-app-name> is the name of the Function App, and <resource-group-name> is the name of the resource group:

    az functionapp config appsettings set \
    --name <function-app-name> \
    --resource-group <resource-group-name> \
    --settings "ADT_CACHE_DURATION=5.0"
    

Create an Event Grid System Topic

To route IoT Hub events to the TwinToDevice Azure Function, you must create an Event Grid System Topic with the following:

  • Set the Topic type to Azure IoT Hub Accounts
  • Select the IoT Hub that you created in Create an IoT Hub as the Resource

For this example, complete the following steps to route IoT Hub events to the TwinToDevice Azure Function:

  1. Obtain the IoT Hub ID, using the following command, where <iot-hub-name> is the name of your IoT Hub:

    az iot hub show --name <iot-hub-name> | jq '.id'
    
  2. Create a System Topic connected to your IoT Hub Resource, using the following command, where <location> is the Region, <event-grid-system-topic-name> is the name of the of the System Topic, <resource-group-name> is the name of the resource group, and <iot-hub-id> is the IoT Hub ID:

    eventgrid system-topic create \
    --location <location> \
    --name <event-grid-system-topic-name> \
    --resource-group <resource-group-name> \
    --source <iot-hub-id> \
    --topic-type Microsoft.Devices.IoTHubs
    
  3. Create a System Topic Event Subscription to send IoT Devices DeviceTelemetry to theTwinToDevice Azure Function as follows:

    1. Collect the TwinToDevice Azure Function ID, using the following command, where <function-app-name> is the Azure Function ID and <resource-group-name> is the name of the resource group:

      az functionapp function show \
      --name <function-app-name> \
      --function-name TwinToDevice \
      --resource-group <resource-group-name> | jq '.id'
      
    2. Create a System Topic Event Subscription with the endpoint set to the ID returnedfrom the previous command, using the following command, where <event-grid-system-topic-name> is the name of the of the System Topic, <resource-group-name> is the name of the resource group, and <azure-function-id> is the ID returned from the previous command:

      az eventgrid system-topic event-subscription create \
      --name iothub-output-event \
      --system-topic-name <event-grid-system-topic-name> \
      --resource-group <resource-group-name> \
      --endpoint <azure-function-id> \
      --endpoint-type azurefunction \
      --included-event-types Microsoft.Devices.DeviceTelemetry