Configuring XRT Using a Device Twin
XRT Azure Sphere applications are configured using the IoT Hub Device Twin configuration mechanism. The XRT component and device service configuration are obtained using the "desired properties" in the device twin JSON document.
Desired Properties Format
The desired properties must provide component configuration and service configuration.
The component configuration consists of a JSON object named Components. The named objects within the components object will be "main" and names of components referenced by the "main" configuration. The content of the main and component objects is in the same format as a file-based XRT configuration.
The service configuration consists of a JSON object named "Services". The named objects within the services object will match the device service's "Name" configuration variable. The content of these objects will be objects named "Devices", "Profiles" and "Schedules". These three objects have the same formats as described in Device Service Components.
An example of the component configuration and service configuration is available here.
When an Azure Sphere application is first installed, the initial configuration is built into the application. The initial configuration is defined using a set of JSON configuration files in the usual XRT format. Typically, this will consist of an AzureSphere Exporter configuration and its dependant components.
The initial configuration will be used be to connect to Azure and obtain the current configuration.
When a new configuration is obtained from the device twin, it is persisted to the Azure Sphere device's mutable storage. When the Azure Sphere device boots, it will use the persisted configuration if no updated configuration is provided by the Azure digital twin.
Configuring a Digital Twin
An Azure Sphere digital twin can be configured using the Microsoft Azure web portal or using the Azure APIs. The following describes how to configure the digital twin using the Azure portal.
Login to the Azure Portal
Navigate to your IoT Hub. This can be found under the "All resource" button or might be shown in the list of "Recent Resources".
Select "Devices" in the left-hand column of the IoT Hub page.
Determine the Device ID of your Azure Sphere development board.
The Device ID can be found using the command
azsphere device show-attached
Select the device in the portal.
If the matching Device ID is not visible on the IoT Hub | Devices page you can locate it by pasting the Device ID into the "Device name" search box and clicking "Find devices".
Select the "Device twin" tab on the device page.
Edit the "properties" / "desired" object as required.
Example JSON excerpts are provided in the "twin" directory. The can be copied into the "desired" object and modified as needed.
Save the changes using the "Save" button.
If a new component configuration is provided at startup the Azure Sphere device will reboot to ensure the new configuration is applied.
If the Components or Services desired properties are updated while the Azure Sphere device is active the new configuration will be persisted and the device rebooted.
Updating the Configuration
The Azure IoT APIs can be used to update device twin configurations. A Python example can be found here.