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
This article describes how to use the [Azure IoT SDK for Java](https://github.com/Azure/azure-iot-sdk-java) to create device and backend service application code for device direct methods.
17
+
18
+
## Create a device application
19
+
20
+
This section describes how to use device application code to:
21
+
22
+
* Respond to a direct method called by the cloud
23
+
* Trigger a simulated device reboot
24
+
* Use the reported properties to enable device twin queries to identify devices and when they were last rebooted
Copy file name to clipboardExpand all lines: includes/iot-hub-howto-device-management-node.md
+22Lines changed: 22 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,3 +14,25 @@ ms.custom: mqtt, devx-track-js
14
14
## Overview
15
15
16
16
This article describes how to use the [Azure IoT SDK for Node.js](https://github.com/Azure/azure-iot-sdk-node) to create device and backend service application code for device direct methods.
17
+
18
+
## Create a device application
19
+
20
+
This section describes how to use device application code to:
21
+
22
+
* Respond to a direct method called by the cloud
23
+
* Trigger a simulated device reboot
24
+
* Use the reported properties to enable device twin queries to identify devices and when they were last rebooted
This article describes how to use the [Azure IoT SDK for Python](https://github.com/Azure/azure-iot-sdk-python) to create device and backend service application code for device direct methods.
17
+
18
+
## Create a device application
19
+
20
+
This section describes how to use device application code to:
21
+
22
+
* Respond to a direct method called by the cloud
23
+
* Trigger a simulated device reboot
24
+
* Use the reported properties to enable device twin queries to identify devices and when they were last rebooted
The [IoTHubDeviceClient](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient) class contains methods that can be used to work with direct methods.
31
+
32
+
To connect an application to a device:
33
+
1. Call [create_from_connection_string](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-create-from-connection-string) to add the device connection string
34
+
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
+
36
+
```python
37
+
# import the device client library
38
+
import asyncio
39
+
from azure.iot.device.aio import IoTHubDeviceClient
40
+
41
+
# substitute the device connection string in conn_str
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.
53
+
54
+
This example sets up a desired properties patch handler named `method_request_handler`.
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
+
69
+
This example sets up a callback listener named `method_request_handler` that will trigger when the "rebootDevice" direct method name is called.
The Azure IoT SDK for Python provides a working sample of a device app that handles direct method tasks. For more information, see [Receive direct method](https://github.com/Azure/azure-iot-sdk-python/blob/main/samples/async-hub-scenarios/receive_direct_method.py).
104
+
105
+
## Create a backend application
106
+
107
+
This section describes how to initiate a remote reboot on a device using a direct method.
108
+
109
+
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
+
111
+
### Import statements
112
+
113
+
Add these import statements to connect to Iot Hub, receive cloud-to-device methods, and call twin methods.
114
+
115
+
```python
116
+
import sys, time
117
+
118
+
from azure.iot.hub import IoTHubRegistryManager
119
+
from azure.iot.hub.models import CloudToDeviceMethod, CloudToDeviceMethodResult, Twin
120
+
```
121
+
122
+
### Connect to IoT hub
123
+
124
+
Connect to IoT hub using [from_connection_string](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager?#azure-iot-hub-iothubregistrymanager-from-connection-string). As a parameter, supply the **IoT Hub service connection string** that you created in the prerequisites section.
125
+
126
+
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
+
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).
129
+
130
+
For example:
131
+
132
+
```python
133
+
# Connect to IoT hub
134
+
IOTHUB_CONNECTION_STRING="{IoT hub service connection string}"
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.
141
+
142
+
To invoke a direct method on a device:
143
+
144
+
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
+
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
+
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.
The Azure IoT SDK for Python provides a working sample of a service app that handles direct method tasks. For more information, see [Service helper sync](https://github.com/Azure/azure-iot-sdk-python/blob/e75d1c2026eab939d5d31097fd0c22924c53abf8/dev_utils/dev_utils/service_helper_sync.py).
0 commit comments