Python SDK Unable to Connect to Azure IoT Hub

Foong, Chun Sheong 31 Reputation points
2021-05-07T06:00:18.717+00:00

Hi,

I created a python module with VSCode extension (Azure IoT Edge: New Iot Edge Solution -> Python module) and tried to deploy onto an edge device I have. However, it fails to connect to IoTHub after i deploy with the error below (obtained with "sudo iotedge logs pythonmodule":

IoT Hub Client for Python
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/common/mqtt_transport.py", line 395, in connect
    host=self._hostname, port=8883, keepalive=self._keep_alive
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 941, in connect
    return self.reconnect()
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1075, in reconnect
    sock = self._create_socket_connection()
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3546, in _create_socket_connection
    return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
  File "/usr/local/lib/python3.7/socket.py", line 728, in create_connection
    raise err
  File "/usr/local/lib/python3.7/socket.py", line 716, in create_connection
    sock.connect(sa)
socket.timeout: timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/iothub/aio/async_clients.py", line 33, in handle_result
    return await callback.completion()
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/common/async_adapter.py", line 94, in completion
    return await self.future
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/common/pipeline/pipeline_stages_mqtt.py", line 193, in _run_op
    self.transport.connect(password=password)
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/common/mqtt_transport.py", line 417, in connect
    raise exceptions.ConnectionFailedError(cause=e)
azure.iot.device.common.transport_exceptions.ConnectionFailedError: ConnectionFailedError(None) caused by timeout('timed out')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./main.py", line 71, in <module>
    loop.run_until_complete(main())
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 587, in run_until_complete
    return future.result()
  File "./main.py", line 23, in main
    await module_client.connect()
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/aio/patch_documentation.py", line 146, in connect
    return await super(IoTHubModuleClient_, self).connect()
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/iothub/aio/async_clients.py", line 187, in connect
    await handle_result(callback)
  File "/usr/local/lib/python3.7/site-packages/azure/iot/device/iothub/aio/async_clients.py", line 37, in handle_result
    raise exceptions.ConnectionFailedError(message="Could not connect to IoTHub", cause=e)
azure.iot.device.exceptions.ConnectionFailedError: ConnectionFailedError('Could not connect to IoTHub') caused by ConnectionFailedError(None)
Unexpected error ConnectionFailedError('Could not connect to IoTHub') caused by ConnectionFailedError(None)

The code I used is the default code created with Azure IoT Extension on VSCode:

module_client = IoTHubModuleClient.create_from_edge_environment()
await module_client.connect()

What should I do to make it work?

Azure IoT Edge
Azure IoT Edge
An Azure service that is used to deploy cloud workloads to run on internet of things (IoT) edge devices via standard containers.
387 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
695 questions
1 vote

Accepted answer
  1. Satish 13,631 Reputation points Microsoft Employee
    2021-05-12T22:03:19.267+00:00

    Hello @Foong, Chun Sheong I have validated the document and was able to send the telemetry messages to the IoTHub via Python SDK.

    The below images show the telemetry being sent to IoTHub.

    96096-image.png

    The below message shows monitoring of the telemetry messages received by IoTHub.

    96068-image.png

    Please make use of the below python code, which is from main.py file of the template code generated by the VS code+ Tutorial.

    96030-mainpy.txt

    Observation: The same error message appeared on my device logs only once and because of the device restart and then it immediately disappeared. Finally I can see telemetry flowing.

    Using commands on device side: sudo iotedge logs <pythonmodulename>

    Please comment in the below section if you need further help in this matter.

    No comments

1 additional answer

Sort by: Most helpful
  1. Foong, Chun Sheong 31 Reputation points
    2021-05-18T11:19:36.54+00:00

    Hi @Satish , yes, I verified that the issue was due to the hostname setting that I set at config.yml. I changed it to its ip address instead of hostname as I faced some TLS issue while trying to connect my Leaf console to the device; as a result that caused the error I mentioned above. Changing it back to hostname fixes the error above and I had to resort to hardcoding my hostname to IP address mapping in order to solve the TLS issue i mentioned prior. Still, thanks a lot for your help.

    No comments