- Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathsimple_send_message.py
32 lines (23 loc) · 1.03 KB
/
simple_send_message.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
importos
importasyncio
fromazure.iot.device.aioimportIoTHubDeviceClient
asyncdefmain():
# Fetch the connection string from an environment variable
conn_str=os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
# Create instance of the device client using the connection string
device_client=IoTHubDeviceClient.create_from_connection_string(conn_str)
# Connect the device client.
awaitdevice_client.connect()
# Send a single message
print("Sending message...")
awaitdevice_client.send_message("This is a message that is being sent")
print("Message successfully sent!")
# Finally, shut down the client
awaitdevice_client.shutdown()
if__name__=="__main__":
asyncio.run(main())