Azure Digital Twins Tutorial -Getting Started With Azure Digital Twins [ A Step-by-Step Guide]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm going to show you how to develop a digital twin solution from scratch using the azure digital twin service and for demonstration i'm going to develop a digital twin for my raspberry pi here that is measuring temperature and humidity using a dht-11 sensor i'm going to begin by introducing you to the concept of a digital twin i'll then show you how to use the azure digital twins definition language to create a model and then i'll show you how to set up your azure digital twins instance on azure cloud how to use azure digital twins explorer to create and manage digital twins and then finally show you how to use iot hub and azure functions to update your twin with real sensor data quick thank you to our sponsors this video is made possible by hive mq providers of an mqtt message broker please do check them out to help support this channel my name is good siremandi teresa with industry 4 auto tv and i regularly publish industry 4.0 and internet of things tutorials on this channel so make sure to subscribe to never miss any of the videos so what is azure digital twins in a nutshell azure digital twins is a platform that enables you to create and interact with real-world entities in the digital space and more generally a digital twin is a virtual representation of a physical object or system such as a pump a car a building a factory or a production process itself the main aim with the digital twin is to create a digital copy of a physical object such that it can receive inputs from sensors gathering data from the object and because of this input which is a continuous stream of real-time data the digital copy is always in sync with its physical counterpart making it possible to apply technologies like machine learning on the copy in order to produce predictions and simulations now how do we create a digital twin let's say for example we have a compressor in effective flow as our physical object and that compressor has instruments for measuring things like motor temperature vibration and motor current if we wanted to create a digital twin for it we would first of all create a model of what our compressor looks like in terms of its structural composition its technical properties and its measured physical parameters such as the temperature current and vibration would then store that model in a digital format and then use it to create editor twin of our compressor connect the digital twin to the real world compressor using some communication mechanism and then continuously update it with data from the physical compressor and then if we wanted to create a digital twin for a separate compressor of the same type in the factory flow we would use the same model to create another instance and connect it to its physical counterpart but how do we actually create a model of a physical object well there are a number of methods that you could use but azure digital twins uses what is called the digital twins definition language or dtdl in short now while the digital twins definition language was created by microsoft for its azure platform they've since brought it to organizations such as the digital twin consortium let us take a look at how the digital twins definition language works and how we can use it to create a model for a digital twin now the digital twins definition language uses an open language called json ld which is similar to json but before we dive into the language syntax i want to first describe to you dtdl's proposition of what the representation of a physical object must be composed of now in dtdl a digital twin model is composed of four key elements properties telemetry components and relationships whereby a property represents the state of that physical entity using our compressor example a property could be its name its power rating its real number and its color properties of a model require a storage mechanism that will retain the data permanently and allow for it to be updated as required and then model telemetry is an event stream representing measurement of physical parameters and these anomaly sensor readings like motor temperature vibration and motor current however telemetry is not stored on the digital twin it is processed as it arrives and sent to an external system if required next components of a model are for when you want to include separate models as part of your model for example a motor could very well be defined as a standalone model and then used as a component of a compressor model through referencing and then we have relationships which let you define how your digital twin is related to other digital twins for example a factory flow digital twin would be related to a production line digital twin in that it contains the production line and an air compressor would be related to a control unit in that it supplies pressure to it and so on now connecting digital twins via relationships in this manner results in a set of digital twins forming what is called a twin graph a twin graph sometimes called a knowledge graph represents physical entities in your entire environment while reflecting their interactions and then finally all the elements of a digital twin model are enclosed in what is called an interface now let's take a look at how to use dtdl to create an actual digital twin model so for demonstration we're going to assume that my raspberry pi with its temperature and humidity measurements is a factory and so we will be creating a digital twin model for a factory so digital twin models can be written in any text editor and saved using the json extension in this example i'm going to use visual studio card so i've got my visual studio code open here so the first required item is the digital twin model identifier and here i have it as demo colon factory and this one here is the version of my factory model and because we're going to be enclosing all elements of our model here we specify this as interface type which is also a required item next we have the display name item which is a user-friendly way of displaying our model name and then we have the context which is used to process this interface and it must always be set to this value now here we put all of the items of my factory digital twin model inside the contents the first item is the temperature which is of type property and its schema is double the schema can be of primitive types such as integer double string or boolean and it can also be of complex types like object enum and map now here i'm using property type for temperature because i want this value to be stored on my digital twin so that we can read it later on in this demo because remember that telemetry type doesn't get stored it is processed as it arrives and if you need to react to it you send it to an external platform such as time series insights the second item is humidity which is also of type property and double schema now this is a basic model but we could add things like relationships and components so here you can see that the name of our relationship is called contains and the target is line which is a production line and then here you can see that the name of our component is a compressor but we're going to keep it simple for now you can check out the dtdl specification on github using the link in the description below for more examples of working with dtdl so this model of our factory is what we would call a custom model however you could use what are called ontologies which are basically pre-built models for specific industries ontologies are actually an ongoing effort to get a sense of what they enter you can check out digital twins definition language ontology for smart cities which have also linked down below okay so once you've finished creating your model you need to upload it to your azure digital twins instance on azure cloud to make it available for creating twins in the digital space so let's head over to my azure portal to create an azure digital twins instance okay so now i'm going to show you how to create a new azure digital twins instance on the azure portal and as you can see i'm already logged in to my azure portal now i'm going to select create a resource search for azure digital twins select it and then go ahead and click create so here i'll select my subscription and then create a new resource group so i'll call it industry 40 tv as your digital twins and then select ok then i'll select the location and then give my actuator twins instance a name so i'll call it my demo adt instance and then click on review and create and then after reviewing the details i'll select create okay so my issue twins instance is being deployed okay so my deployment is complete so i will select go to resource and then here i will take note of my azure digital twin's instance name its hostname and its resource group okay so my azure digital twins instance is now up and running okay so now because the account that i'm currently signed in with here is a microsoft account i won't be able to access my azure digital twin instance from a client application so i need to set up user access permissions for a corporate account and to do that i'll go and access control i am here and then i'll click on add add draw assignment and then here i'll select azure digital twins owner and then i'll leave this one and then here i'll use this on microsoft.com account which i normally use for my azure sphere connections okay and then you can see it appears as selected and then i'll click on save okay so if we go to role assignments here you can see that i've got my corporate email here assigned as azure digital twins data owner okay so we've successfully created our azure digital twins instance and assigned user access role that would allow us to manage our instance now when you need to interact with your azure digital twins instance you mainly use azure digital twins apis and sdks for languages such as c sharp java python javascript and go what this means is that in order to upload models to your instance delete them create digital twins using your models query or digital twins and create relationships etc you'd have to develop client applications using the apis and sdks that i've just mentioned and there are samples to help you do that however microsoft developed a simple note.js client application called azure digital twins explorer which allows us to use its graphical interface to interact with our azure digital twins instance to perform things like uploading and exploring models uploading and editing graphs of twins running queries against the twins graph and visualizing the twins graph etc with that said what i'm going to show you next is how to perform some of those functionalities against our azure digital twins instance using the azure digital twins explorer so the first thing that we need to do is to download and run the azure digital twins explorer sample code but before you download it you need to make sure that you've got nodejs version 10 or later installed on your machine ok so to download the assemble app you go to this github repo which i've also linked in the description below under code here you can select download zip so i've already downloaded this and extracted it on my c drive here now before you run the app you've got to make sure that you've signed into your azure account using your machine because the azure digital twin explorer app depends on the host machine's azure credentials the only thing that we're going to input directly on the azure digital twin explorer is the hostname of our azure digital twin instance which we noted earlier after deployment of the instance so i'll log in using my visual studio code and then i'll select my own microsoft.com account and now i'm signed into my azure account okay so let's open up our extracted folder so here we go into client and then into source we copy the path open command prompt and then cd into our source folder and then once we're here we run the npm install command to retrieve all dependencies of this sample app okay when that is done you run npm run start to start the azure digital twin explorer app okay so we have successfully run our azure digital twins explorer app okay so now i need to put the hostname of my azure digital twin instance here so i'll go to my azure portal and then here i'll copy the hostname then i'll add https here and then select save okay so what it means is that i've successfully connected my azure digital twins explorer to my azure digital twins instance now to upload your model using the azure digital twins explorer you click on this upload icon here and then browse to the json file containing your module then select open okay our model has been uploaded now you can select to see your model information by clicking this icon here and you can see that this is the model that we designed earlier okay so now we need to create our factory twin using this model and to do that you click on the plus button here so we'll call this twin factory zero four twenty 21 because the name has to be unique and then i'll serve this and then if i select the twin here on the canvas you can see its properties here so you can see here we've got an initial value of zero for our temperature and humidity which you can adjust if you wish okay so we've successfully created our factory digital twin using our factory model now in this area here you can actually run queries against your digital twins so for example if i set the humidity of my factory digital twin to say 54 and the temperature to 28 and then save okay so now here if i run a query to get the twins whose humidity is greater than 60 i get nothing as you can see no results found but if i say less than 60 i get that factory digital twin because our humidity is at 54. so i'll reset these to zero okay so now i'm going to show you how to send real sensor data to our factory digital twin so how it works is that we'll have our raspberry pi publishing messages to an iot hub and then we'll have an azure function catching these messages and ingesting them into our factory digital twin so the first step for us is to create an iot hub so let's go ahead and do that so i'll go to my azure portal and then here i'll select create a resource search for iot hub select create and then here under resource group i'll select this industry for the tv as a digital twins resource group and then i'll select the region and then i'll call my iot hub my azure digital twins iot hub and then i'll select review and create and then i'll go ahead and select create okay so my iot hub deployment is complete so i'll go ahead and select go to resource okay so now here i want to create a device so i'll select on new here now the device id here has to be the same name as our digital twin which is factory zero for 2021 so i'll go ahead and put that in and then i'll leave everything as default select save okay so my iot hub device has been successfully created now the next step is to create an azure function and to do that we're going to use visual studio so i'll go ahead and open it up so here i'll select on create a new project and then here i'll type azure functions select that and click next and then i'll call it factory twin ingest function and then select create and then here i'll select event grid trigger click on create now for my function to interact with the azure digital twins i need to use the azure digital twins sdk and to do that i'll include some nokia packages on my project so i'll include as you add to digitaltwins.com or go ahead and install that and then i'll also include azure.identity followed by system.net.http and then finally i'll add azure.com okay and then in my function one two three file i'll add the following assembly reference now we need to add our authentication code that would allow our azure functions up to access our azure digital twins instance so here as you can see we'll be reading our azure digital twin service url as an environment variable which we'll configure later on on our azure portal and then we create an instance of http client and then inside our function here we add the code for managed identity credentials and then we instantiate our azure t-shirt twins client and then after that we highlight that our connection has been created okay now if we receive data from iot hub we log the data convert the message into a json object and then we get our device id temperature and humidity from the object and then we lock the temperature and humidity and then finally we update our factory digital twin with the temperature and humidity from our raspberry pi and then we make our method asynchronous now we'll get rid of this and then enclose our logic in a try catch block and then if there's an error in ingesting into our digital twin we log the message here and then in the event that our azure digital twins instance is not configured as an environment variable we need to log an error so i'll put this statement here okay and then we can call this function iot hub to twins okay and then we can go ahead and publish this app i'll select publish to microsoft cloud click next and then here i'll leave windows as default click next so my subscription name here is already selected so i'll go ahead and click on the plus sign to create a new function so i'll leave this name here as default make sure my correct subscription is selected and then i'll select my resource group which is in the tv as your digital twins and then i'll leave my plan type as consumption then i'll select a location and then i'll create a new azure storage here select a location and leave the account type as default click ok and then i'll select create okay here i'll click finish okay and then i'll go ahead and click on publish okay so we've successfully published our function app to azure so let's head over to our azure portal to confirm that so here we'll search for factory twin and you see it pop up here so we'll go ahead and select it and then if we go under functions here you can see there is our iit hub to twins function okay now the next step is to set up security access for our azure function app in order to grant it access to our azure cloud services without using credentials in code and for that we use what is called a system assigned managed identity so to do that we go to identity here and make sure that the status is set to on select azure role assignments add a new role assignment and then here under scope i'll select resource group i'll leave my subscription as default and then my resource group is already selected for me and then here for the role i will select azure digital twins data owner and then i'll save the assignment settings okay so now as mentioned earlier we need to set an environment variable to make the url of our azure digital twins instance accessible to our function app and to do that we first need to go to our azure digital twins instance copy the hostname and then go back to our function up and then under our function up we'll select configuration and select new application settings and then here we add the name adt service url now if you remember that's the name that we used for our azure data twin instance url in the code of our function app and then under value here i'll test the host name of my azure digital twins instance and then i need to add an https here click ok ok so you can see here our adt service url is now appearing on the list so we save our configuration settings and then continue okay so we've successfully set up security access for our function app and configure its application settings okay now let's connect our function up to our iot hub and to do that we need to set up our iot hub to send device messages coming from the raspberry pi to our function up as events so i'll navigate to my iit hub instance that i created earlier under my iot hub i'll select events and then click to create an event subscription for the name i'll put att event subscription for the event schema you leave it as event grid schema and then for the topic i'll put att system topic and then for filter to event types you select device telemetry and then for our endpoint type we select azure function and then click on endpoint to select the actual endpoint and then on this page here i'll leave the subscription as default and also the resource group i'll also leave my function up as default and also the slot as production and then finally i'll make sure that the function which i named iot up to twins is selected here under function and then i'll confirm my selection and then click on create okay so it seems i need to first register event grid as a resource provider under my account okay so i'll search for subscriptions here select subscriptions then select my subscription and then here i'll select resource providers okay so as you can see most of the resource providers are registered by default but it so happens that event grid is not registered by default so you need to register it so there is our event grid if you select it and then click on register okay so now my event grid is successfully registered and appearing here on the list okay so we will go ahead and create okay and then if we go to event subscriptions you can see that our azure digital twin event subscription is now appearing okay so we've successfully connected our iot hub to our azure function app by creating an event subscription the next thing that we're going to do is to update our digital twin from our raspberry pi by sending sensor data to azure iot hub okay so here i've got my dht 11 temperature and humidity sensor data connected to my raspberry pi and my raspberry pi is running a uwp application on windows iot core which is collecting this temperature and humidity data and sending it to my iot hub so as to update the temperature and humidity properties of our factory digital twin via our function app now you can do this using python node red or whatever it doesn't matter the idea is to publish data to an iot hub that is connected to your function app so let's head over to my visual studio so that i can walk you through this shop application running on my pie okay so this is my c sharp uwp application running on my raspberry pi i'll link the source codes in the description below okay so i'm currently running in debug mode here so this is the main page i start here by calling the update fake traditional twin method which calls the method for sending sensor data to the iot hub okay so let's go to the class that does the reading and sending of sensor data okay so these here are dht11 variables i'm reading the sensor at gpr pin number four and this is my iit hub connection string and here is my device connection string so here you'll notice the id of the device which i created on my iit hub earlier called factory offer 2021 which is also the same id as that of the factory digital twin that we created from our model and then inside our method here i first set my gpio pin as an input pin i then instantiate an azure device client using my device connection string and then i read the temperature and humidity from the dht11 sensor convert the sensor values to json and then send the events to iot hub and then we repeat the cycle after every five seconds okay so now if i go to my azure portal here and then go into my iit hub you can see here that we're publishing messages to the iot hub and then if i go to events you can see here that you are registering events okay so now i'll pull up my azure digital twin explorer okay so as you can see it's currently set here at zero so if i run query we can see that we're starting to get updated information from our raspberry pi if i run query again in the game okay i'm gonna have to breathe into my sense idea to increase the temperature and humidity okay you can see was starting to detect a rise in humidity if i run query again you see now we are back to 888 so all of this information is coming from our azure digital twin okay so we have successfully created a digital twin solution for our demo factory using the azure digital twins service thanks for watching
Info
Channel: Industry40tv
Views: 12,601
Rating: undefined out of 5
Keywords: azure digital twins tutorial, azure digital twin tutorial, azure digital twin demo, azure digital twins, azure digital twin explorer, azure digital twin example, azure digital twins documentation, azure digital twins learning path, azure digital twins sla, azure digital twin vs device twin, digital twin tutorial, digital twin, digital twin technology, digital twin example, digital twins example, digital twins iot, Digital twin technology driven by iot
Id: YBwraf72BKI
Channel Id: undefined
Length: 41min 12sec (2472 seconds)
Published: Sun Apr 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.