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/how-to-device-management.md
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,6 +47,19 @@ This article shows you how to develop two types of applications:
47
47
* Language SDK requirements:
48
48
***.NET SDK** - Requires Visual Studio.
49
49
***Python SDK** - [Python version 3.7 or later](https://www.python.org/downloads/) is recommended. Make sure to use the 32-bit or 64-bit installation as required by your setup. When prompted during the installation, make sure to add Python to your platform-specific environment variable.
50
+
51
+
* Device applications require the **azure-iot-device** package. You can install the package using this command:
52
+
53
+
```cmd/sh
54
+
pip install azure-iot-device
55
+
```
56
+
57
+
* Service applications require the **azure-iot-hub** package. You can install the package using this command:
58
+
59
+
```cmd/sh
60
+
pip install azure-iot-hub
61
+
```
62
+
50
63
* **Java** - Requires [Java SE Development Kit 8](/azure/developer/java/fundamentals/). Make sure you select **Java 8** under **Long-term support** to navigate to downloads for JDK 8.
51
64
* **Node.js** - Requires Node.js version 10.0.x or later.
These import statements are required by `IoTHubDeviceClient`.
31
+
32
+
```python
33
+
# import the device client library
34
+
import asyncio
35
+
from azure.iot.device.aio import IoTHubDeviceClient
36
+
```
37
+
28
38
### Connect to a device
29
39
30
40
The [IoTHubDeviceClient](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient) class contains methods that can be used to work with direct methods.
@@ -34,10 +44,6 @@ To connect an application to a device:
34
44
1. Call [connect](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-connect) to connect the device client to an Azure IoT hub
35
45
36
46
```python
37
-
# import the device client library
38
-
import asyncio
39
-
from azure.iot.device.aio import IoTHubDeviceClient
40
-
41
47
# substitute the device connection string in conn_str
42
48
# and add it to the IoTHubDeviceClient object
43
49
conn_str ="{IOT hub device connection string}"
@@ -49,7 +55,7 @@ await device_client.connect()
49
55
50
56
### Create a direct method callback
51
57
52
-
Call [on_method_request_received](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?&#azure-iot-device-iothubdeviceclient-on-twin-desired-properties-patch-received) to create a handler function or coroutine that is called when a direct method is received. The listener is associated with a method name keyword, such as "reboot". The method name can be used in an IoT Hub or backend application to trigger the callback method on the device.
58
+
Call [on_method_request_received](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?view=azure-python&branch=main#azure-iot-device-iothubdeviceclient-on-method-request-received) to create a handler function or coroutine that is called when a direct method is received. The listener is associated with a method name keyword, such as "reboot". The method name can be used in an IoT Hub or backend application to trigger the callback method on the device.
53
59
54
60
This example sets up a desired properties patch handler named `method_request_handler`.
55
61
@@ -66,7 +72,7 @@ except:
66
72
67
73
In this example, the `method_request_handler` callback method implements the direct method on the device. This code updates reported properties related to a simulated device reboot. The reported properties can be read and verified by an IoT Hub or backend application, as demonstrated in the **Create a backend application** section of this article.
68
74
69
-
This example sets up a callback listener named `method_request_handler` that will trigger when the "rebootDevice" direct method name is called.
75
+
This example sets up a callback listener named `method_request_handler` that triggers when the "rebootDevice" direct method is called from a service application.
70
76
71
77
```python
72
78
# Define the handler for method requests
@@ -104,13 +110,13 @@ The Azure IoT SDK for Python provides a working sample of a device app that hand
104
110
105
111
## Create a backend application
106
112
107
-
This section describes how to initiate a remote reboot on a device using a direct method.
113
+
This section describes how to initiate a remote reboot on a device using a direct method call from a backend service application.
108
114
109
115
The [IoTHubRegistryManager](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager) class exposes all methods required to create a backend application to send messages to a device.
110
116
111
117
### Import statements
112
118
113
-
Add these import statements to connect to Iot Hub, receive cloud-to-device methods, and call twin methods.
119
+
Add these import statements to connect to Iot Hub, receive cloud-to-device methods, and call device twin methods.
114
120
115
121
```python
116
122
import sys, time
@@ -125,7 +131,7 @@ Connect to IoT hub using [from_connection_string](/python/api/azure-iot-hub/azur
125
131
126
132
To invoke a direct method on a device through IoT Hub, your service needs the **service connect** permission. By default, every IoT Hub is created with a shared access policy named **service** that grants this permission.
127
133
128
-
As a parameter to `CreateFromConnectionString`, supply the **service** shared access policy. For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
134
+
As a parameter to `CreateFromConnectionString`, supply the **service** shared access policy connection string. For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
You can invoke a direct method on a device. The method name is mapped to a name, which is "rebootTime" in the examples within this article.
146
+
You can invoke a direct method by name on a device. The method name identifies the method. The method name is "rebootTime" in the examples within this article.
141
147
142
148
To invoke a direct method on a device:
143
149
144
150
1. Create a [CloudToDeviceMethod](/python/api/azure-iot-hub/azure.iot.hub.protocol.models.cloudtodevicemethod) object. Supply the method name and payload as parameters.
145
151
1. Call [invoke_device_method](/python/api/azure-iot-hub/azure.iot.hub.iothub_registry_manager.iothubregistrymanager?#azure-iot-hub-iothub-registry-manager-iothubregistrymanager-invoke-device-method) to invoke a direct method on a device. Supply the device ID and `CloudToDeviceMethod` payload object as parameters.
146
152
147
-
This example invokes a direct method named "rebootDevice" on a device. The example then uses device twin queries to discover the last reboot time for the device that was updated in the **Create a direct method callback** section of this article.
153
+
This example invokes a direct method named "rebootDevice" on a device. The example then uses device twin queries to discover the last reboot time for the device that was updated as described in the **Create a direct method callback** section of this article.
0 commit comments