Skip to content

Commit df32fac

Browse files
committed
added v2 service code
1 parent 61abb1f commit df32fac

File tree

1 file changed

+72
-37
lines changed

1 file changed

+72
-37
lines changed

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

Lines changed: 72 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ms.author: menchi
1515
[!INCLUDE [iot-hub-selector-module-twin-getstarted](../../includes/iot-hub-selector-module-twin-getstarted.md)]
1616

1717
> [!NOTE]
18-
> [Module identities and module twins](iot-hub-devguide-module-twins.md) are similar to Azure IoT Hub device identity and device twin, but provide finer granularity. While Azure IoT Hub device identity and device twin enable the back-end application to configure a device and provides visibility on the devices conditions, a module identity and module twin provide these capabilities for individual components of a device. On capable devices with multiple components, such as operating system based devices or firmware devices, it allows for isolated configuration and conditions for each component.
18+
> [Module identities and module twins](iot-hub-devguide-module-twins.md) are similar to Azure IoT Hub device identity and device twin, but provide finer granularity. While Azure IoT Hub device identity and device twin enable the back-end application to configure a device and provides visibility on the device's conditions, a module identity and module twin provide these capabilities for individual components of a device. On capable devices with multiple components, such as operating system based devices or firmware devices, it allows for isolated configuration and conditions for each component.
1919
>
2020
2121
At the end of this tutorial, you have two Python apps:
@@ -28,7 +28,7 @@ At the end of this tutorial, you have two Python apps:
2828

2929
## Prerequisites
3030

31-
[!INCLUDE [iot-hub-include-python-installation-notes](../../includes/iot-hub-include-python-installation-notes.md)]
31+
[!INCLUDE [iot-hub-include-python-v2-installation-notes](../../includes/iot-hub-include-python-v2-installation-notes.md)]
3232

3333
## Create an IoT hub
3434

@@ -42,43 +42,78 @@ At the end of this tutorial, you have two Python apps:
4242

4343
## Create a device identity and a module identity in IoT Hub
4444

45-
In this section, you create a Python 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.
45+
In this section, you create a Python 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.
4646

47-
Add the following code to your Python file:
47+
1. At your command prompt, run the following command to install the **azure-iot-hub** package:
4848

49-
```python
50-
import sys
51-
import iothub_service_client
52-
from iothub_service_client import IoTHubRegistryManager, IoTHubRegistryManagerAuthMethod, IoTHubError
53-
54-
CONNECTION_STRING = "YourConnString"
55-
DEVICE_ID = "myFirstDevice"
56-
MODULE_ID = "myFirstModule"
57-
58-
try:
59-
# RegistryManager
60-
iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
61-
62-
# CreateDevice
63-
primary_key = ""
64-
secondary_key = ""
65-
auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY
66-
new_device = iothub_registry_manager.create_device(
67-
DEVICE_ID, primary_key, secondary_key, auth_method)
68-
print("new_device <" + DEVICE_ID +
69-
"> has primary key = " + new_device.primaryKey)
70-
71-
# CreateModule
72-
new_module = iothub_registry_manager.create_module(
73-
DEVICE_ID, primary_key, secondary_key, MODULE_ID, auth_method)
74-
print("device/new_module <" + DEVICE_ID + "/" + MODULE_ID +
75-
"> has primary key = " + new_module.primaryKey)
76-
77-
except IoTHubError as iothub_error:
78-
print("Unexpected error {0}".format(iothub_error))
79-
except KeyboardInterrupt:
80-
print("IoTHubRegistryManager sample stopped")
81-
```
49+
```cmd/sh
50+
pip install azure-iot-hub
51+
```
52+
53+
2. Using a text editor, create a file named **CreateModule.py** in your working directory.
54+
55+
3. Add the following code to your Python file:
56+
57+
```python
58+
import sys
59+
from msrest.exceptions import HttpOperationError
60+
from azure.iot.hub import IoTHubRegistryManager
61+
62+
CONNECTION_STRING = "YourConnString"
63+
DEVICE_ID = "myFirstDevice"
64+
MODULE_ID = "myFirstModule"
65+
66+
try:
67+
# RegistryManager
68+
iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
69+
70+
try:
71+
# CreateDevice - let IoT Hub assign keys
72+
primary_key = ""
73+
secondary_key = ""
74+
device_state = "enabled"
75+
new_device = iothub_registry_manager.create_device_with_sas(
76+
DEVICE_ID, primary_key, secondary_key, device_state
77+
)
78+
except HttpOperationError as ex:
79+
if ex.response.status_code == 409:
80+
# 409 indicates a conflict. This happens because the device already exists.
81+
new_device = iothub_registry_manager.get_device(DEVICE_ID)
82+
else:
83+
raise
84+
85+
print("device <" + DEVICE_ID +
86+
"> has primary key = " + new_device.authentication.symmetric_key.primary_key)
87+
88+
try:
89+
# CreateModule - let IoT Hub assign keys
90+
primary_key = ""
91+
secondary_key = ""
92+
managed_by = ""
93+
new_module = iothub_registry_manager.create_module_with_sas(
94+
DEVICE_ID, MODULE_ID, managed_by, primary_key, secondary_key
95+
)
96+
except HttpOperationError as ex:
97+
if ex.response.status_code == 409:
98+
# 409 indicates a conflict. This happens because the module already exists.
99+
new_module = iothub_registry_manager.get_module(DEVICE_ID, MODULE_ID)
100+
else:
101+
raise
102+
103+
print("device/module <" + DEVICE_ID + "/" + MODULE_ID +
104+
"> has primary key = " + new_module.authentication.symmetric_key.primary_key)
105+
106+
except Exception as ex:
107+
print("Unexpected error {0}".format(ex))
108+
except KeyboardInterrupt:
109+
print("IoTHubRegistryManager sample stopped")
110+
```
111+
112+
4. At your command prompt, run the following command:
113+
114+
```cmd/sh
115+
python AddModule.py
116+
```
82117
83118
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.
84119

0 commit comments

Comments
 (0)