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
Copy file name to clipboardExpand all lines: articles/iot-hub/iot-hub-python-python-c2d.md
+68-84Lines changed: 68 additions & 84 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ ms.service: iot-hub
6
6
services: iot-hub
7
7
ms.devlang: python
8
8
ms.topic: conceptual
9
-
ms.date: 07/30/2019
9
+
ms.date: 04/09/2020
10
10
ms.author: robinsh
11
11
---
12
12
@@ -24,60 +24,69 @@ This tutorial builds on [Send telemetry from a device to an IoT hub](quickstart-
24
24
25
25
* Receive cloud-to-device messages on a device.
26
26
27
-
* From your solution back end, request delivery acknowledgment (*feedback*) for messages sent to a device from IoT Hub.
28
-
29
27
You can find more information on cloud-to-device messages in the [IoT Hub developer guide](iot-hub-devguide-messaging.md).
30
28
31
29
At the end of this tutorial, you run two Python console apps:
32
30
33
31
***SimulatedDevice.py**, a modified version of the app created in [Send telemetry from a device to an IoT hub](quickstart-send-telemetry-python.md), which connects to your IoT hub and receives cloud-to-device messages.
34
32
35
-
***SendCloudToDeviceMessage.py**, which sends a cloud-to-device message to the simulated device app through IoT Hub, and then receives its delivery acknowledgment.
33
+
***SendCloudToDeviceMessage.py**, which sends cloud-to-device messages to the simulated device app through IoT Hub.
* 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-hub-mqtt-support.md#connecting-to-iot-hub).
44
42
45
43
## Receive messages in the simulated device app
46
44
47
45
In this section, you create a Python console app to simulate the device and receive cloud-to-device messages from the IoT hub.
48
46
49
-
1. Using a text editor, create a **SimulatedDevice.py** file.
47
+
1. From a command prompt in your working directory, install the **Azure IoT Hub Device SDK for Python**:
48
+
49
+
```cmd/sh
50
+
pip install azure-iot-device
51
+
```
52
+
53
+
1. Using a text editor, create a file named **SimulatedDevice.py**.
50
54
51
-
2. Add the following `import` statements and variables at the start of the **SimulatedDevice.py** file:
55
+
1. Add the following `import` statements and variables at the start of the **SimulatedDevice.py** file:
52
56
53
-
```python
57
+
```python
54
58
import threading
59
+
import time
55
60
from azure.iot.device import IoTHubDeviceClient
56
61
57
62
RECEIVED_MESSAGES = 0
58
63
```
59
64
60
-
3. Add the following code to **SimulatedDevice.py**file. Replace the "{deviceConnectionString}" placeholder value with the device connection string for the device you created in the [Send telemetry from a device to an IoT hub](quickstart-send-telemetry-python.md) quickstart:
65
+
1. Add the following code to **SimulatedDevice.py** file. Replace the `{deviceConnectionString}` placeholder value with the device connection string for the device you created in the [Send telemetry from a device to an IoT hub](quickstart-send-telemetry-python.md) quickstart:
61
66
62
67
```python
63
68
CONNECTION_STRING = "{deviceConnectionString}"
64
69
```
65
70
66
-
4. Add the following function to print received messages to the console:
71
+
1. Add the following function to print received messages to the console:
print ( "Waiting for C2D messages, press Ctrl-C to exit" )
104
113
105
114
iothub_client_sample_run()
106
115
```
107
116
108
-
7. Save and close **SimulatedDevice.py**file.
117
+
1. Save and close the **SimulatedDevice.py** file.
109
118
110
119
## Get the IoT hub connection string
111
120
112
-
In this article you create a backend service to send cloud-to-device messages through the IoT hub you created in [Send telemetry from a device to an IoT hub](quickstart-send-telemetry-python.md). To send cloud-to-device messages, 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.
121
+
In this article, you create a backend service to send cloud-to-device messages through the IoT hub you created in [Send telemetry from a device to an IoT hub](quickstart-send-telemetry-python.md). To send cloud-to-device messages, 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.
In this section, you create a Python console app that sends cloud-to-device messages to the simulated device app. You need the device ID of the device you added in the [Send telemetry from a device to an IoT hub](quickstart-send-telemetry-python.md) quickstart. You also need the the IoT hub connection string you copied previously in [Get the IoT hub connection string](#get-the-iot-hub-connection-string).
127
+
In this section, you create a Python console app that sends cloud-to-device messages to the simulated device app. You need the device ID of the device you added in the [Send telemetry from a device to an IoT hub](quickstart-send-telemetry-python.md) quickstart. You also need the IoT hub connection string you copied previously in [Get the IoT hub connection string](#get-the-iot-hub-connection-string).
119
128
120
-
1. Using a text editor, create a **SendCloudToDeviceMessage.py**file.
129
+
1. In your working directory, open a command prompt and install the **Azure IoT Hub Service SDK for Python**.
121
130
122
-
2. Add the following `import` statements and variables at the start of the **SendCloudToDeviceMessage.py**file:
131
+
```cmd/sh
132
+
pip install azure-iot-hub
133
+
```
134
+
135
+
1. Using a text editor, create a file named **SendCloudToDeviceMessage.py**.
136
+
137
+
1. Add the following `import` statements and variables at the start of the **SendCloudToDeviceMessage.py** file:
123
138
124
139
```python
125
140
import random
126
141
import sys
127
-
import iothub_service_client
128
-
from iothub_service_client import IoTHubMessaging, IoTHubMessage, IoTHubError
142
+
from azure.iot.hub import IoTHubRegistryManager
129
143
130
-
OPEN_CONTEXT=0
131
-
FEEDBACK_CONTEXT=1
132
-
MESSAGE_COUNT=1
144
+
MESSAGE_COUNT=2
133
145
AVG_WIND_SPEED=10.0
134
146
MSG_TXT="{\"service client sent a message\": %.2f}"
135
147
```
136
148
137
-
3. Add the following code to **SendCloudToDeviceMessage.py**file. Replace the "{iot hub connection string}"and"{device id}" placeholder values with the IoT hub connection string and device ID you noted previously:
149
+
1. Add the following code to **SendCloudToDeviceMessage.py**file. Replace the `{iot hub connection string}`and`{device id}` placeholder values with the IoT hub connection string and device ID you noted previously:
138
150
139
151
```python
140
152
CONNECTION_STRING="{IoTHubConnectionString}"
141
153
DEVICE_ID="{deviceId}"
142
154
```
143
155
144
-
4. Add the following function to print feedback messages to the console:
145
-
146
-
```python
147
-
def open_complete_callback(context):
148
-
print ( 'open_complete_callback called with context: {0}'.format(context) )
0 commit comments