Quickstart: Find available rooms by using Azure Digital Twins
The Azure Digital Twins service allows you to re-create a digital image of your physical environment. You can then get notified by events in your environment and customize your responses to them.
This quickstart uses a pair of .NET samples to digitize an imaginary office building. It shows you how to find available rooms in that building. With Digital Twins, you can associate many sensors with your environment. You also can find out if the air quality of your available room is optimal with the help of a simulated sensor for carbon dioxide. One of the sample applications generates random sensor data to help you visualize this scenario.
The following video summarizes quickstart setup:
If you don't have an Azure account, create a free account before you begin.
The two console applications you run in this quickstart are written by using C#. Install the .NET Core SDK version 2.1.403 or above on your development machine. If you have the .NET Core SDK installed, verify the current version of C# on your development machine. Run
dotnet --versionin a command prompt.
Download the sample C# project. Extract the digital-twins-samples-csharp-master.zip archive.
Create a Digital Twins instance
Create a new instance of Digital Twins in the portal by following the steps in this section.
Sign in to the Azure portal.
From the left pane, select Create a resource. Search for digital twins, and select Digital Twins (preview). Select Create to start the deployment process.
In the Digital Twins pane, enter the following information:
Resource Name: Create a unique name for your Digital Twins instance.
Subscription: Choose the subscription that you want to use to create this Digital Twins instance.
Resource group: Select or create a resource group for the Digital Twins instance.
Location: Select the closest location to your devices.
Review your Digital Twins information, and then select Create. Your Digital Twins instance might take a few minutes to be created. You can monitor the progress in the Notifications pane.
Open the Overview pane of your Digital Twins instance. Note the link under Management API.
The Management API URL is formatted as
https://yourDigitalTwinsName.yourLocation.azuresmartspaces.net/management/swagger. This URL takes you to the Azure Digital Twins REST API documentation that applies to your instance. Read How to use Azure Digital Twins Swagger to learn how to read and use this API documentation.
Modify the Management API URL to this format
https://yourDigitalTwinsName.yourLocation.azuresmartspaces.net/management/api/v1.0/. Your application will use the modified URL as the base URL to access your instance. Copy this modified URL to a temporary file. You'll need this in the next section.
Set permissions for your app
This section registers your sample application to Azure Active Directory (Azure AD) so that it can access your Digital Twins instance. If you already have an Azure AD app registration, reuse it for your sample. Make sure that it's configured as described in this section.
In the Azure portal, open Azure Active Directory from the left pane, and then open the Properties pane. Copy the Directory ID to a temporary file. You'll use this value to configure a sample application in the next section.
Open the App registrations pane, and then select the New application registration button.
Give a friendly name for this app registration in the Name box. Choose Application type as Native, and Redirect URI as
https://microsoft.com. Select Create.
Open the registered app, and copy the value of the Application ID field to a temporary file. This value identifies your Azure Active Directory app. You'll use the application ID to configure your sample application in the following sections.
Open your app registration pane. Select Settings > Required permissions, and then:
a. Select Add on the upper left to open the Add API access pane.
b. Select Select an API and search for Azure Digital Twins. If your search doesn't locate the API, search for Azure Smart Spaces instead.
c. Select the Azure Digital Twins (Azure Smart Spaces Service) option and choose Select.
d. Choose Select permissions. Select the Read/Write Access delegated permissions check box, and choose Select.
e. Select Done in the Add API access pane.
f. In the Required permissions pane, select the Grant permissions button, and accept the acknowledgement that appears.
Build the occupancy application by following these steps.
- Open a command prompt. Go to the folder where your
digital-twins-samples-csharp-master.zipfiles were extracted.
- Edit appSettings.json to update the following variables:
- ClientId: Enter the Application ID of your Azure AD app registration, noted in the preceding section.
- Tenant: Enter the Directory ID of your Azure AD tenant, also noted in the previous section.
- BaseUrl: The Management API URL of your Digital Twins instance is in the format
https://yourDigitalTwinsName.yourLocation.azuresmartspaces.net/management/api/v1.0/. Replace the placeholders in this URL with values for your instance from the previous section.
This step provisions your Digital Twins spatial graph with:
- Several spaces.
- One device.
- Two sensors.
- A custom function.
- One role assignment.
The spatial graph is provisioned by using the provisionSample.yaml file.
dotnet run ProvisionSample.
The Device Login Azure CLI tool is used to authenticate the user to Azure AD. The user must enter a given code to authenticate by using the Microsoft login page. After code is entered, follow the steps to authenticate. The user must authenticate when the tool is running.
When you run this step, make sure your variables were copied properly if the following error message appears:
EXIT: Unexpected error: The input is not a valid Base-64 string ...
The provisioning step might take a few minutes. It also provisions an IoT Hub within your Digital Twins instance. It loops through until the IoT Hub shows Status=
At the end of the execution, copy the
ConnectionStringof the device for use in the device simulator sample. Copy only the string outlined in this image.
You can view and modify your spatial graph using the Azure Digital Twins Graph Viewer.
Send sensor data
Build and run the sensor simulator application by following these steps.
Open a new command prompt. Go to the project you downloaded in the digital-twins-samples-csharp-master folder.
Edit appsettings.json to update DeviceConnectionString with the previous
dotnet runto start sending sensor data. You see it sent to Digital Twins as shown in the following image.
Let this simulator run so that you can view results side by side with the next step action. This window shows you the simulated sensor data sent to Digital Twins. The next step queries in real time to find available rooms with fresh air.
When you run this step, make sure
DeviceConnectionStringwas copied properly if the following error message appears:
EXIT: Unexpected error: The input is not a valid Base-64 string ...
Find available spaces with fresh air
The sensor sample simulates random data values for two sensors. They're motion and carbon dioxide. Available spaces with fresh air are defined in the sample by no presence in the room. They're also defined by a carbon dioxide level under 1,000 ppm. If the condition isn't fulfilled, the space isn't available or the air quality is poor.
Open the command prompt you used to run the previous provisioning step.
dotnet run GetAvailableAndFreshSpaces.
Look at this command prompt and the sensor data command prompt side by side.
One command prompt sends simulated motion and carbon dioxide data to Digital Twins every five seconds. The other command reads the graph in real time to find out available rooms with fresh air based on random simulated data. It displays one of these conditions in near real time based on the sensor data that was sent last:
Available rooms with fresh air.
Occupied or poor air quality of the room.
To understand what happened in this quickstart and what APIs were called, open Visual Studio Code with the code workspace project found in digital-twins-samples-csharp. Use the following command:
The tutorials go deep into the code. They teach you how to modify configuration data and what APIs are called. For more information on Management APIs, go to your Digital Twins Swagger page:
|YOUR_INSTANCE_NAME||The name of your Digital Twins instance|
|YOUR_LOCATION||Which server region your instance is hosted on|
Or for convenience, browse to Digital Twins Swagger.
Clean up resources
The tutorials go into detail about how to:
- Build an application for facility managers to increase occupant productivity.
- Operate the building more efficiently.
To continue to the tutorials, don't clean up the resources created in this quickstart. If you don't plan to continue, delete all the resources created by this quickstart.
Delete the folder that was created when you downloaded the sample repository.
From the menu on the left in the Azure portal, select All resources. Then select your Digital Twins resource. At the top of the All resources pane, select Delete.
If you experienced trouble deleting your Digital Twins instance, a service update has been rolled out with the fix. Please retry deleting your instance.
This quickstart used a simple scenario to show how to find rooms with good working conditions. For in-depth analysis of this scenario, see this tutorial:
Send feedback about: