-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiot_create_device.py
More file actions
43 lines (37 loc) · 2.22 KB
/
Copy pathiot_create_device.py
File metadata and controls
43 lines (37 loc) · 2.22 KB
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
42
43
import sys
import iothub_service_client
from iothub_service_client import IoTHubRegistryManager, IoTHubRegistryManagerAuthMethod
from iothub_service_client import IoTHubDeviceStatus, IoTHubError
CONNECTION_STRING = "HostName=no-66-project.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=gpW4noN/73/8rqx3uRcs9TYylKnTHiYmrdFkrxcePV0="
DEVICE_ID = "FirstPythonDevice" #TBD
def print_device_info(title, iothub_device):
print ( title + ":" )
print ( "iothubDevice.deviceId = {0}".format(iothub_device.deviceId) )
print ( "iothubDevice.primaryKey = {0}".format(iothub_device.primaryKey) )
print ( "iothubDevice.secondaryKey = {0}".format(iothub_device.secondaryKey) )
print ( "iothubDevice.connectionState = {0}".format(iothub_device.connectionState) )
print ( "iothubDevice.status = {0}".format(iothub_device.status) )
print ( "iothubDevice.lastActivityTime = {0}".format(iothub_device.lastActivityTime) )
print ( "iothubDevice.cloudToDeviceMessageCount = {0}".format(iothub_device.cloudToDeviceMessageCount) )
print ( "iothubDevice.isManaged = {0}".format(iothub_device.isManaged) )
print ( "iothubDevice.authMethod = {0}".format(iothub_device.authMethod) )
print ( "" )
def iothub_createdevice():
try:
iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
auth_method = IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY
new_device = iothub_registry_manager.create_device(DEVICE_ID, "", "", auth_method)
print_device_info("CreateDevice", new_device)
except IoTHubError as iothub_error:
print ( "Unexpected error {0}".format(iothub_error) )
return
except KeyboardInterrupt:
print ( "iothub_createdevice stopped" )
if __name__ == '__main__':
print ( "" )
print ( "Python {0}".format(sys.version) )
print ( "Creating device using the Azure IoT Hub Service SDK for Python" )
print ( "" )
print ( " Connection string = {0}".format(CONNECTION_STRING) )
print ( " Device ID = {0}".format(DEVICE_ID) )
iothub_createdevice()