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
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications between millions of devices and a solution back end.
20
20
21
21
This article shows you how to:
22
22
23
-
* Send cloud-to-device messages, from your solution backend, to a single device through IoT Hub
23
+
* Send cloud-to-device (C2D) messages from your solution backend to a single device through IoT Hub
24
24
25
25
* Receive cloud-to-device messages on a device
26
26
@@ -30,9 +30,9 @@ This article shows you how to:
30
30
31
31
At the end of this article, you run two .NET console apps.
32
32
33
-
***SimulatedDevice**: a modified version of the app created in [Send telemetry from a device to an IoT hub](../iot-develop/quickstart-send-telemetry-iot-hub.md?pivots=programming-language-csharp), which connects to your IoT hub and receives cloud-to-device messages.
33
+
***MessageReceiveSample**: a sample device app included with the [Microsoft Azure IoT SDK for .NET](https://github.com/Azure/azure-iot-sdk-csharp/tree/main/iothub/device/samples), which connects to your IoT hub and receives cloud-to-device messages.
34
34
35
-
***SendCloudToDevice**: sends a cloud-to-device message to the device app through IoT Hub and then receives its delivery acknowledgment.
35
+
***SendCloudToDevice**: a service app that sends a cloud-to-device message to the device app through IoT Hub and then receives its delivery acknowledgment.
36
36
37
37
> [!NOTE]
38
38
> IoT Hub has SDK support for many device platforms and languages (C, Java, Python, and JavaScript) through [Azure IoT device SDKs](iot-hub-devguide-sdks.md).
@@ -41,48 +41,58 @@ You can find more information on cloud-to-device messages in [D2C and C2D Messag
41
41
42
42
## Prerequisites
43
43
44
-
*Visual Studio
44
+
*An Azure subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
45
45
46
-
* A complete working version of the [Send telemetry from a device to an IoT hub](../iot-develop/quickstart-send-telemetry-iot-hub.md?pivots=programming-language-java) quickstart or the [Configure message routing with IoT Hub](tutorial-routing.md) article. This cloud-to-device article builds on the quickstart.
46
+
* An IoT hub in your Azure subscription. If you don't have a hub yet, you can follow the steps in [Create an IoT hub](iot-hub-create-through-portal.md).
47
+
48
+
* A device registered in your IoT hub. If you haven't registered a device yet, register one in the [Azure portal](iot-hub-create-through-portal.md#register-a-new-device-in-the-iot-hub).
49
+
50
+
* This article uses sample code from the [Azure IoT SDK for C#](https://github.com/Azure/azure-iot-sdk-csharp).
51
+
52
+
* Download or clone the SDK repository from GitHub to your development machine.
53
+
* Make sure that .NET Core 3.0.0 or greater is installed on your development machine. Check your version by running `dotnet --version` and [download .NET](https://dotnet.microsoft.com/download) if necessary.
47
54
48
55
* 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).
49
56
50
-
## Receive messages in the device app
57
+
* Visual Studio.
51
58
52
-
In this section, modify your device app to receive cloud-to-device messages from the IoT hub.
59
+
## Get the device connection string
53
60
54
-
1.In Visual Studio, in the **SimulatedDevice**project, add the following method to the **SimulatedDevice** class.
61
+
In this article, you run a sample app that simulates a device, which receives cloud-to-device messages sent through your IoT Hub. The **MessageReceiveSample**sample app included with the [Microsoft Azure IoT SDK for .NET](https://github.com/Azure/azure-iot-sdk-csharp/tree/main/iothub/device/samples) connects to your IoT hub and acts as your simulated device. The sample uses the primary connection string of the registered device on your IoT hub.
55
62
56
-
```csharp
57
-
privatestaticasyncvoidReceiveC2dAsync()
58
-
{
59
-
Console.WriteLine("\nReceiving cloud to device messages from service");
In this section, run the **MessageReceiveSample** sample device app to receive C2D messages sent through your IoT hub. Open a new command prompt and navigate to the **azure-iot-sdk-csharp\iothub\device\samples\getting started\MessageReceiveSample** folder, under the folder where you expanded the Azure IoT C# SDK. Run the following commands, replacing the `{Your device connection string}` placeholder value with the device connection string you copied from the registered device in your IoT hub.
74
68
75
-
1. Add the following method in the **Main** method, right before the `Console.ReadLine()` line:
69
+
```cmd/sh
70
+
dotnet restore
71
+
dotnet run --c "{Your device connection string}"
72
+
```
76
73
77
-
```csharp
78
-
ReceiveC2dAsync();
79
-
```
74
+
The following output is from the sample device app after it successfully starts and connects to your IoT hub:
75
+
76
+
```cmd/sh
77
+
5/22/2023 11:13:18 AM> Press Control+C at any time to quit the sample.
78
+
79
+
5/22/2023 11:13:18 AM> Device waiting for C2D messages from the hub...
80
+
5/22/2023 11:13:18 AM> Use the Azure Portal IoT hub blade or Azure IoT Explorer to send a message to this device.
81
+
5/22/2023 11:13:18 AM> Trying to receive C2D messages by polling using the ReceiveAsync() method. Press 'n' to move to the next phase.
82
+
```
83
+
84
+
The sample device app polls for messages by using the [ReceiveAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.receiveasync) and [CompleteAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.completeasync) methods. The `ReceiveC2dMessagesPollingAndCompleteAsync` method uses the `ReceiveAsync` method, which asynchronously returns the received message at the time the device receives the message. `ReceiveAsync` returns *null* after a specifiable timeout period. In this example, the default of one minute is used. When the device receives a *null*, it should continue to wait for new messages. This requirement is the reason why the sample app includes the following block of code in the `ReceiveC2dMessagesPollingAndCompleteAsync` method:
80
85
81
-
The `ReceiveAsync` method asynchronously returns the received message at the time that it's received by the device. It returns *null* after a specifiable timeout period. In this example, the default of one minute is used. When the app receives a *null*, it should continue to wait for new messages. This requirement is the reason for the `if (receivedMessage == null) continue` line.
86
+
```csharp
87
+
if (receivedMessage==null)
88
+
{
89
+
continue;
90
+
}
91
+
```
82
92
83
-
The call to `CompleteAsync()` notifies IoT Hub that the message has been successfully processed and that the message can be safely removed from the device queue. The device should call this method when its processing successfully completes regardless of the protocol it's using.
93
+
The call to the `CompleteAsync` method notifies IoT Hub that the message has been successfully processed and that the message can be safely removed from the device queue. The device should call this method when its processing successfully completes regardless of the protocol it's using.
84
94
85
-
With AMQP and HTTPS, but not MQTT, the device can also:
95
+
With AMQP and HTTPS protocols, but not the [MQTT protocol](../iot/iot-mqtt-connect-to-iot-hub.md), the device can also:
86
96
87
97
* Abandon a message, which results in IoT Hub retaining the message in the device queue for future consumption.
88
98
* Reject a message, which permanently removes the message from the device queue.
@@ -124,7 +134,7 @@ In this section, you create a .NET console app that sends cloud-to-device messag
124
134
usingMicrosoft.Azure.Devices;
125
135
```
126
136
127
-
1. Add the following fields to the **Program** class. Replace the `{iot hub connection string}` placeholder value with the IoT hub connection string you noted previously in [Get the IoT hub connection string](#get-the-iot-hub-connection-string). Replace the `{device id}` placeholder value with the device ID of the device you added in the [Send telemetry from a device to an IoT hub](../iot-develop/quickstart-send-telemetry-iot-hub.md?pivots=programming-language-csharp) quickstart.
137
+
1. Add the following fields to the **Program** class. Replace the `{iot hub connection string}` placeholder value with the IoT hub connection string you noted previously in [Get the IoT hub connection string](#get-the-iot-hub-connection-string). Replace the `{device id}` placeholder value with the device ID of the registered device in your IoT hub.
128
138
129
139
```csharp
130
140
staticServiceClientserviceClient;
@@ -155,19 +165,26 @@ In this section, you create a .NET console app that sends cloud-to-device messag
It's possible to request delivery (or expiration) acknowledgments from IoT Hub for each cloud-to-device message. This option enables the solution back end to easily inform, retry, or compensation logic. For more information about cloud-to-device feedback, see [D2C and C2D Messaging with IoT Hub](iot-hub-devguide-messaging.md).
0 commit comments