Azure Back To School: Developing with Digital Twins

Sep 24, 2021 |

This post will be a companion to the video that is going to be featured in the Azure Back To School event.

Hello Everyone!

Thanks for joining me for my post in the Azure Back to School event, which can be found here: Azure Back To School, which is an awesome event put on by my friend Captain Hyperscaler.  

For this post, I'm going to just give a general overview of the moving pieces and list out links that I used to build the video, and the post will coincide with the link to the video (available after 9/29/2021).

The demonstration video can be found here:

The Demonstration Video

To begin, A few prerequisites

1) You'll need to have the ability to run .Net code for the device simulators and the azure function to ingest data, so make sure you have VSCode or VSCommunity with .Net Core 3.1 LTS.  You can likely do this with .Net Core 6 once the new function v4 runtime is out in November-ish of 2021.

    You may also want my samples AB2S Digital Twins Demo Repository

2) You'll need a few tools

- Azure CLI (could be optional): Install the Azure CLI 

- The DTDL Validator: DTDL Validator

- Install and use Azure IoT explorer: How to install and use the IoT Explorer

- The IoT Explorer [download link]: IoT Explorer

- The ADT Explorer: ADT Explorer

3) Some fluff you may want someday

- Azure Samples for IoT: Azure IoT Samples [C#]

With those things in place, you should be ready to follow along with my demonstration.

The rest of this post is a summary/outline for the video, to group resources and allow anyone to step through the video along with trying out a few things on your own.  Let me know if you like this format or if you'd rather I do a full walkthrough with images here so you don't have to watch the video in the future.

You should also review these before getting started:

What is an Azure Digital Twin? Read this document to find out!

What is the ADTMI? Review this repository for more information on Azure Digital Twin Model Identifiers

What's in a model?

The Process:

1) Create an Azure Digital Twin instance at Azure [you can do this via the portal or via the CLI, your choice]: 

How to set up an ADT Instance at Azure

Reminder: Save yourself a lot of pain by including yourself as the ADT Data Owner

2) Create an IoT Hub

Create an IoT Hub

3) Create the digital models [Follow along with the video].

Ultimately, we're going to be a very bad trucking company.  In fact, we'll call ourselves WCDTrucking - for We Crash Daily Trucking.  At the end of the day, modelling a complex scenario like getting trucks, drivers, trailers, shippers, receivers, routes, and electronic logs to be coordinated is not a trivial endeavor.  To make this demo simple, we'll just be modelling the following:
a) A truck -> with an ID and a location {we won't use the location}

b) A trailer -> with an ID, a Temperature, a Location, and a Temperature alert.  {we'll simulate the temp and alert only }

c) A driver -> with an ID and a name

d) A trip -> which is a driver, a truck, and a trailer (and in the real world is much more complex).  

Solutions can be found here: My Repo

4) Validate the DTDL Models

DTDL Validator

5) Build out the IoT ADT Explorer locally

Getting started with ADT Explorer

Requires npm, run from the ADT src folder

Log in to Azure, and set the URL for your ADT instance.  You can view it locally  

az login

az dt list --resource-group "your-group" --query "[].hostName" -o tsv

npm run start

Put in the output from the command above.  Alternatively, just get it from the overview on your ADT instance at Azure.  Don't forget to preface with `https://`

6) Use the Azure Portal to explore your ADT

Note that changes locally affect changes at Azure and vice-versa.

7) Use the CLI to create a new digital twin

az dt twin create --dt-name $name --dtmi $yourModel --twin-id $modelId --properties '{ ... }'

Run a query to see your data

8) Import data from spreadsheet

[You can find the spreadsheet in the repo]

Use the import on the ADT explorer locally or in the Azure portal to import the data.

9) Import data from JSON

[Also in the repo]

Run some more queries.  Read more about how to query your ADT instance here.

10) Add devices to your IoT Hub at Azure or via the IoT Explorer or directly in the portal

How to use the Azure IoT Explorer

Azure IoT Samples - C#

11) Load up the simulators to simulate the devices by connection string.

[simulator code is also in the repo]

Send telemetry to an IoT Hub

12) Ensure your devices are simulated and you are getting data into the IoT Hub

13) Create an Azure Function app with a managed identity for authentication to the ADT instance.

[The function app is also in the repo]

How to authenticate your client with the ADT

How to Ingest data into ADT from an IoT Hub

Publish the function app.

14) Make sure to associate the function app as the data owner on the ADT instance using the managed identity

Make sure to set the system managed identity on the Azure Function App

Make sure to Add the ADT Data Owner role to the assigned roles for the Function App

Configure the function app

15) Add a setting in the function app to connect to the ADT Environment.  In the Function App Config, use:

ADT_SERVICE_URL and set it to the URL for your ADT.

Configure the function app environment variables [configuration]

16) Publish your function app code

Don't forget to deploy the function app code into the azure function app

17) Create the event grid trigger from the function app.

You need to respond to the events in order to publish the data into your ADT instance

Connect your function app to IoT Hub via EventGridTrigger

18) Open the Streaming logs for the Azure Function app

You will be able to easily see that everything is working as expected by reviewing the data as it streams.

19) Start the simulators, see the events streaming in, check for errors

20) Assuming everything is working, review the digital twins to see how they are updated with the real-time data from the sensors

Additional Resources:

Develop With Azure Digital Twins [MS Learn]

I hope you have found this useful and informative!  This endeavor turned out to be much more intense than I had originally planned, but has been quite fun to learn and pass on to you.  Let me know if this has helped you out!

Categories: : Azure, Digital Twins, IoT