forked from dm4p385/iot-smartcities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulatedDevice.py
41 lines (31 loc) · 1.19 KB
/
SimulatedDevice.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
33
34
35
36
37
38
39
40
41
import time
from azure.iot.device import IoTHubDeviceClient
RECEIVED_MESSAGES = 0
CONNECTION_STRING = "HostName=IoTBITS.azure-devices.net;DeviceId=ROAD1;SharedAccessKey=4FCmKfKnMpVX1Ns0rScaupILu6MxClbZIAIoTIRWSZs="
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))
def main():
print ("Starting the Python IoT Hub C2D Messaging device sample...")
# Instantiate the client
client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
print ("Waiting for C2D messages, press Ctrl-C to exit")
try:
# Attach the handler to the client
client.on_message_received = message_handler
while True:
time.sleep(1000)
except KeyboardInterrupt:
print("IoT Hub C2D Messaging device sample stopped")
finally:
# Graceful exit
print("Shutting down IoT Hub Client")
client.shutdown()
if __name__ == '__main__':
main()