Skip to content

Commit e38083f

Browse files
committed
added desired properties update code
1 parent 799f043 commit e38083f

File tree

1 file changed

+73
-57
lines changed

1 file changed

+73
-57
lines changed

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

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ ms.author: menchi
1818
> [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
21-
At the end of this tutorial, you have two Python apps:
21+
At the end of this tutorial, you have three Python apps:
2222

2323
* **CreateIdentities**, which creates a device identity, a module identity and associated security key to connect your device and module clients.
2424

25-
* **UpdateModuleTwinReportedProperties**, which sends updated module twin reported properties to your IoT Hub.
25+
* **UpdateModuleTwinDesiredProperties**, which sends updated module twin desired properties to your IoT Hub.
26+
27+
* **
2628

2729
[!INCLUDE [iot-hub-include-python-sdk-note](../../includes/iot-hub-include-python-sdk-note.md)]
2830

@@ -50,16 +52,16 @@ In this section, you create a Python app that creates a device identity and a mo
5052
pip install azure-iot-hub
5153
```
5254
53-
2. Using a text editor, create a file named **CreateModule.py** in your working directory.
55+
1. Using a text editor, create a file named **CreateModule.py** in your working directory.
5456
55-
3. Add the following code to your Python file:
57+
1. Add the following code to your Python file:
5658
5759
```python
5860
import sys
5961
from msrest.exceptions import HttpOperationError
6062
from azure.iot.hub import IoTHubRegistryManager
6163
62-
CONNECTION_STRING = "YourConnString"
64+
CONNECTION_STRING = "YourIotHubConnectionString"
6365
DEVICE_ID = "myFirstDevice"
6466
MODULE_ID = "myFirstModule"
6567
@@ -109,7 +111,7 @@ In this section, you create a Python app that creates a device identity and a mo
109111
print("IoTHubRegistryManager sample stopped")
110112
```
111113
112-
4. At your command prompt, run the following command:
114+
1. At your command prompt, run the following command:
113115
114116
```cmd/sh
115117
python AddModule.py
@@ -121,94 +123,108 @@ This app creates a device identity with ID **myFirstDevice** and a module identi
121123
> 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).
122124
>
123125
124-
## Update the module twin using Python device SDK
125-
126-
In this section, you create a Python app on your simulated device that updates the module twin reported properties.
127-
128-
1. Get your module connection string. In [Azure portal](https://portal.azure.com/), navigate to your IoT Hub and select **IoT devices** on the left pane. Select **myFirstDevice** from the list of devices and open it. Under **Module identities** select **myFirstModule**. Copy the module connection string. You need it in a following step.
126+
## Update the module twin using Python service SDK
129127
130-
![Azure portal module detail](./media/iot-hub-python-python-module-twin-getstarted/module-detail.png)
128+
In this section, you create a Python app that updates the module twin desired properties.
131129
132-
2. At your command prompt, run the following command to install the **azure-iot-device** package:
130+
1. At your command prompt, run the following command to install the **azure-iot-hub** package:
133131
134132
```cmd/sh
135-
pip install azure-iot-device
133+
pip install azure-iot-hub
136134
```
137135
138-
3. Using a text editor, create a file named **UpdateModuleTwinReportedProperties.py** in your working directory.
136+
1. Using a text editor, create a file named **UpdateModuleTwinDesiredProperties.py** in your working directory.
139137
140-
4. Add the following code to your Python file:
138+
1. Add the following code to your Python file:
141139
142140
```python
143141
import sys
144-
import iothub_service_client
145-
from iothub_service_client import IoTHubRegistryManager, IoTHubRegistryManagerAuthMethod, IoTHubDeviceTwin, IoTHubError
142+
from azure.iot.hub import IoTHubRegistryManager
143+
from azure.iot.hub.models import Twin, TwinProperties
146144
147-
CONNECTION_STRING = "{moduleConnectionString}"
145+
CONNECTION_STRING = "YourIoTHubConnectionString"
148146
DEVICE_ID = "myFirstDevice"
149147
MODULE_ID = "myFirstModule"
150148
151-
UPDATE_JSON = "{\"properties\":{\"desired\":{\"telemetryInterval\":122}}}"
152-
153149
try:
154-
iothub_twin = IoTHubDeviceTwin(CONNECTION_STRING)
150+
# RegistryManager
151+
iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
155152
156-
twin_info = iothub_twin.get_twin(DEVICE_ID, MODULE_ID)
153+
module_twin = iothub_registry_manager.get_module_twin(DEVICE_ID, MODULE_ID)
157154
print ( "" )
158-
print ( "Twin before update :" )
159-
print ( "{0}".format(twin_info) )
160-
161-
twin_info_updated = iothub_twin.update_twin(DEVICE_ID, MODULE_ID, UPDATE_JSON)
155+
print ( "Module twin properties before update :" )
156+
print ( "{0}".format(module_twin.properties) )
157+
158+
# Update twin
159+
twin_patch = Twin()
160+
twin_patch.properties = TwinProperties(desired={"telemetryInterval": 122})
161+
updated_module_twin = iothub_registry_manager.update_module_twin(
162+
DEVICE_ID, MODULE_ID, twin_patch, module_twin.etag
163+
)
162164
print ( "" )
163-
print ( "Twin after update :" )
164-
print ( "{0}".format(twin_info_updated) )
165+
print ( "Module twin properties after update :" )
166+
print ( "{0}".format(updated_module_twin.properties) )
165167
166-
except IoTHubError as iothub_error:
167-
print ( "Unexpected error {0}".format(iothub_error) )
168+
except Exception as ex:
169+
print ( "Unexpected error {0}".format(ex) )
168170
except KeyboardInterrupt:
169171
print ( "IoTHubRegistryManager sample stopped" )
170172
```
171173
172174
## Get updates on the device side
173175
174-
In addition to the above code, you can use the following code to get the twin update message on your device.
176+
You can use the following code to get the module twin update patch on your device.
177+
178+
1. Get your module connection string. In [Azure portal](https://portal.azure.com/), navigate to your IoT Hub and select **IoT devices** on the left pane. Select **myFirstDevice** from the list of devices and open it. Under **Module identities** select **myFirstModule**. Copy the module connection string. You need it in a following step.
179+
180+
![Azure portal module detail](./media/iot-hub-python-python-module-twin-getstarted/module-detail.png)
175181
176-
```python
177-
import time
178-
import threading
179-
from azure.iot.device import IoTHubModuleClient
182+
1. At your command prompt, run the following command to install the **azure-iot-device** package:
180183
181-
CONNECTION_STRING = "{deviceConnectionString}"
184+
```cmd/sh
185+
pip install azure-iot-device
186+
```
182187
188+
1. Using a text editor, create a file named **ReceiveModuleTwinDesiredPropertiesPatch.py** in your working directory.
183189
184-
def twin_update_listener(client):
185-
while True:
186-
patch = client.receive_twin_desired_properties_patch() # blocking call
187-
print("")
188-
print("Twin desired properties patch received:")
189-
print(patch)
190+
1. Add the following code to your Python file:
190191
191-
def iothub_client_sample_run():
192-
try:
193-
module_client = IoTHubModuleClient.create_from_connection_string(CONNECTION_STRING)
192+
```python
193+
import time
194+
import threading
195+
from azure.iot.device import IoTHubModuleClient
194196
195-
twin_update_listener_thread = threading.Thread(target=twin_update_listener, args=(module_client,))
196-
twin_update_listener_thread.daemon = True
197-
twin_update_listener_thread.start()
197+
CONNECTION_STRING = "YourModuleConnectionString"
198198
199+
200+
def twin_update_listener(client):
199201
while True:
200-
time.sleep(1000000)
202+
patch = client.receive_twin_desired_properties_patch() # blocking call
203+
print("")
204+
print("Twin desired properties patch received:")
205+
print(patch)
201206
202-
except KeyboardInterrupt:
203-
print("IoTHubModuleClient sample stopped")
207+
def iothub_client_sample_run():
208+
try:
209+
module_client = IoTHubModuleClient.create_from_connection_string(CONNECTION_STRING)
210+
211+
twin_update_listener_thread = threading.Thread(target=twin_update_listener, args=(module_client,))
212+
twin_update_listener_thread.daemon = True
213+
twin_update_listener_thread.start()
204214
215+
while True:
216+
time.sleep(1000000)
205217
206-
if __name__ == '__main__':
207-
print ( "Starting the IoT Hub Python sample..." )
208-
print ( "IoTHubModuleClient waiting for commands, press Ctrl-C to exit" )
218+
except KeyboardInterrupt:
219+
print("IoTHubModuleClient sample stopped")
209220
210-
iothub_client_sample_run()
211-
```
221+
222+
if __name__ == '__main__':
223+
print ( "Starting the IoT Hub Python sample..." )
224+
print ( "IoTHubModuleClient waiting for commands, press Ctrl-C to exit" )
225+
226+
iothub_client_sample_run()
227+
```
212228
213229
## Next steps
214230

0 commit comments

Comments
 (0)