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
The [DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) class exposes all the methods required to interact with device twins from the device.
Call [GetTwinAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.gettwinasync?#microsoft-azure-devices-client-deviceclient-gettwinasync) to retrieve the current device twin properties. There are many [Twin](/dotnet/api/microsoft.azure.devices.shared.twin?) object [properties](/dotnet/api/microsoft.azure.devices.shared.twin?&#properties) that you can use to access specific areas of the `Twin` JSON data including `Properties`, `Status`, `Tags`, and `Version`.
After opening the client connection, call [getTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwin-gettwin(com-microsoft-azure-sdk-iot-service-devicetwin-devicetwindevice)) to retrieve the current twin properties into a `Twin` object.
The [azure-iot-device](/javascript/api/azure-iot-device) package contains objects that interface with IoT devices. The [Twin](/javascript/api/azure-iot-device/twin) class includes twin-specific objects. This section describes `Client` class code that is used to read and write device twin data.
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 twins.
17
17
18
+
## Install packages
19
+
20
+
The **azure-iot-device** library must be installed to create device applications.
21
+
22
+
```cmd/sh
23
+
pip install azure-iot-device
24
+
```
25
+
26
+
The **azure-iot-hub** library must be installed to create backend service applications.
27
+
28
+
```cmd/sh
29
+
pip install azure-iot-hub
30
+
```
31
+
18
32
## Create a device application
19
33
20
34
Device applications can read and write twin reported properties, and be notified of desired twin property changes that are set by a backend application or IoT Hub.
@@ -26,30 +40,45 @@ This section describes how to create device application code that:
26
40
* Retrieves a device twin and examine reported properties
27
41
* Patch reported device twin properties
28
42
43
+
### Device import statement
44
+
45
+
Add this code to import the `IoTHubDeviceClient` functions from the azure.iot.device SDK.
46
+
47
+
```python
48
+
from azure.iot.device import IoTHubDeviceClient
49
+
```
50
+
51
+
### Connect a device to IoT Hub
52
+
53
+
A device app can authenticate with IoT Hub using the following methods:
This section shows how to connect an application to a device using a device primary key that includes a shared access key.
62
+
To connect a device to IoT Hub:
34
63
35
-
To connect an application to a device:
36
-
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
37
-
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
64
+
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 primary connection string.
65
+
1. Call [connect](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-connect) to connect the device client.
38
66
39
-
```python
40
-
# import the device client library
41
-
import asyncio
42
-
from azure.iot.device.aio import IoTHubDeviceClient
67
+
For example:
43
68
44
-
# substitute the device connection string in conn_str
### Retrieve a device twin and examine reported properties
54
83
55
84
You can retrieve and examine device twin information including tags and properties. The device twin information retrieved matches device twin JSON-formatted data that you can view for a device in the Azure portal.
0 commit comments