| author | SoniaLopezBravo | ||||
|---|---|---|---|---|---|
| ms.author | sonialopez | ||||
| ms.service | azure-iot-hub | ||||
| ms.devlang | python | ||||
| ms.topic | include | ||||
| ms.date | 12/19/2024 | ||||
| ms.custom |
|
This section describes how to receive cloud-to-device messages.
The IoTHubDeviceClient class includes methods to create a synchronous connection from a device to an Azure IoT Hub and receive messages from IoT Hub.
The azure-iot-device library must be installed to create device applications.
pip install azure-iot-device
For a Python-based device application to receive cloud-to-device messages, it must connect to IoT Hub and then set up a callback message handler to process incoming messages from IoT Hub.
Add this code to import the IoTHubDeviceClient functions from the azure.iot.device SDK.
from azure.iot.device import IoTHubDeviceClientA device app can authenticate with IoT Hub using the following methods:
- Shared access key
- X.509 certificate
[!INCLUDE iot-authentication-device-connection-string.md]
To connect a device to IoT Hub:
- Call create_from_connection_string to add the device primary connection string.
- Call connect to connect the device client.
For example:
# Add your IoT hub primary connection string
CONNECTION_STRING = "{Device primary connection string}"
device_client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
# Connect the client
device_client.connect()[!INCLUDE iot-hub-howto-auth-device-cert-python]
IoTHubDeviceClient will by default attempt to reestablish a dropped connection. Reconnection behavior is governed by the IoTHubDeviceClient connection_retry and connection_retry_interval parameters.
Create a message handler function to process incoming messages to the device. This will be assigned by on_message_received (next step) as the callback message handler.
In this example, message_handler is called when a message is received. The message properties (.items) are printed to the console using a loop.
def message_handler(message):
global RECEIVED_MESSAGES
RECEIVED_MESSAGES += 1
print("")
print("Message received:")
# print data from both system and application (custom) properties
for property in vars(message).items():
print (" {}".format(property))
print("Total calls received: {}".format(RECEIVED_MESSAGES))Use the on_message_received method to assign the message handler method.
In this example, a message handler method named message_handler is attached to the IoTHubDeviceClient client object. The client object waits to receive a cloud-to-device message from an IoT Hub. This code waits up to 300 seconds (5 minutes) for a message, or exits if a keyboard key is pressed.
try:
# Attach the handler to the client
client.on_message_received = message_handler
while True:
time.sleep(300)
except KeyboardInterrupt:
print("IoT Hub C2D Messaging device sample stopped")
finally:
# Graceful exit
print("Shutting down IoT Hub Client")
client.shutdown()Receive Message - Receive Cloud-to-Device (C2D) messages sent from the Azure IoT Hub to a device.
This section describes how to send a cloud-to-device message. A solution backend application connects to an IoT Hub and messages are sent to IoT Hub encoded with a destination device. IoT Hub stores incoming messages to its message queue, and messages are delivered from the IoT Hub message queue to the target device.
The IoTHubRegistryManager class exposes all methods required to create a backend application to interact with cloud-to-device messages from the service. The azure-iot-hub library must be installed to create backend service applications.
pip install azure-iot-hub
Add the following import statement. IoTHubRegistryManager includes APIs for IoT Hub Registry Manager operations.
from azure.iot.hub import IoTHubRegistryManagerYou can connect a backend service to IoT Hub using the following methods:
- Shared access policy
- Microsoft Entra
[!INCLUDE iot-authentication-service-connection-string.md]
Connect to IoT hub using from_connection_string.
For example:
IoTHubConnectionString = "{IoT hub service connection string}"
registry_manager = IoTHubRegistryManager.from_connection_string(IoTHubConnectionString)[!INCLUDE iot-hub-howto-connect-service-iothub-entra-python]
Use send_c2d_message to send a message through the cloud (IoT Hub) to the device.
send_c2d_message uses these parameters:
deviceID- The string identifier of the target device.message- The cloud-to-device message. The message is of typestr(string).properties- An optional collection of properties of typedict. Properties can contain application properties and system properties. The default value is{}.
This example sends a test message to the target device.
# define the device ID
deviceID = "Device-1"
# define the message
message = "{\"c2d test message\"}"
# include optional properties
props={}
props.update(messageId = "message1")
props.update(prop1 = "test property-1")
props.update(prop1 = "test property-2")
prop_text = "Test message"
props.update(testProperty = prop_text)
# send the message through the cloud (IoT Hub) to the device
registry_manager.send_c2d_message(deviceID, message, properties=props)The Azure IoT SDK for Python provides a working sample of a service app that demonstrates how to send a cloud-to-device message. For more information, see send_message.py demonstrates how to send a cloud-to-device message.