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
Copy file name to clipboardExpand all lines: articles/iot-hub/iot-hub-node-node-module-twin-getstarted.md
+24-32Lines changed: 24 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,25 +19,28 @@ ms.date: 04/26/2018
19
19
At the end of this tutorial, you have two Node.js apps:
20
20
21
21
***CreateIdentities**, which creates a device identity, a module identity and associated security key to connect your device and module clients.
22
+
22
23
***UpdateModuleTwinReportedProperties**, which sends updated module twin reported properties to your IoT Hub.
23
24
24
25
> [!NOTE]
25
-
> For information about the Azure IoT SDKs that you can use to build both applications to run on devices, and your solution back end, see [Azure IoT SDKs][lnk-hub-sdks].
26
+
> For information about the Azure IoT SDKs that you can use to build both applications to run on devices, and your solution back end, see [Azure IoT SDKs](iot-hub-devguide-sdks.md).
26
27
27
28
To complete this tutorial, you need the following:
28
29
29
-
* An active Azure account. (If you don't have an account, you can create a [free account][lnk-free-trial] in just a couple of minutes.)
30
+
* An active Azure account. (If you don't have an account, you can create a [free account](https://azure.microsoft.com/pricing/free-trial/) in just a couple of minutes.)
30
31
* An IoT Hub.
31
32
* Install the latest [Node.js SDK](https://github.com/Azure/azure-iot-sdk-node).
32
33
33
34
You have now created your IoT hub, and you have the host name and IoT Hub connection string that you need to complete the rest of this tutorial.
34
35
35
36
## Create a device identity and a module identity in IoT Hub
36
37
37
-
In this section, you create a Node.js app that creates a device identity and a module identity in the identity registry in your IoT hub. A device or module cannot connect to IoT hub unless it has an entry in the identity registry. For more information, see the "Identity registry" section of the [IoT Hub developer guide][lnk-devguide-identity]. When you run this console app, it generates a unique ID and key for both device and module. Your device and module use these values to identify itself when it sends device-to-cloud messages to IoT Hub. The IDs are case-sensitive.
38
+
In this section, you create a Node.js app that creates a device identity and a module identity in the identity registry in your IoT hub. A device or module cannot connect to IoT hub unless it has an entry in the identity registry. For more information, see the "Identity registry" section of the [IoT Hub developer guide](iot-hub-devguide-identity-registry.md). When you run this console app, it generates a unique ID and key for both device and module. Your device and module use these values to identify itself when it sends device-to-cloud messages to IoT Hub. The IDs are case-sensitive.
38
39
39
40
1. Create a directory to hold your code.
41
+
40
42
2. Inside of that directory, first run **npm init -y** to create an empty package.json with defaults. This is the project file for your code.
43
+
41
44
3. Run **npm install -S azure-iothub\@modules-preview** to install the service SDK inside the **node_modules** subdirectory.
42
45
43
46
> [!NOTE]
@@ -79,14 +82,14 @@ In this section, you create a Node.js app that creates a device identity and a m
console.log('Error getting module back from hub: '+ err);
@@ -102,26 +105,26 @@ In this section, you create a Node.js app that creates a device identity and a m
102
105
103
106
This app creates a device identity withID**myFirstDevice** and a module identity withID**myFirstModule** under device **myFirstDevice**. (If that moduleID already exists in the identity registry, the code simply retrieves the existing module information.) The app then displays the primary key for that identity. You use this key in the simulated module app to connect to your IoT hub.
104
107
105
-
1.Run this using node add.js. It will give you a connection string for your device identity and another one for your module identity.
108
+
Run this using node add.js. It will give you a connection string for your device identity and another one for your module identity.
106
109
107
-
> [!NOTE]
108
-
> The IoT Hub identity registry only stores device and module identities to enable secure access to the IoT hub. The identity registry stores device IDs and keys to use as security credentials. The identity registry also stores an enabled/disabled flag for each device that you can use to disable access for that device. If your application needs to store other device-specific metadata, it should use an application-specific store. There is no enabled/disabled flag formoduleidentities. For more information, see [IoT Hub developer guide][lnk-devguide-identity].
110
+
> [!NOTE]
111
+
> The IoT Hub identity registry only stores device and module identities to enable secure access to the IoT hub. The identity registry stores device IDs and keys to use as security credentials. The identity registry also stores an enabled/disabled flag for each device that you can use to disable access for that device. If your application needs to store other device-specific metadata, it should use an application-specific store. There is no enabled/disabled flag formoduleidentities. For more information, see [IoT Hub developer guide](iot-hub-devguide-identity-registry.md).
109
112
110
113
## Update the module twin using Node.js device SDK
111
114
112
115
In this section, you create a Node.js app on your simulated device that updates the module twin reported properties.
113
116
114
-
1.**Get your module connection string**--now if you login to [Azure portal][lnk-portal]. Navigate to your IoT Hub and click IoT Devices. Find myFirstDevice, open it and you see myFirstModule was successfully created. Copy the module connection string. It is needed in the next step.
117
+
1.**Get your module connection string**--Sign into the [Azure portal](https://portal.azure.com/). Navigate to your IoT Hub and click IoT Devices. Find myFirstDevice, open it and you see myFirstModule was successfully created. Copy the module connection string. It is needed in the next step.
2. Similar to you did in the step above, create a directory for your device code and use NPM to initialize it and install the device SDK (**npm install -S azure-iot-device-amqp\@modules-preview**).
119
122
120
-
> [!NOTE]
121
-
> The npm install command may feel slow. Be patient, it's pulling down lots of code from the package repository.
123
+
> [!NOTE]
124
+
> The npm install command may feel slow. Be patient, it's pulling down lots of code from the package repository.
122
125
123
-
> [!NOTE]
124
-
> If you see an error that says npm ERR! registry error parsing json, this is safe to ignore. If you see an error that says npm ERR! registry error parsing json, this is safe to ignore.
126
+
> [!NOTE]
127
+
> If you see an error that says npm ERR! registry error parsing json, this is safe to ignore. If you see an error that says npm ERR! registry error parsing json, this is safe to ignore.
125
128
126
129
3. Create a file called twin.js. Copy and paste your module identity string.
127
130
@@ -148,15 +151,15 @@ In this section, you create a Node.js app on your simulated device that updates
148
151
console.error('error getting twin:' + err);
149
152
process.exit(1);
150
153
}
151
-
// Output the current properties
154
+
// Output the current properties
152
155
console.log('twin contents:');
153
156
console.log(twin.properties);
154
-
// Add a handler for desired property changes
157
+
// Add a handler for desired property changes
155
158
twin.on('properties.desired', function(delta) {
156
159
console.log('newdesired properties received:');
157
160
console.log(JSON.stringify(delta));
158
161
});
159
-
// create a patch to send to the hub
162
+
// create a patch to send to the hub
160
163
var patch = {
161
164
updateTime: new Date().toString(),
162
165
firmwareVersion:'1.2.1',
@@ -165,7 +168,7 @@ In this section, you create a Node.js app on your simulated device that updates
0 commit comments