Create Pull Request
| Date | Scan | Status | Result |
|---|---|---|---|
| 2025-07-12 23:44 | #41 | cancelled |
Biased
|
| 2025-07-12 00:58 | #8 | cancelled |
Clean
|
| 2025-07-10 05:06 | #7 | processing |
Clean
|
You can now stop running the project. Keep the console window open at this location, though, as you use this app again later in the tutorial.
## Set up the sample function app
The next step is setting up an [Azure Functions app](../azure-functions/functions-overview.md) that will be used throughout this tutorial to process data. The function app, SampleFunctionsApp, contains two functions:
* *ProcessHubToDTEvents*: processes incoming IoT Hub data and updates Azure Digital Twins accordingly
* *ProcessDTRoutedData*: processes data from digital twins, and updates the parent twins in Azure Digital Twins accordingly
In this section, you publish the prewritten function app, and ensure the function app can access Azure Digital Twins by assigning it a Microsoft Entra identity.
The function app is part of the sample project you downloaded, located in the *digital-twins-samples-main\AdtSampleApp\SampleFunctionsApp* folder.
### Publish the app
To publish the function app to Azure, you need to create a storage account, then create the function app in Azure, and finally publish the functions to the Azure function app. This section completes these actions using the Azure CLI. In each command, replace any placeholders in angle brackets with the details for your own resources.
1. Create an Azure storage account by running the following command:
The output shows information about the event subscription that you created. You can confirm that the operation completed successfully by verifying the `provisioningState` value in the result:
Save the file. Now, to see the results of the data simulation that you set up, open a new local console window and navigate to *digital-twins-samples-main\DeviceSimulator\DeviceSimulator*. >[!NOTE] > You should now have two open console windows: one that's open to the *DeviceSimulator\DeviceSimulator* folder, and one from earlier that's still open to the *AdtSampleApp\SampleClientApp* folder. Use the following dotnet command to run the device simulator project:
You should see the live updated temperatures from your Azure Digital Twins instance being logged to the console every two seconds. They should reflect the values that the data simulator is generating (you can place the console windows side-by-side to verify that the values coordinate). >[!NOTE] > It might take a few seconds for the data from the device to propagate through to the twin. The first few temperature readings might show as 0 before data begins to arrive. :::image type="content" source="media/tutorial-end-to-end/console-digital-twins-telemetry.png" alt-text="Screenshot of the console output showing log of temperature messages from digital twin thermostat67."::: Once you verify that the live temperature logging is working successfully, you can stop running both projects. Keep the console windows open, as you use them again later in the tutorial. ## Propagate Azure Digital Twins events through the graph So far in this tutorial, you see how Azure Digital Twins can be updated from external device data. Next, you see how changes to one digital twin can propagate through the Azure Digital Twins graph—in other words, how to update twins from service-internal data. To do so, you use the *ProcessDTRoutedData* Azure function to update a Room twin when the connected Thermostat twin is updated. The update functionality happens in this part of the end-to-end scenario (**arrow C**): :::image type="content" source="media/tutorial-end-to-end/building-scenario-c.png" alt-text="Diagram of an excerpt from the full building scenario diagram highlighting the section that shows the elements after Azure Digital Twins."::: Here are the actions you perform to set up this data flow: 1. [Create an Event Grid topic](#create-the-event-grid-topic) to enable movement of data between Azure services 1. [Create an endpoint](#create-the-endpoint) in Azure Digital Twins that connects the instance to the Event Grid topic 1. [Set up a route](#create-the-route) within Azure Digital Twins that sends twin property change events to the endpoint 1. [Set up an Azure function](#connect-the-azure-function) that listens on the Event Grid topic at the endpoint, receives the twin property change events that are sent there, and updates other twins in the graph accordingly [!INCLUDE [digital-twins-twin-to-twin-resources.md](includes/digital-twins-twin-to-twin-resources.md)] ### Connect the Azure function Next, subscribe the *ProcessDTRoutedData* Azure function to the Event Grid topic you previously created. This subscription allows simulated device data to flow from the thermostat67 twin through the Event Grid topic to the function, which goes back into Azure Digital Twins and updates the room21 twin accordingly. To do so, you create an Event Grid subscription that sends data from the Event Grid topic that you created earlier to your *ProcessDTRoutedData* Azure function. Use the following CLI command to create the event subscription. There's a placeholder for you to enter a name for this event subscription, and there are also placeholders for you to enter your subscription ID, resource group, the name of your Event Grid topic, and the name of your function app.
## Run the simulation and see the results Now, events should have the capability to flow from the simulated device into Azure Digital Twins, and through the Azure Digital Twins graph to update twins as appropriate. In this section, you run the device simulator again to kick off the full event flow you set up, and query Azure Digital Twins to see the live results Go to your console window that's open to the *DeviceSimulator\DeviceSimulator* folder, and run the device simulator project with `dotnet run`. Like the first time that you ran the device simulator, the project starts running and displays simulated temperature device data messages. These events are going through the flow you set up earlier to update the thermostat67 twin, and then going through the flow you set up recently to update the room21 twin to match. :::image type="content" source="media/tutorial-end-to-end/console-simulator-telemetry.png" alt-text="Screenshot of the console output of the device simulator showing temperature device data being sent."::: You don't need to do anything else in this console, but leave it running while you complete the next steps. To see the data from the Azure Digital Twins side, go to your other console window that's open to the *AdtSampleApp\SampleClientApp* folder, and run the *SampleClientApp* project with `dotnet run`. Once the project is running and accepting commands, run the following command to get the temperatures reported by both the digital twin thermostat67 and the digital twin room21.
1. Next, you zip up the functions and publish them to your new Azure function app.
1. Open a console window on your machine (if you're using the local Azure CLI, it can be the same window) and navigate into the *digital-twins-samples-main\AdtSampleApp\SampleFunctionsApp* folder inside your downloaded sample project.
1. In the console, run the following command to publish the project locally:
This command publishes the project to the *digital-twins-samples-main\AdtSampleApp\SampleFunctionsApp\publish* directory.
1. Using your preferred method, create a zip of the published files that are located **inside** the *digital-twins-samples-main\AdtSampleApp\SampleFunctionsApp\publish* directory. Name the zipped folder *publish.zip*.
>[!IMPORTANT]
>Make sure the zipped folder doesn't include an extra layer for the *publish* folder itself. It should only contain the contents that were inside the *publish* folder.
Here's an image of how the zip contents might look (it might change depending on your version of .NET).
:::image type="content" source="media/tutorial-end-to-end/publish-zip.png" alt-text="Screenshot of File Explorer in Windows showing the contents of the publish zip folder.":::
The last step is done in the Azure CLI.
1. In the Azure CLI, run the following command to deploy the published and zipped functions to your Azure function app:
Next, plug these values into the device simulator code in your local project to connect the simulator into this IoT hub and IoT hub device. Navigate on your local machine to the downloaded sample folder, and into the *digital-twins-samples-main\DeviceSimulator\DeviceSimulator* folder. Open the *AzureIoTHub.cs* file for editing. Change the following connection string values to the values you previously gathered:
The output is the list of settings for the Azure Function, which should now contain an entry called `ADT_SERVICE_URL`. ## Process simulated device data from an IoT Hub device Typically, device data from real devices drives an Azure Digital Twins graph. In this step, you connect a simulated thermostat device registered in [IoT Hub](../iot-hub/about-iot-hub.md) to the digital twin that represents it in Azure Digital Twins. As the simulated device emits data, the data is directed through the *ProcessHubToDTEvents* Azure function that triggers a corresponding update in the digital twin. In this way, the digital twin stays up to date with the real device's data. In Azure Digital Twins, the process of directing events data from one place to another is called [routing events](concepts-route-events.md). Processing the simulated device data happens in this part of the end-to-end scenario (**arrow B**): :::image type="content" source="media/tutorial-end-to-end/building-scenario-b.png" alt-text="Diagram of an excerpt from the full building scenario diagram highlighting the section that shows elements before Azure Digital Twins."::: Here are the actions that you perform to set up this device connection: 1. Create an IoT hub that manages the simulated device 2. Connect the IoT hub to the appropriate Azure function by setting up an event subscription 3. Register the simulated device in IoT hub 4. Run the simulated device and generate device data 5. Query Azure Digital Twins to see the live results ### Create an IoT Hub instance Azure Digital Twins is designed to work alongside [IoT Hub](../iot-hub/about-iot-hub.md), an Azure service for managing devices and their data. In this step, you set up an IoT hub that manages the sample device in this tutorial. In the Azure CLI, use this command to create a new IoT hub: