Transport temperature data from a Raspberry Pi to an Azure Digital Twins instance via an IoT Hub

In this article, I will demonstrate deploying a system to reflect the temperature data that an IoT device gets to a Twin belonging to an Azure Digital Twins instance.

Azure Digital Twins is a platform as a service (PaaS) offering that enables the creation of twin graphs based on digital models of entire environments.

https://learn.microsoft.com/en-us/azure/digital-twins/overview

Imagine how awesome it would be to have a replica of the natural world in virtual space!

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/8ebea724-956e-83a4-8145-a62554472d3c.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/962b4daa-5a88-276b-561e-bf2e933aef79.png)

---

## The system includes the following Azure resources.
 - An IoT Hub instance
 - A Storage Account
 - An IoT device belonging to the IoT Hub instance
 - An Event Grid instance
 - A Function App
 - An Azure Digital Twins instance
 - Two models and related twins

 ## System connectivity imagine
 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/90e33eaf-b3f0-cba0-02ca-01d9f6641339.png)

 
## Here is a library of this demo that includes the steps and helpful content.
https://github.com/kanesato/Azure-IoT/blob/main/articles/IoTDigitalTwinDemo.md

---
## Instructions
### 1. Operations on an IoT Device (Raspberry Pi + BME280 sensor)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/0346646e-312b-67e6-8ffd-a27ecf3a9212.png)

This time, I use a [BME280](https://www.amazon.co.jp/AZ-FRONT-%E3%82%BB%E3%83%B3%E3%82%B5%E3%83%BC%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB-Arduino-Raspberry-GY-BME280/dp/B0CS94KHRP/ref=sr_1_5?__mk_ja_JP=%E3%82%AB%E3%82%BF%E3%82%AB%E3%83%8A&crid=QZ57NG658AYW&dib=eyJ2IjoiMSJ9.EBZpiNRg7ouB-4gkE87VYT3Qjuszs7lgcNnvHyUoLKMOXEJQrTIMBTFDxif58L5GcoVp3mdLMxFAMwrLxawZHRIP9xdkW8tcxI8GxZaUZlyMwJ2QnPBFwBS8mRreh3Vl1EPqU5qWWPkNSCbJ7ujBmvUF50qPczJsSZjXjxo-gvMe1nxMHxz3tK6qodQopzqt4oe-zL4oe-iJZe9thdC6ZrtIhzWaA95HC4UcGpSGXGl-r6Y4JtiIEoVVKiROIr5We74A3z5-55GT0pEcqbCjcUR80kgibxbt-qKiL1OgoE0.fQPj_btn5RyBRbFy6kaNR9S5GKzNmWBbHTaaux9r3nA&dib_tag=se&keywords=bme280&qid=1713517007&sprefix=bme280%2Caps%2C167&sr=8-5) sensor to get the temperature date and use a [Raspberry Pi](https://www.raspberrypi.com/) as an IoT Device.

The following site provides instructions on connecting a BME280 sensor with a Raspberry Pi. After finishing the connectivity, A script is necessary to get the temperature data and send it to an IoT Hub instance as telemetry data. A sample Python script is also available on the following site.

https://randomnerdtutorials.com/raspberry-pi-bme280-python/

Also, here is the Python script that I'm using.
In the script code, remember to update the IoT Hub device's _CONNECTING_STRING_.　
<font color="Red"> ※The _CONNECT_STRING_ will be created later.</font>

https://github.com/kanesato/Azure-IoT/blob/main/articles/Resources/RaspberryPi/iothub-device.md

### 2. Operations on an IoT Hub and a Storage Account
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/5ea3a8bd-db9e-fc8c-d727-63b0bdac07c4.png)

### 2-1. Deploy an IoT Hub instance.
Use the "Free" price tier because the daily message limit is enough for the demo.

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/690626a2-1924-b0e9-4b39-a6f201fc061b.png)


Use [Public access] in this demo.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/a250e90b-5dc1-7c85-c47a-347395c29a32.png)

Turn on the [IoT Hub Data Contributor role].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/4f6a0a19-2aff-7dfa-6b9e-7de63e46327c.png)

 Accept the default value in the [Add-ons] and [Tags] tabs.
 Click [Create].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/646d30de-9e02-39f6-869f-0f1e2b2e316f.png)


### 2-２. Deploy an IoT device in the IoT Hub.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/aa9e8544-1455-73d6-d63e-34946bcc3048.png)

!!! _The device name must be the same as the twin's._ !!!
<font color="Red"> ※The twin will be deployed later </font>
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/d2333c2f-23a4-ae4b-92cb-908cf15121d4.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/f6498f8a-6d1d-19c3-981b-9cd22333eefb.png)

### 2-3. Get the primary CONNECT_STRING.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/c5a3bd18-996f-5f7a-3041-2ee836236d0a.png)

### 2-4. Confirm the connectivity between the Raspberry Pi and the IoT device.

Update the Python Script with the CONNECT_STRING.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/49e5180f-7ac3-4e41-6b6a-572c200a7ca6.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/8326d4e4-7849-ab3c-e04a-60291a13fcc2.png)

Run the script to confirm the temperature data is sent to the IoT device properly.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/3c25f0a2-f6ee-6123-c72c-9d08735e41f3.png)

Open the IoT Hub's overview page.
Set the [Show data] period to "1 hour", and confirm charts that the message usage is more than 0.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/5ab507e0-f6de-4067-b37a-f917c1538b1c.png)


### 2-5. Deploy a [Message routing] to store the temperature data in a blob container.

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/82869377-5481-e465-9f14-cda7e97a6d2c.png)

Choose the [Endpoint type] as "Storage".
Set a value in the [Endpoint name].
Choose a blob container from an existing storage account or create a new one.
Set the [Encoding] as "JSON".
Click [Create + next].

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/54a61a68-6170-f917-f4dc-e6b3e45c49ae.png)

Set a value in the [Name].
Click [Create + skip enrichments].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/46340f96-4099-a634-b1fd-90e7d5ffa3b7.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/6863c6ef-f16b-3ed1-58fc-43f60fae156d.png)

### 2-6. Confirm the temperature data is stored in the blob container.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/9b4a2886-9f0b-3e61-73f1-646be666b44d.png)

### 3. Operations on the Azure Digital Twins
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/31e08137-179c-62d1-eab5-66ee7fc56b60.png)

### 3-1. Deploy an Azure Digital Twins instance.

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/6f1728a1-fdad-754d-63db-72e15da8a900.png)

Turn on the [Assing Azure Digital Twins Data Owner Rolo] to assign the role to your active account.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/1c8d7db2-13ee-f4d6-4ed8-708108edd25b.png)

Allow Internet access since we use it in this demo, but private access is also available when choosing [Private endpoint].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/3aa5918a-adca-4739-73e6-661080d39256.png)

Keep the [System managed identity] off since we don't connect the Azure Digital Twins to other Azure services. Remember to turn this option on to use the Managed ID to access other resources and services.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/39088e0c-e9e5-a350-f938-1cc2a88b736b.png)

Create it.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/4d9271fe-394d-cc5a-28d0-4358e52a9cc2.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/dbd3d740-4755-0a44-d14b-20a36d7501a9.png)

### 3-2. Open the Azure Digital Twins Explorer.

Confirm the Azure Digital Twins instance deployed properly and click it to open the overview page.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/60eaeb5c-9fb8-1caa-710b-1dd4377ea149.png)

Click [Open Azure Digital Twins Explorer] to open it.
!!! It takes approximately 1 minute on my laptop to completely open the explorer, so be patient.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/a6b6a7e2-db42-de63-1c97-785ecbe228f6.png)

### 3-3. Create two models and twins.
Digital models and twins are defined by a specific language called "Digital Twin Definition Language," AKA "DTDL."

Here is a detailed library of DTDL.
The popular version is 2 and 3.

https://github.com/Azure/opendigitaltwins-dtdl/blob/master/DTDL/v2/DTDL.v2.md

Users use DTDL to create model definition and save it as a JSON file. Then, they can upload the file to the Azure Digital Twins Explorer by clicking the [Upload] or running a command on a command prompt.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/fef58920-3a2d-5327-f33d-3b2fc14afb16.png)

In this demo, I will run commands to create two models and twins related to the models and their relationship.

 - The definition of a model named "Room" (Room.json)
 ```bash
{
    "@id": "dtmi:sample:room;2",
    "@context": "dtmi:dtdl:context;2",
    "@type": "Interface",
    "displayName": "room",
    "contents": [
      {
        "@type": "Property",
        "name": "RoomName",
        "schema": "string"
      },
      {
        "@type": "Relationship",
        "name": "contains"
      }
    ]
  }
```
- The definition of a model named "thermostat" (Thermostat.json)
<font color="Red"> ※This model (twin) will be synchronized with the IoT device, so the name of this twin must be the same as the IoT device.</font>
```bash
{
    "@id": "dtmi:sample:DigitalTwins:thermostat;1",
    "@context": "dtmi:dtdl:context;2",
    "@type": "Interface",
    "displayName": "thermostat",
    "contents": [
      {
        "@type": "Property",
        "name": "DisplayName",
        "schema": "string"
      },
      {
        "@type": "Property",
        "name": "Temperature",
        "schema": "double"
      }
    ]
  }
```

Run the command below to upload a model to a specific Azure Digital Twins instance. 
```
az dt model create -n <Azure Digital Twins instance URL> -- models <the definition file's full path>
``` 

In this demo, run the commands below to upload the two models. (Mask a part of the resources's information for security protection purposes)
```bash
az dt model create -n "adt-demo-01.api.jpe.xxxxxx.azure.net" --models /xxxx/Room.json

az dt model create -n "adt-demo-01.api.jpe.xxxxxx.azure.net" --models /xxxx/Thermostat.json
```

Confirm the models are correctly deployed.
<font color="Red"> Sometimes, clicking the [Refresh] button or refreshing the page is necessary. </font>

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/2085d0da-6a4b-43d1-b477-a07884ba2d33.png)

Then, run the command below to create a twin related to a specific model.
```
az dt twin create -n <Azure Digital Twins instance URL> --dtmi <the model's dtmi> --twin-id <Define a name for the twin>
```

In this demo, run the commands below to create the two twins. (Mask a part of the resources's information for security protection purposes)
```bash
az dt twin create -n "adt-demo-01.api.jpe.xxxx.azure.net" --dtmi "dtmi:sample:room;2" --twin-id room

az dt twin create -n "adt-demo-01.api.jpe.xxxx.azure.net" --dtmi "dtmi:sample:DigitalTwins:thermostat;1" --twin-id thermostat
```

Click [Run Query] to update the Azure Digital Twins Explorer and confirm the twins are correctly created.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/f0e257ab-fd94-61b4-f2c9-6e9f3f18021c.png)

Finally, create a relationship between [room] and [thermostat].
Run the command below to create the relationship.
```
az dt twin relationship create -n "adt-demo-01.api.jpe.xxxx.azure.net" --relationship-id <the relationship's name> --relationship <relationship's type> --twin-id <the first twin's name> --target <the second twin's name>
```

In this demo, run the command below to create the relationship.(Mask a part of the resources's information for security protection purposes)
```bash
az dt twin relationship create -n "adt-demo-01.api.jpe.xxxx.azure.net" --relationship-id adtdemorelationship --relationship contains --twin-id room --target thermostat
```

Click [Run Query] to refresh the Explorer page and confirm the relationship has been correctly created.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/1af69a7f-8765-eeaa-16ae-3238e658e184.png)

### 3-4. Initialize the "thermostat" twin's property [temperature].
<font color="Red"> We need to initialize the property [temperature] in advance to synchronize it with the IoT device.</font>

Click the twin "thermostat" to open the properties on the right side. Then, input a 1(number) to the property [temperature] and click the [save] (floppy disk mark) on the top right to save it.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/04370cfc-c507-6600-8da0-5d0d1fdd5b7c.png)

The Explorer will display a dialogue below for confirming; click [Close] to close it.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/84468590-ba3e-70ce-86eb-0b850b7765e2.png)

### 4. Operations on the Function App and Azure Digital Twins instance.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/e832aa9f-d9a8-2604-b01e-2336d57f9022.png)

Now, we have connected the Raspberry Pi with the IoT Hub and deployed an Azure Digital Twins instance with the needed models and twins.

From now on, we need a program to transport the temperature data from the IoT Hub to the Azure Digital Twins instance.

We will create a Function App and upload the transportation program to it.

### 4-1. Create a Function App.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/663363a2-0a2e-f880-c830-804ff493a13c.png)

Create a Function App that uses .NET and set the version to 8(LTS)　because we use a C program that Microsoft provides in the demo.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/d5d0699b-3107-bee0-bd2d-92cf97ac4016.png)

Choose "Windows" as the operating System.
Choose an existing "App service plan" if you have one, or choose "Consumption (Serverless) as the price tier.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/335c6f6e-3ae8-735c-68ac-d95b1c9bd9d0.png)

Set an existing storage account or create a new one.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/ed5e2ebb-c039-5a75-668e-e6ba422a041e.png)

Use the default setting of Networking.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/0f190b21-1a83-2607-f192-833ba7ea4630.png)

Turning off the [Application Insights] in this demo is no problem.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/a1612bd5-671c-7734-013e-2fbedad1bcf9.png)

Associate a Github account with the Function App is a good choice for CICD, but we will skip it in this demo.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/21ed0e5a-4001-68c9-754c-a4861d59d05a.png)

Create.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/a5831f94-8704-cf39-f476-48e60d07395f.png)

### 4-2. Upload the transportation program to the Function App.

This demo will use an MS-provided sample program that can transport temperature data. Users can also deploy programs that can transport different data.

Access the following link.

https://learn.microsoft.com/ja-jp/samples/azure-samples/digital-twins-samples/digital-twins-samples/

Click [Browse code].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/13cc3c97-2f72-5dcb-28f1-7b2fe591cfdb.png)

Download the source code as a ZIP file.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/ecd2601d-236c-c94c-c4d1-c612397d6e8f.png)

Extract the ZIP file and open a Powershell terminal.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/1cd51bcd-dd47-507c-dc9f-8c2e823982e0.png)

Move to the directory "\digital-twins-samples-main\AdtSampleApp\SampleFunctionsApp"
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/14787c7a-cf3d-e587-f9d4-00b0c6d60457.png)

Run the following command to build the program.
<font color="Red"> ※.NET SDK is necessary since the program is coded in C language.</font>

https://dotnet.microsoft.com/en-us/download/visual-studio-sdks

```bash
dotnet publish -c Release
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/9a138b44-6614-692d-3292-f43764673d15.png)

Confirm the following directory existing: "\digital-twins-samples-main\AdtSampleApp\SampleFunctionsApp\bin\Release\net7.0".

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/4d64d82b-c095-6cbf-1a63-7876b6ba2cea.png)

Run the following command to create a ZIP file of the program.
<font color="Red"> ※Replace the directory with the directory of your program.</font>
```bash
Compress-Archive -Path C:\adt-demo\digital-twins-samples-main\AdtSampleApp\SampleFunctionsApp\bin\Release\net7.0\publish\* -DestinationPath .\publish.zip
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/7c24beb0-ba7f-f5a2-dd64-7b11e9e6ce30.png)

Run the command below to upload the ZIP file to the Function App.
<font color="Red"> ※Azure CLI is necessary to run the command.</font>

https://learn.microsoft.com/en-us/cli/azure/install-azure-cli

```bash
az functionapp deployment source config-zip --resource-group <Resource group name> --name <Azure Functions Name> --src <full path of the publish.zip>
```
e.g.
```bash
az functionapp deployment source config-zip --resource-group "adt-demo" --name "adt-demo" --src "/Volumes/ExtraDisk/Github/AzureDoc/TechTips/articles/IoTRef/publiczip-to-Functions/publish.zip"
```
The prompt may tell a version issue, but it doesn't make the uploading fail.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/719091b7-d84c-c337-d727-7004d6ed0e99.png)

Confirm the program has been appropriately uploaded on the Function App overview page.
Two programs will be uploaded to the Function App, and we will use the [ProcessHubToDTEvents].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/df562e54-7113-e7ae-3d07-19ce978a1d51.png)

### 4-3. Add the Azure Digital Twins's URL as an application setting variable in the Function App.

Get the Azure Digital Twins's URL(Hostname).
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/ddf101b6-057c-45c3-7530-552f1c4b28e3.png)

Move to the Function App, open the [Configuration] page, and click [New application setting].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/d56bf03a-af00-201b-9548-f788d378ca24.png)

Set the [Name] as "ADT_SERVICE_URL" (Don't change the name) and set the [Value] as the Azure Digital Twins's URL. (Remember to add the "https://" header in front of the URL)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/8600007c-0675-c915-500f-02b8ed190806.png)

Confirm the variable has been properly created and click [Save].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/c925ff06-92ed-97b3-4b8f-5ede4ef5b2a2.png)

### 4-4. Assign the [Azure Digital Twins Data Owner] role to the Function App.

The Function App needs the role to access the Azure Digital Twins instance.
We will turn on the Function App's Managed ID and assign the role to it.

Open the [Identity] in the Function App's [Setting] menu.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/59c72f19-b1bb-c45e-df2f-8886bfee22e5.png)

Turn on the [Sysmem assigned]'s status and Click [Save].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/8bcdac86-c6f5-23bc-269a-6eb2715bbe52.png)

Click [Yes].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/8aa57378-0b02-3c9b-53dc-bec0a6f808a7.png)

Move to the Azure Digital Twins overview and open the [Access control(IAM)] page.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/c07b614a-1f8e-abb1-b5a9-7b10716bee28.png)

Click [Add] → [Add role assignment].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/25ee47ff-47ed-4c44-4459-a93585e2c464.png)

Search "digital" and choose the [Azure Digital Twins Data Owner].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/7fe05848-51a8-f526-2ca1-ee5c56b706fc.png)

Follow the flow below to choose the Function App as a member.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/808aed5c-6120-88c6-1934-0428ca9d4bc8.png)

↓
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/d28c88d9-03c3-45dd-f1f7-653c06d2fe86.png)

Click [Review + assign].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/3837b71b-3f08-0938-8cb9-1e7c63032284.png)

### 5. Operations on an Event Grid instance.

Now, we have the program for transporting the temperature data.
There is still one more thing we need to do, which is to deploy an Event-driven trigger. The trigger starts the program in the Function App when the IoT Hub receives telemetry data from an IoT device and then triggers the trigger.

Select the [System topics] from the [Azure service events] menu for Event Grid and click [Create].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/af114361-0d91-2592-0c9f-84d781508f7f.png)

Select the [Topic Types] as "Azure IoT Hub Accounts" and choose the IoT Hub instance created in the previous step.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/ccc49eaa-ed51-a994-860f-608687f37325.png)

Enable the system-assigned identity to allow this resource to access the IoT Hub instance.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/345f0836-5ec8-4c8a-d9c9-652644561252.png)

Click [Create].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/99e6d3dc-9fa5-14f4-3198-26b4b43bd939.png)

The system topic trigger can now receive commands from the IoT Hub. Then, deploy an [Event Subscription] to let the trigger be able to start the program in the Function App.

Click the system topic trigger created just now.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/969bd929-e4de-50f9-df10-f0988ac15188.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/a3f908f6-b04e-4a22-e2aa-7cdf1d617610.png)

Select "Event Grid Schema" as the [Event Schema], and only check "Device Telemetry" in the [Filter to Event Types].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/6b472763-fb9a-8947-326b-062d6127bdb7.png)

Select "Azure Function" in the [Endpoint Type] and follow the steps below to set the endpoint. In the Function App, make sure to select the correct function, "ProcessHubToDTEvent."
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/ca42f42c-d34b-106e-08aa-9cf8316637e9.png)

Click [Create].
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/5fb97afe-c19c-ad11-8099-f0b00ce37157.png)

Confirm the event subscription has been properly created.
If your IoT device (Raspberry Pi) continuously sends telemetry data to the IoT Hub. Then, you may find out that the event subscription is already working on sending data to the Function App.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/ec0aa845-34c7-f1a6-8d3c-d435cec570e4.png)

### 5. Confirm the temperature data is being correctly sent to the Azure Digital Twins.

Still, remember this page? 
This is the Azure Digital Twins Explorer page. We previously initialized the property [temperature] with a 1(number).
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/cd8234d0-2f9f-b50c-7810-3e14b80b6a8b.png)

Click the [Run Query] button to refresh.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/51275786-a81a-b325-5f0e-205be2a9c2aa.png)

See!? The value of [temperature] has been updated with the current temperature.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/29728daf-701e-4a41-14b2-3d6c0bf61698.png)

When the [temperature] value is compared to the IoT device's data, there is an approximately 5-second delay. (The interval of sending data is three seconds)

It need to be better for real-time indication but more than enough for a near real-time model.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2985687/59909d80-74cb-5ad6-46f2-a307666982f2.png)

---------
It is all for this demo.

Gathering data and showcasing them not just as a list but as models with relationships and images is fantastic.

Azure Digital Twins also provides a 3D-model environment to showcase the data. I will discuss this in another article.

See you soon!