Skip to content

Commit 15b078e

Browse files
committed
Copy the node-node-module-twin article and pics from previous PR to this one.
1 parent a68dada commit 15b078e

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

articles/iot-hub/iot-hub-node-node-module-twin-getstarted.md

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,28 @@ ms.date: 04/26/2018
1919
At the end of this tutorial, you have two Node.js apps:
2020

2121
* **CreateIdentities**, which creates a device identity, a module identity and associated security key to connect your device and module clients.
22+
2223
* **UpdateModuleTwinReportedProperties**, which sends updated module twin reported properties to your IoT Hub.
2324

2425
> [!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).
2627
2728
To complete this tutorial, you need the following:
2829

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.)
3031
* An IoT Hub.
3132
* Install the latest [Node.js SDK](https://github.com/Azure/azure-iot-sdk-node).
3233

3334
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.
3435

3536
## Create a device identity and a module identity in IoT Hub
3637

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.
3839

3940
1. Create a directory to hold your code.
41+
4042
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+
4144
3. Run **npm install -S azure-iothub\@modules-preview** to install the service SDK inside the **node_modules** subdirectory.
4245

4346
> [!NOTE]
@@ -79,14 +82,14 @@ In this section, you create a Node.js app that creates a device identity and a m
7982
}
8083
console.log('device connection string = "HostName=' + hubName + ';DeviceId=' + deviceId + ';SharedAccessKey=' + primaryKey + '"');
8184

82-
// Then add a module to that device
85+
// Then add a module to that device
8386
registry.addModule({ deviceId: deviceId, moduleId: moduleId }, function(err) {
8487
if (err) {
8588
console.log('Error creating module identity: ' + err);
8689
process.exit(1);
8790
}
8891

89-
// Finally, retrieve the module details from the hub so we can construct the connection string
92+
// Finally, retrieve the module details from the hub so we can construct the connection string
9093
registry.getModule(deviceId, moduleId, function(err, foundModule) {
9194
if (err) {
9295
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
102105

103106
This app creates a device identity with ID **myFirstDevice** and a module identity with ID **myFirstModule** under device **myFirstDevice**. (If that module ID 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.
104107

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.
106109

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 for module identities. 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 for module identities. For more information, see [IoT Hub developer guide](iot-hub-devguide-identity-registry.md).
109112

110113
## Update the module twin using Node.js device SDK
111114

112115
In this section, you create a Node.js app on your simulated device that updates the module twin reported properties.
113116

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 in to 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.
115118

116-
![Azure portal module detail][15]
119+
![Azure portal module detail](./media/iot-hub-node-node-module-twin-getstarted/module-detail.png)
117120

118121
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**).
119122

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.
122125
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.
125128
126129
3. Create a file called twin.js. Copy and paste your module identity string.
127130
@@ -148,15 +151,15 @@ In this section, you create a Node.js app on your simulated device that updates
148151
console.error('error getting twin: ' + err);
149152
process.exit(1);
150153
}
151-
// Output the current properties
154+
// Output the current properties
152155
console.log('twin contents:');
153156
console.log(twin.properties);
154-
// Add a handler for desired property changes
157+
// Add a handler for desired property changes
155158
twin.on('properties.desired', function(delta) {
156159
console.log('new desired properties received:');
157160
console.log(JSON.stringify(delta));
158161
});
159-
// create a patch to send to the hub
162+
// create a patch to send to the hub
160163
var patch = {
161164
updateTime: new Date().toString(),
162165
firmwareVersion:'1.2.1',
@@ -165,7 +168,7 @@ In this section, you create a Node.js app on your simulated device that updates
165168
humidity: 17
166169
}
167170
};
168-
// send the patch
171+
// send the patch
169172
twin.properties.reported.update(patch, function(err) {
170173
if (err) throw err;
171174
console.log('twin state reported');
@@ -174,7 +177,7 @@ In this section, you create a Node.js app on your simulated device that updates
174177
});
175178
```
176179
177-
2. Now, run this using the command **node twin.js**.
180+
4. Now, run this using the command **node twin.js**.
178181
179182
```
180183
F:\temp\module_twin>node twin.js
@@ -191,17 +194,6 @@ In this section, you create a Node.js app on your simulated device that updates
191194
192195
To continue getting started with IoT Hub and to explore other IoT scenarios, see:
193196
194-
* [Getting started with device management][lnk-device-management]
195-
* [Getting started with IoT Edge][lnk-iot-edge]
196-
197-
<!-- Images. -->
198-
[15]: ./media/iot-hub-node-node-module-twin-getstarted/module-detail.png
199-
<!-- Links -->
200-
[lnk-hub-sdks]: iot-hub-devguide-sdks.md
201-
[lnk-free-trial]: https://azure.microsoft.com/pricing/free-trial/
202-
[lnk-portal]: https://portal.azure.com/
197+
* [Getting started with device management](iot-hub-node-node-device-management-get-started.md)
203198
204-
[lnk-device-management]: iot-hub-node-node-device-management-get-started.md
205-
[lnk-iot-edge]: ../iot-edge/tutorial-simulate-device-linux.md
206-
[lnk-devguide-identity]: iot-hub-devguide-identity-registry.md
207-
[lnk-nuget-service-sdk]: https://www.nuget.org/packages/Microsoft.Azure.Devices/
199+
* [Getting started with IoT Edge](../iot-edge/tutorial-simulate-device-linux.md)
0 Bytes
Loading

0 commit comments

Comments
 (0)