I want to create/subscribe a new (simulated) device with my local machine in azure IoT Hub.
I am using python 3.6.4 (64bit) on windows7 (64bit) machine and run the code with spyder.
Installed, relevant packages:
- azure-iothub-device-client (1.3.1)
- azure-iothub-service-client (1.3.1)
- spyder (3.2.8)
I followed the steps from here: CreateDeviceIdentity.py
The code snipped:
import sys
import iothub_service_client
CONNECTION_STRING = "myConnectionString"
DEVICE_ID = "pythonDevice_1"
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 = iothub_service_client.IoTHubRegistryManager(CONNECTION_STRING)
primary_key = ""
secondary_key = ""
auth_method = iothub_service_client.IoTHubRegistryManagerAuthMethod.SHARED_PRIVATE_KEY
new_device = iothub_registry_manager.create_device(DEVICE_ID, primary_key, secondary_key, auth_method)
print_device_info("CreateDevice", new_device)
except iothub_service_client.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()
'''
If I run this code I always get the error:
Unexpected error IoTHubRegistryManager.create_device, IoTHubRegistryManagerResult.HTTPAPI_ERROR
I found the (or a similar) error on several pages in the inet but never got a working solution for it. The other code example from the microsoft documentation 1 (SimulatedDevice.py) works fine.
addon: the error can also be reproduced when running the script by command line. The complete error log:
Error: Time:Thu Apr 5 08:59:55 2018 File:C:\release\iot-sdks-internals\release\python\automation\az iotsdk_pytools\src\c\c-utility\adapters\httpapi_winhttp.c Func:HTTPAPI_Init Line:142 WinHttpOpen failed.
Error: Time:Thu Apr 5 08:59:55 2018 File:C:\release\iot-sdks-internals\release\python\automation\aziotsdk_pytools\src\c\c-utility\adapters\httpapi_winhttp.c Func:HTTPAPI_Init Line:142 GetLastError: Falscher Parameter.
Error: Time:Thu Apr 5 08:59:55 2018 File:C:\release\iot-sdks-internals\release\python\automation\aziotsdk_pytools\src\c\c-utility\src\httpapiex.c Func:HTTPAPIEX_ExecuteRequest Line:475 unable to recover sending to a working state
Error: Time:Thu Apr 5 08:59:55 2018 File:C:\release\iot-sdks-internals\release\python\automation\az iotsdk_pytools\src\c\iothub_service_client\src\iothub_registrymanager.c Func:sendHttpRequestCRUD Line:982 HTTPAPIEX_SAS_ExecuteRequest failed Unexpected error IoTHubRegistryManager.create_device, IoTHubRegistryManagerResult.HTTPAPI_ERROR