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
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 new desired properties.
25
+
* Service apps that can add modules, and read and set 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.
29
29
30
30
## Prerequisites
31
31
32
-
***An IoT hub**. Some SDK calls require the IoT Hub primary connection string, so make a note of the connection string.
33
-
34
-
***A registered device**. Some SDK calls require the device primary connection string, so make a note of the connection string.
32
+
* An IoT hub
33
+
* An IoT hub device
34
+
*An IoT hub device module identity
35
35
36
36
* If your application uses the MQTT protocol, make sure that **port 8883** is open in your firewall. The MQTT protocol communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see [Connecting to IoT Hub (MQTT)](../iot/iot-mqtt-connect-to-iot-hub.md#connecting-to-iot-hub).
Copy file name to clipboardExpand all lines: includes/iot-hub-howto-module-twins-dotnet.md
+12-4Lines changed: 12 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,10 +42,13 @@ using Microsoft.Azure.Devices.Shared;
42
42
43
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.
44
44
45
-
Connect to the device using the [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.client.moduleclient.createfromconnectionstring) method with the module connection string. `CreateFromConnectionString` connects using the default AMQP transport.
45
+
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
+
48
+
Calling `CreateFromConnectionString` without a transport parameter connects using the default AMQP transport.
The Azure IoT SDK for .NET provides a working sample of a device app that handles module identity twin tasks. For more information, see [TwinSample](https://github.com/Azure/azure-iot-sdk-csharp/tree/main/iothub/device/samples/getting%20started/TwinSample).
134
+
The Azure IoT SDK for .NET provides working samples of device apps that handle module identity twin tasks. For more information, see:
@@ -163,6 +169,8 @@ The SDK methods in this section require these shared access policy permissions:
163
169
164
170
As a parameter to `CreateFromConnectionString`, supply a shared access policy connection string that includes these permissions. For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
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.
201
209
202
-
The `Module` class includes [properties](/dotnet/api/microsoft.azure.devices.shared.twin?&#properties) that correspond to sections of an identity module twin. Use the `Module` class properties to view and update module identity twin fields. You can use the `Module` object properties to update multiple fields before writing the updates to the device using `UpdateModuleAsync`.
210
+
The `Module` class includes `properties` that correspond to sections of an identity module twin. Use the Module class properties to view and update module identity twin fields. You can use the `Module` object properties to update multiple fields before writing the updates to the device using `UpdateModuleAsync`.
203
211
204
212
After making module identity twin field updates, call [UpdateModuleAsync](/dotnet/api/microsoft.azure.devices.registrymanager.updatemoduleasync) to write `Module` object field updates back to a device. Use `try` and `catch` logic coupled with an error handler to catch incorrectly formatted patch errors from `UpdateModuleAsync`.
@@ -22,8 +22,8 @@ Device applications can read and write module identity twin reported properties,
22
22
This section describes how to use the [azure-iot-device](/javascript/api/azure-iot-device) package in the Azure IoT SDK for Node.js to create a device application to:
23
23
24
24
* Retrieve a module identity twin and examine reported properties
25
-
* Update reported module identity twin properties
26
-
* Receive notice of module desired property changes
25
+
* Update module identity reported twin properties
26
+
* Receive notice of module identity twin desired property changes
For more information about the differences between MQTT, AMQP, and HTTPS support, see [Cloud-to-device communications guidance](../articles/iot-hub/iot-hub-devguide-c2d-guidance.md) and [Choose a communication protocol](../articles/iot-hub/iot-hub-devguide-protocols.md).
57
57
58
-
### Create a client module
58
+
### Create a client object
59
59
60
-
Create a `Client`module using the installed package.
60
+
Create a `Client`object using the installed package.
61
61
62
62
For example:
63
63
64
64
```javascript
65
65
constClient=require('azure-iot-device').Client;
66
66
```
67
67
68
-
### Create a protocol module
68
+
### Create a protocol object
69
69
70
-
Create a `Protocol`module using an installed transport package.
70
+
Create a `Protocol`object using an installed transport package.
let client =Client.fromConnectionString(deviceConnectionString, Protocol);
92
92
```
93
93
94
94
### Open the connection to IoT Hub
95
95
96
-
Use the [open](/javascript/api/azure-iot-device/client?#azure-iot-device-client-open) method to open a connection between an IoT device and IoT Hub.
97
-
Use `.catch(err)` to catch an error and execute handler code.
96
+
Use the [open](/javascript/api/azure-iot-device/client?#azure-iot-device-client-open) method to open connection between an IoT device and IoT Hub.
98
97
99
98
For example:
100
99
101
100
```javascript
102
-
client.open() //open the connection
103
-
.catch((err) => {
104
-
console.error('Could not connect: '+err.message);
105
-
});
101
+
client.open(function(err) {
102
+
if (err) {
103
+
console.error('error connecting to hub: '+ err);
104
+
process.exit(1);
105
+
}
106
+
})
106
107
```
107
108
108
109
### Retrieve a module identity twin and examine reported properties
109
110
110
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.
Use [update](/javascript/api/azure-iothub/twin?#azure-iothub-twin-update) to update device reported properties. Include a JSON-formatted patch as the first parameter and function execution status callback method as the second parameter to the method.
### Receive notice of module identity twin desired property changes
158
161
159
-
Create a desired property update event listener that executes when a desired property is changed in the device by passing the callback handler method name to [twin.on](/javascript/api/azure-iot-device/twin?#azure-iot-device-twin-on).
162
+
Create a module identity twin desired property update event listener that executes when a desired property is changed by passing the callback handler method name to [twin.on](/javascript/api/azure-iot-device/twin?#azure-iot-device-twin-on).
160
163
161
-
The desired property event listener can take one of the following forms:
164
+
The desired property event listener can take the following forms:
162
165
163
166
* Receive all patches with a single event handler
164
167
* Receive an event if anything changes under a properties grouping
@@ -237,22 +240,71 @@ You can set up a listener for a single property change. In this example, the cod
237
240
});
238
241
```
239
242
243
+
#### Complete example
244
+
245
+
This example encapsulates the principles of this section, including callback funcation nesting.
246
+
247
+
```javascript
248
+
var Client =require('azure-iot-device').Client;
249
+
var Protocol =require('azure-iot-device-amqp').Amqp;
250
+
// Copy/paste your module connection string here.
251
+
var connectionString ='HostName=xxx.azure-devices.net;DeviceId=myFirstDevice2;ModuleId=myFirstModule2;SharedAccessKey=xxxxxxxxxxxxxxxxxx';
252
+
// Create a client using the Amqp protocol.
253
+
var client =Client.fromConnectionString(connectionString, Protocol);
A backend application connects to a device through IoT Hub and can read and write device desired properties.
251
-
252
-
This section describes how to create a backend application that:
253
-
254
-
* Creates a module
255
-
* Retrieve a module identity twin and update desired properties
307
+
This section describes how to create a backend application that retrieves a module identity twin and update desired properties.
256
308
257
309
### Install service SDK packages
258
310
@@ -262,6 +314,8 @@ Run this command to install **azure-iothub** on your development machine:
262
314
npm install azure-iothub --save
263
315
```
264
316
317
+
### Create a Registry object
318
+
265
319
The [Registry](/javascript/api/azure-iothub/registry) class exposes all methods required to interact with module identity twins from a backend application.
266
320
267
321
### Connect to IoT hub
@@ -276,70 +330,60 @@ The SDK methods in this section require these shared access policy permissions:
276
330
As a parameter to `CreateFromConnectionString`, supply a shared access policy connection string that includes these permissions. For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
277
331
278
332
```javascript
279
-
var iothub =require('azure-iothub');
280
-
var connectionString ='{IoT hub shared access policy connection string}';
281
-
var registry =iothub.Registry.fromConnectionString(connectionString);
282
-
```
283
-
284
-
### Create a module
285
-
286
-
Call [addModule](/javascript/api/azure-iothub/registry?#azure-iothub-registry-addmodule-1) to add a module to a device.
let connectionString ='{IoT hub shared access policy connection string}';
335
+
let registry =Registry.fromConnectionString(serviceConnectionString);
299
336
```
300
337
301
338
### Retrieve a module identity twin and update desired properties
302
339
303
-
You can create a patch that contains tag and desired property updates for a module identity twin.
340
+
You can create a patch that contains desired property updates for a module identity twin.
304
341
305
342
To update a module identity twin:
306
343
307
344
1. Call [getModuleTwin](/javascript/api/azure-iothub/registry?#azure-iothub-registry-getmoduletwin-1) to retrieve the device [Twin](/javascript/api/azure-iothub/twin) object.
308
345
309
-
1. Format a patch that contains the module identity twin update. The patch is formatted in JSON as described in [Twin class](/javascript/api/azure-iothub/twin). A backend service patch can contain tag and desired property updates. For more patch format information, see [Tags and properties format](/azure/iot-hub/iot-hub-devguide-device-twins#tags-and-properties-format).
346
+
1. Format a patch that contains the module identity twin update. The patch is formatted in JSON as described in [Twin class](/javascript/api/azure-iothub/twin). A backend service patch contains desired property updates. For more patch format information, see [Tags and properties format](/azure/iot-hub/iot-hub-devguide-device-twins#tags-and-properties-format).
310
347
311
348
1. Call [updateModuleTwin](/javascript/api/azure-iothub/registry?&#azure-iothub-registry-updatemoduletwin-1) to update the module identity twin with the patch.
312
349
313
350
In this example, the module identity twin is retrieved for `myDeviceId` and `myModuleId`. Then a patch is applied to the twins that contains `updateTime`, `firmwareVersion`, and `weather` information.
0 commit comments