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
Device twins are JSON documents that store device state information, including metadata, configurations, and conditions. IoT Hub persists a device twin for each device that connects to it.
18
+
Use the IoT Hub device and service SDKs to develop applications that handle common device twin tasks. Device twins are JSON documents that store device state information, including metadata, configurations, and conditions. IoT Hub persists a device twin for each device that connects to it.
* Store device metadata from your solution back end.
24
+
* Store device metadata from your solution back end
25
25
26
-
* Report current state information such as available capabilities and conditions, for example, the connectivity method used, from your device app.
26
+
* Report current state information such as available capabilities and conditions, for example, the connectivity method used, from your device app
27
27
28
-
* Synchronize the state of long-running workflows, such as firmware and configuration updates, between a device app and a back-end app.
28
+
* Synchronize the state of long-running workflows, such as firmware and configuration updates, between a device app and a back-end app
29
29
30
-
* Query your device metadata, configuration, or state.
30
+
* Query your device metadata, configuration, or state
31
31
32
32
Device twins are designed for synchronization and for querying device configurations and conditions. For more information about device twins, including when to use device twins, see [Understand device twins](iot-hub-devguide-device-twins.md).
33
33
@@ -62,9 +62,9 @@ This article shows you how to:
62
62
63
63
## Prerequisites
64
64
65
-
***An IoT hub**. Create one using the [Azure portal, CLI, or PowerShell](create-hub.md).
65
+
***An IoT hub**. Some SDK calls require the IoT Hub primary connection string, so make a note of the connection string.
66
66
67
-
***A registered device**. Register one in the [Azure portal](create-connect-device.md).
67
+
***A registered device**. Some SDK calls require the device primary connection string, so make a note of the connection string.
68
68
69
69
* Make sure that **port 8883** is open in your firewall. The device sample in this article uses MQTT protocol, which communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see [Connecting to IoT Hub (MQTT)](../iot/iot-mqtt-connect-to-iot-hub.md#connecting-to-iot-hub).
70
70
@@ -88,6 +88,12 @@ This article shows you how to:
88
88
89
89
For more information about IoT Hub shared access policies and permissions, see [Access control and permissions](/azure/iot-hub/authenticate-authorize-sas).
90
90
91
+
* Language SDK requirements:
92
+
***.NET SDK** - Requres Visual Studio.
93
+
***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.
94
+
***Java** - [Java SE Development Kit 8](/azure/developer/java/fundamentals/) is required to use the SDK. Make sure you select **Java 8** under **Long-term support** to navigate to downloads for JDK 8.
95
+
***Node.js** - Node.js version 10.0.x or later is required to use the SDK.
This unit describes how to use the Azure SDK for .NET to create device and backend service application code for device twins.
17
-
18
-
Visual Studio is required to use the SDK.
16
+
This article describes how to use the [Azure IoT SDK for .NET](https://github.com/Azure/azure-iot-sdk-csharp/blob/main/readme.md) to create device and backend service application code for device twins.
19
17
20
18
## Create a device application
21
19
@@ -29,21 +27,24 @@ This section describes how to use device application code to:
29
27
30
28
### Add device NuGet Package
31
29
32
-
Device client applications require the **Microsoft.Azure.Devices.Client** NuGet package.
33
-
34
-
To install the **Microsoft.Azure.Devices.Client** NuGet package:
35
-
36
-
1. In your application **Solution Explorer**, right-click your project, and then select **Manage NuGet Packages**.
37
-
1. Select **Browse** and search for and choose **Microsoft.Azure.Devices.Client**.
38
-
1. Select **Install**. This downloads, installs, and adds a reference to the Azure IoT device SDK NuGet package and its dependencies.
30
+
Device client applications written in C# require the **Microsoft.Azure.Devices.Client** NuGet package.
39
31
40
32
### Connect to a device
41
33
42
-
The [DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) class exposes all the methods you require to interact with device twins from the device.
34
+
The [DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) class exposes all the methods required to interact with device twins from the device.
43
35
44
36
Connect to the device using the [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.client.deviceclient.createfromconnectionstring?#microsoft-azure-devices-client-deviceclient-createfromconnectionstring(system-string)) method along with device connection string and the connection transport protocol.
45
37
46
-
The `CreateFromConnectionString`[TransportType](/dotnet/api/microsoft.azure.devices.client.transporttype) transport protocol parameter supports `Mqtt`, `Mqtt_WebSocket_Only`, `Mqtt_Tcp_Only`, `Amqp`, `Amqp_WebSocket_Only`, and `Amqp_Tcp_Only`. `Http1` is not supported for device twin updates.
38
+
The `CreateFromConnectionString`[TransportType](/dotnet/api/microsoft.azure.devices.client.transporttype) transport protocol parameter supports the following transport protocols:
39
+
40
+
*`Mqtt`
41
+
*`Mqtt_WebSocket_Only`
42
+
*`Mqtt_Tcp_Only`
43
+
*`Amqp`
44
+
*`Amqp_WebSocket_Only`
45
+
*`Amqp_Tcp_Only`
46
+
47
+
The `Http1` protocol is not supported for device twin updates.
47
48
48
49
This example connects to a device using the `Mqtt` transport protocol.
### Retrieve a device twin and examine reported properties
62
+
### Retrieve a device twin and examine properties
62
63
63
-
Call [GetTwinAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.gettwinasync?#microsoft-azure-devices-client-deviceclient-gettwinasync) to retrieve the current device twin properties. The result [Twin](/dotnet/api/microsoft.azure.devices.shared.twin?) object includes the device twin reported properties. There are many `Twin` object [properties](/dotnet/api/microsoft.azure.devices.shared.twin?&branch=main#properties) that you can use to access specific areas of the Twin JSON data including Twin `Properties`, `Status`, `Tags`, and `Version`.
64
+
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?&branch=main#properties) that you can use to access specific areas of the Twin JSON data including Twin `Properties`, `Status`, `Tags`, and `Version`.
64
65
65
-
This example retrieves device twin reported properties and prints the twin values in JSON format.
66
+
This example retrieves device twin properties and prints the twin values in JSON format.
66
67
67
68
```csharp
68
69
Console.WriteLine("Retrieving twin...");
@@ -98,7 +99,7 @@ catch (Exception ex)
98
99
99
100
### Create a desired property update callback handler
100
101
101
-
You can create a desired property update callback handler that executes when a desired property is changed in the device by passing the callback handler method name to [SetDesiredPropertyUpdateCallbackAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.setdesiredpropertyupdatecallbackasync?#microsoft-azure-devices-client-deviceclient-setdesiredpropertyupdatecallbackasync(microsoft-azure-devices-client-desiredpropertyupdatecallback-system-object)).
102
+
Create a desired property update callback handler that executes when a desired property is changed in the device twin by passing the callback handler method name to [SetDesiredPropertyUpdateCallbackAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.setdesiredpropertyupdatecallbackasync?#microsoft-azure-devices-client-deviceclient-setdesiredpropertyupdatecallbackasync(microsoft-azure-devices-client-desiredpropertyupdatecallback-system-object)).
102
103
103
104
For example, this call sets up the system to notify a method named`OnDesiredPropertyChangedAsync` whenever a desired property is changed.
The SDK includes this[TwinSample](https://github.com/Azure/azure-iot-sdk-csharp/tree/main/iothub/device/samples/getting%20started/TwinSample).
138
+
The Azure IoT SDK for .NET provides a working sample of a device app that handles device twin tasks. For more information, see[TwinSample](https://github.com/Azure/azure-iot-sdk-csharp/tree/main/iothub/device/samples/getting%20started/TwinSample).
138
139
139
140
## Create a backend application
140
141
@@ -154,15 +155,9 @@ This section describes how to create backend application code to:
154
155
155
156
Backend service applications require the **Microsoft.Azure.Devices** NuGet package.
156
157
157
-
To install the **Microsoft.Azure.Devices** NuGet package:
158
-
159
-
1. In your application **Solution Explorer**, right-click your project, and then select **Manage NuGet Packages**.
160
-
1. Select **Browse** and search for and choose **Microsoft.Azure.Devices**.
161
-
1. Select **Install**. This downloads, installs, and adds a reference to the Azure IoT device SDK NuGet package and its dependencies.
162
-
163
158
### Connect to IoT hub
164
159
165
-
Connect a backend application to a device using [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.client.deviceclient.createfromconnectionstring?#microsoft-azure-devices-client-deviceclient-createfromconnectionstring(system-string-microsoft-azure-devices-client-transporttype)). As a parameter, supply the **IoT Hub service connection string** that you created in the Prerequisites section.
160
+
Connect a backend application to a device using [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.registrymanager.createfromconnectionstring). As a parameter, supply the **IoT Hub service connection string** that you created in the prerequisites section.
166
161
167
162
```csharp
168
163
usingMicrosoft.Azure.Devices;
@@ -216,7 +211,7 @@ catch (Exception e)
216
211
}
217
212
```
218
213
219
-
##### Update using a JSON string
214
+
##### Update tags using a JSON string
220
215
221
216
You can create and apply a JSON-formatted device twin information update patch. IoT Hub parses and applies the patch if it is correctly formatted.
This unit describes how to use the Azure SDK for Java to create device and backend service application code for device twins.
17
-
18
-
[Java SE Development Kit 8](/azure/developer/java/fundamentals/) is required to use the SDK. Make sure you select **Java 8** under **Long-term support** to navigate to downloads for JDK 8.
16
+
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 twins.
19
17
20
18
## Create a device application
21
19
@@ -29,9 +27,9 @@ This section describes how to create device application code to:
29
27
30
28
The [DeviceClient](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient) class exposes all the methods you require to interact with device twins from the device.
31
29
32
-
### Import statements
30
+
### Device import statements
33
31
34
-
Use the following import statements to access the IoT Hub device SDK.
32
+
Use the following device import statements to access the Azure IoT SDK for Java.
35
33
36
34
```java
37
35
importcom.microsoft.azure.sdk.iot.device.*;
@@ -75,7 +73,7 @@ System.out.println("Received current twin:");
75
73
System.out.println(twin);
76
74
```
77
75
78
-
### Update reported device twin properties
76
+
### Update device twin reported properties
79
77
80
78
After retrieving the current twin, you can begin making reported property updates. You can also make reported property updates without getting the current twin as long as you have the correct reported properties version. If you send reported properties and receive a "precondition failed" error, then your reported properties version is out of date. In that case, get the latest version by calling `getTwin` again.
81
79
@@ -102,7 +100,7 @@ System.out.println("Successfully set property \"HomeTemp(F)\" to value " + newTe
102
100
103
101
### Subscribe to desired property changes
104
102
105
-
Call [subscribeToDesiredProperties](/java/api/com.microsoft.azure.sdk.iot.device.internalclient?#com-microsoft-azure-sdk-iot-device-internalclient-subscribetodesiredproperties(java-util-map(com-microsoft-azure-sdk-iot-device-devicetwin-property-com-microsoft-azure-sdk-iot-device-devicetwin-pair(com-microsoft-azure-sdk-iot-device-devicetwin-propertycallback(java-lang-string-java-lang-object)-java-lang-object)))) to subscribe to desired properties. This client receives a callback with a `Twin` object each time a desired property is updated. That callback either contains the full desired properties set, or only the updated desired property depending on how the desired property was changed.
103
+
Call [subscribeToDesiredProperties](/java/api/com.microsoft.azure.sdk.iot.device.internalclient?#com-microsoft-azure-sdk-iot-device-internalclient-subscribetodesiredproperties(java-util-map(com-microsoft-azure-sdk-iot-device-devicetwin-property-com-microsoft-azure-sdk-iot-device-devicetwin-pair(com-microsoft-azure-sdk-iot-device-devicetwin-propertycallback(java-lang-string-java-lang-object)-java-lang-object)))) to subscribe to desired property changes. This client receives a callback with a `Twin` object each time a desired property is updated. That callback either contains the full desired properties set, or only the updated desired property depending on how the desired property was changed.
106
104
107
105
This example subscribes to desired property changes. Any desired property changes are passed to a handler named `DesiredPropertiesUpdatedHandler`.
108
106
@@ -136,22 +134,26 @@ In this example, the `DesiredPropertiesUpdatedHandler` desired property change c
136
134
137
135
### SDK sample
138
136
139
-
The SDK includes this [Device Twin Sample](https://github.com/Azure/azure-iot-sdk-java/tree/main/iothub/device/iot-device-samples/device-twin-sample).
137
+
The Azure IoT SDK for Java includes a working sample to test the device app concepts described in this article. For more information, see [Device Twin Sample](https://github.com/Azure/azure-iot-sdk-java/tree/main/iothub/device/iot-device-samples/device-twin-sample).
140
138
141
139
## Create a backend application
142
140
143
-
A backend application:
144
-
145
-
* Connects to a device through IoT Hub
146
-
* Can read device reported and desired properties, write device desired properties, and run device queries
147
-
148
141
This section describes how to create a backend application that:
149
142
150
143
* Updates device twin tags
151
144
* Queries devices using filters on the tags and properties
152
145
153
146
The `ServiceClient`[DeviceTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin) class contains methods that services can use to access device twins.
154
147
148
+
### Service import statements
149
+
150
+
Use the following service import statements to access the Azure IoT SDK for Java.
0 commit comments