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
* 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
57
```python
54
58
import threading
@@ -57,27 +61,31 @@ In this section, you create a Python console app to simulate the device and rece
57
61
RECEIVED_MESSAGES = 0
58
62
```
59
63
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:
64
+
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
65
62
66
```python
63
67
CONNECTION_STRING = "{deviceConnectionString}"
64
68
```
65
69
66
-
4. Add the following function to print received messages to the console:
70
+
1. Add the following function to print received messages to the console:
print ( "IoTHubDeviceClient waiting for commands, press Ctrl-C to exit" )
104
112
105
113
iothub_client_sample_run()
106
114
```
107
115
108
-
7. Save and close **SimulatedDevice.py**file.
116
+
1. Save and close the **SimulatedDevice.py** file.
109
117
110
118
## Get the IoT hub connection string
111
119
@@ -117,65 +125,56 @@ In this article you create a backend service to send cloud-to-device messages th
117
125
118
126
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).
119
127
120
-
1. Using a text editor, create a **SendCloudToDeviceMessage.py**file.
128
+
1. In your working directory, open a command prompt and install the **Azure IoT Hub Service SDK for Python**.
129
+
130
+
```cmd/sh
131
+
pip install azure-iot-hub
132
+
```
121
133
122
-
2. Add the following `import` statements and variables at the start of the **SendCloudToDeviceMessage.py**file:
134
+
1. Using a text editor, create a file named **SendCloudToDeviceMessage.py**.
135
+
136
+
1. Add the following `import` statements and variables at the start of the **SendCloudToDeviceMessage.py** file:
123
137
124
138
```python
125
139
import random
126
140
import sys
127
-
import iothub_service_client
128
-
from iothub_service_client import IoTHubMessaging, IoTHubMessage, IoTHubError
141
+
from azure.iot.hub import IoTHubRegistryManager
129
142
130
-
OPEN_CONTEXT=0
131
-
FEEDBACK_CONTEXT=1
132
-
MESSAGE_COUNT=1
143
+
MESSAGE_COUNT=2
133
144
AVG_WIND_SPEED=10.0
134
145
MSG_TXT="{\"service client sent a message\": %.2f}"
135
146
```
136
147
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:
148
+
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
149
139
150
```python
140
151
CONNECTION_STRING="{IoTHubConnectionString}"
141
152
DEVICE_ID="{deviceId}"
142
153
```
143
154
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