You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This article shows you how to develop two types of applications:
23
23
24
-
* Device apps that view and update reported properties and handle requests to update module desired properties.
25
-
* Service apps that can add modules, and read and set desired properties.
24
+
* Device apps that view and update module identity twin reported properties and handle requests to update desired properties.
25
+
* Service apps that can read and set module identity desired properties.
26
26
27
27
> [!NOTE]
28
28
> This article is meant to complement [Azure IoT SDKs](iot-hub-devguide-sdks.md) samples that are referenced from within this article. You can use SDK tools to build both device and back-end applications.
Copy file name to clipboardExpand all lines: includes/iot-hub-howto-module-twins-dotnet.md
+11-42Lines changed: 11 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,15 +17,13 @@ This article describes how to use the [Azure IoT SDK for .NET](https://github.co
17
17
18
18
## Create a device application
19
19
20
-
Device applications can read and write module identity twin reported properties, and be notified of desired module identity twin property changes that are set by a backend application or IoT Hub.
21
-
22
20
This section describes how to use device application code to:
23
21
24
-
* Retrieve module identity twin and examine reported properties
22
+
* Retrieve a module identity twin and examine reported properties
25
23
* Update reported module identity twin properties
26
24
* Create a module desired property update callback handler
27
25
28
-
### Add device NuGet Package
26
+
### Required device NuGet package
29
27
30
28
Device client applications written in C# require the **Microsoft.Azure.Devices.Client** NuGet package.
31
29
@@ -40,26 +38,26 @@ using Microsoft.Azure.Devices.Shared;
40
38
41
39
### Connect to a device
42
40
43
-
The [ModuleClient](/dotnet/api/microsoft.azure.devices.client.moduleclient) class exposes all the methods required to interact with module identity twins from the device.
41
+
The [ModuleClient](/dotnet/api/microsoft.azure.devices.client.moduleclient) class exposes all methods required to interact with module identity twins from the device.
44
42
45
43
Connect to the device using the [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.client.moduleclient.createfromconnectionstring) method with the module connection string.
46
-
For example:
47
44
48
45
Calling `CreateFromConnectionString` without a transport parameter connects using the default AMQP transport.
49
46
47
+
This example connects to the device using the default AMQP transport.
### Retrieve a module identity twin and examine properties
59
57
60
58
Call [GetTwinAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient.gettwinasync?#microsoft-azure-devices-client-moduleclient-gettwinasync) to retrieve the current module identity twin properties into a [Twin](/dotnet/api/microsoft.azure.devices.shared.twin?) object.
61
59
62
-
This example retrieves and displays module identity twin properties.
60
+
This example retrieves and displays module identity twin properties in JSON format.
63
61
64
62
```csharp
65
63
Console.WriteLine("Retrieving twin...");
@@ -95,7 +93,7 @@ catch (Exception ex)
95
93
96
94
### Create a desired property update callback handler
97
95
98
-
Create a desired property update callback handler that executes when a desired property is changed in the module identity twin by passing the callback handler method name to [SetDesiredPropertyUpdateCallbackAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient.setdesiredpropertyupdatecallbackasync).
96
+
Pass the callback handler method name to [SetDesiredPropertyUpdateCallbackAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient.setdesiredpropertyupdatecallbackasync) to create a desired property update callback handler that executes when a desired property is changed in the module identity twin.
99
97
100
98
For example, this call sets up the system to notify a method named `OnDesiredPropertyChangedAsync` whenever a desired module property is changed.
101
99
@@ -140,12 +138,9 @@ The Azure IoT SDK for .NET provides working samples of device apps that handle m
140
138
141
139
The [RegistryManager](/dotnet/api/microsoft.azure.devices.registrymanager) class exposes all methods required to create a backend application to interact with module identity twins from the service.
142
140
143
-
This section describes how to create backend application code to:
141
+
This section describes how to read and update module identity fields.
144
142
145
-
* Add a module
146
-
* Read and update module fields
147
-
148
-
### Add service NuGet Package
143
+
### Required service NuGet package
149
144
150
145
Backend service applications require the **Microsoft.Azure.Devices** NuGet package.
Call [AddModuleAsync](/dotnet/api/microsoft.azure.devices.registrymanager.addmoduleasync) to add a module to a device.
183
-
184
-
In this example, the code calls `AddModuleAsync` to add a module named **myFirstModule** to a device named **myFirstDevice**. if the module already exists, the code calls [GetModuleAsync](/dotnet/api/microsoft.azure.devices.registrymanager.getmoduleasync) to fetch the module data into a [Module](/dotnet/api/microsoft.azure.devices.module) object.
Call [GetModuleAsync](/dotnet/api/microsoft.azure.devices.registrymanager.getmoduleasync) to retrieve current module identity twin fields into a [Module](/dotnet/api/microsoft.azure.devices.module) object.
Copy file name to clipboardExpand all lines: includes/iot-hub-howto-module-twins-node.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -110,7 +110,7 @@ client.open(function(err) {
110
110
111
111
Call [getTwin](/javascript/api/azure-iot-device/client?#azure-iot-device-client-gettwin-1) to retrieve current module identity twin information into a [Twin](/javascript/api/azure-iot-device/twin) object.
112
112
113
-
Device code can then access the twin properties.
113
+
Device code can then access the module identity twin properties.
114
114
115
115
For example:
116
116
@@ -306,7 +306,7 @@ The Azure IoT SDK for Node.js provides working samples of a device apps that han
306
306
307
307
This section describes how to create a backend application that retrieves a module identity twin and update desired properties.
308
308
309
-
### Install service SDK packages
309
+
### Install service SDK package
310
310
311
311
Run this command to install **azure-iothub** on your development machine:
### Retrieve a device twin and examine reported properties
81
+
### Retrieve a module identity twin and examine reported properties
82
82
83
-
Call [get_twin](/python/api/azure-iot-device/azure.iot.device.iothubmoduleclient?#azure-iot-device-iothubmoduleclient-get-twin) to retrieve the module identity twin from the Azure IoT Hub service. The twin information is placed into a variable that can be printed or examined.
83
+
Call [get_twin](/python/api/azure-iot-device/azure.iot.device.iothubmoduleclient?#azure-iot-device-iothubmoduleclient-get-twin) to retrieve the module identity twin from the Azure IoT Hub service. The twin information is placed into a variable that can be examined.
84
84
85
85
This example retrieves the device twin and uses the `print` command to view the device twin in JSON format.
You can apply a patch to update module identity twin reported properties in JSON format.
97
97
@@ -100,8 +100,6 @@ To apply a patch to update reported properties:
100
100
1. Assign a reported property JSON patch to a variable.
101
101
1. Call [patch_twin_reported_properties](/python/api/azure-iot-device/azure.iot.device.iothubmoduleclient?#azure-iot-device-iothubmoduleclient-patch-twin-reported-properties) to apply the JSON patch to reported properties.
102
102
103
-
If `patch_twin_reported_properties` returns an error, this function raises the corresponding error.
104
-
105
103
For example:
106
104
107
105
```python
@@ -112,7 +110,7 @@ print("Setting reported temperature to {}".format(reported_properties["temperatu
Call [on_twin_desired_properties_patch_received](/python/api/azure-iot-device/azure.iot.device.iothubmoduleclient?#azure-iot-device-iothubmoduleclient-on-twin-desired-properties-patch-received) to create a handler function or coroutine that is called when a module identity twin desired properties patch is received. The handler takes one argument, which is the twin patch in the form of a JSON dictionary object.
118
116
@@ -148,10 +146,7 @@ The Azure IoT SDK for Python includes the following samples:
148
146
149
147
## Create a backend application
150
148
151
-
This section describes how to create a backend application to:
152
-
153
-
* Create a module
154
-
* Update desired properties
149
+
This section describes how to create a backend application to retrieve and update module identity twin desired properties.
155
150
156
151
The [IoTHubRegistryManager](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager) class exposes all methods required to create a backend application to interact with module identity twins from the service.
Use [create_module_with_sas](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-create-module-with-sas) to create a device identity module using a shared access key.
190
-
191
-
This example creates a new module named `myFirstModule` for device `myFirstDevice`. If the module already exists, the code calls [get_module](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-get-module) to retrieve the device identity module from IoT Hub.
### Retrieve and update module identity twin desired properties
212
183
213
-
### Update desired properties
184
+
You can update desired properties from a backend application using [update_module_twin](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-update-module-twin).
214
185
215
-
You can update both module identity twin tags and desired properties from a backend application at the same time using [update_module_twin](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-update-module-twin).
186
+
To retrieve and update module identity twin desired properties:
216
187
217
-
1. Call [get_module_twin](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-get-module-twin) to get the current version of the module identity twin
218
-
1. Use the [Twin](/python/api/azure-iot-hub/azure.iot.hub.protocol.models.twin(class)) class to add module tags and properties in JSON format.
188
+
1. Call [get_module_twin](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-get-module-twin) to get the current version of the module identity twin.
189
+
1. Use the [Twin](/python/api/azure-iot-hub/azure.iot.hub.protocol.models.twin(class)) class to add desired properties in JSON format.
219
190
1. Call `update_module_twin` to apply the patch to the device twin. You can also use [replace_module_twin](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-replace-module-twin) to replace desired properties and tags for a module identity twin.
220
191
221
192
This example updates the `telemetryInterval` desired property to `122`.
0 commit comments