Skip to content

Commit fb0aa20

Browse files
authored
Merge branch 'howto-device-twins' into patch-2
2 parents 6349f48 + 4252fef commit fb0aa20

File tree

5 files changed

+88
-86
lines changed

5 files changed

+88
-86
lines changed

articles/iot-hub/how-to-device-twins.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ This article shows you how to develop two types of applications. Device apps can
3333
3434
## Prerequisites
3535

36-
* **An IoT hub**. Create one using the [Azure portal, CLI, or PowerShell](create-hub.md).
36+
* **An IoT hub**. Some SDK calls require the IoT Hub primary connection string, so make a note of the connection string.
3737

38-
* **A registered device**. Register one in the [Azure portal](create-connect-device.md).
38+
* **A registered device**. Some SDK calls require the device primary connection string, so make a note of the connection string.
3939

4040
* 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).
4141

@@ -59,6 +59,12 @@ This article shows you how to develop two types of applications. Device apps can
5959

6060
For more information about IoT Hub shared access policies and permissions, see [Access control and permissions](/azure/iot-hub/authenticate-authorize-sas).
6161

62+
* Language SDK requirements:
63+
* **.NET SDK** - Requres Visual Studio.
64+
* **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.
65+
* **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.
66+
* **Node.js** - Node.js version 10.0.x or later is required to use the SDK.
67+
6268
:::zone pivot="programming-language-csharp"
6369

6470
[!INCLUDE [iot-hub-howto-device-twins-dotnet](../../includes/iot-hub-howto-device-twins-dotnet.md)]

includes/iot-hub-howto-device-twins-dotnet.md

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ ms.custom: mqtt, devx-track-csharp, devx-track-dotnet
1313

1414
## Overview
1515

16-
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.
1917

2018
## Create a device application
2119

@@ -29,21 +27,24 @@ This section describes how to use device application code to:
2927

3028
### Add device NuGet Package
3129

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.
3931

4032
### Connect to a device
4133

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.
4335

4436
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.
4537

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.
4748

4849
This example connects to a device using the `Mqtt` transport protocol.
4950

@@ -58,11 +59,11 @@ _deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString,
5859
TransportType.Mqtt);
5960
```
6061

61-
### Retrieve a device twin and examine reported properties
62+
### Retrieve a device twin and examine properties
6263

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`.
6465

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.
6667

6768
```csharp
6869
Console.WriteLine("Retrieving twin...");
@@ -98,7 +99,7 @@ catch (Exception ex)
9899

99100
### Create a desired property update callback handler
100101

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)).
102103

103104
For example, this call sets up the system to notify a method named`OnDesiredPropertyChangedAsync` whenever a desired property is changed.
104105

@@ -134,7 +135,7 @@ private async Task OnDesiredPropertyChangedAsync(TwinCollection desiredPropertie
134135

135136
### SDK sample
136137

137-
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).
138139

139140
## Create a backend application
140141

@@ -154,15 +155,9 @@ This section describes how to create backend application code to:
154155

155156
Backend service applications require the **Microsoft.Azure.Devices** NuGet package.
156157

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-
163158
### Connect to IoT hub
164159

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.
166161

167162
```csharp
168163
using Microsoft.Azure.Devices;
@@ -216,7 +211,7 @@ catch (Exception e)
216211
}
217212
```
218213

219-
##### Update using a JSON string
214+
##### Update tags using a JSON string
220215

221216
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.
222217

includes/iot-hub-howto-device-twins-java.md

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ ms.custom: mqtt, devx-track-java, devx-track-extended-java
1313

1414
## Overview
1515

16-
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.
1917

2018
## Create a device application
2119

@@ -29,9 +27,9 @@ This section describes how to create device application code to:
2927

3028
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.
3129

32-
### Import statements
30+
### Device import statements
3331

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.
3533

3634
```java
3735
import com.microsoft.azure.sdk.iot.device.*;
@@ -75,7 +73,7 @@ System.out.println("Received current twin:");
7573
System.out.println(twin);
7674
```
7775

78-
### Update reported device twin properties
76+
### Update device twin reported properties
7977

8078
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.
8179

@@ -102,7 +100,7 @@ System.out.println("Successfully set property \"HomeTemp(F)\" to value " + newTe
102100

103101
### Subscribe to desired property changes
104102

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.
106104

107105
This example subscribes to desired property changes. Any desired property changes are passed to a handler named `DesiredPropertiesUpdatedHandler`.
108106

@@ -136,22 +134,26 @@ In this example, the `DesiredPropertiesUpdatedHandler` desired property change c
136134

137135
### SDK sample
138136

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).
140138

141139
## Create a backend application
142140

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-
148141
This section describes how to create a backend application that:
149142

150143
* Updates device twin tags
151144
* Queries devices using filters on the tags and properties
152145

153146
The `ServiceClient` [DeviceTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin) class contains methods that services can use to access device twins.
154147

148+
### Service import statements
149+
150+
Use the following service import statements to access the Azure IoT SDK for Java.
151+
152+
```java
153+
import com.microsoft.azure.sdk.iot.service.devicetwin.*;
154+
import com.microsoft.azure.sdk.iot.service.exceptions.IotHubException;
155+
```
156+
155157
### Connect to the IoT hub service client
156158

157159
To connect to IoT Hub to view and update device twin information:
@@ -163,13 +165,6 @@ To connect to IoT Hub to view and update device twin information:
163165
For example:
164166

165167
```java
166-
import com.microsoft.azure.sdk.iot.service.devicetwin.*;
167-
import com.microsoft.azure.sdk.iot.service.exceptions.IotHubException;
168-
169-
import java.io.IOException;
170-
import java.util.HashSet;
171-
import java.util.Set;
172-
173168
public static final String iotHubConnectionString = "{IoT hub service connection string}";
174169
public static final String deviceId = "myDeviceId";
175170
public static final String region = "US";

0 commit comments

Comments
 (0)