1

I am working on a project that involves connecting an MQTT client to AWS IoT Core. I am using the AWSIoTPythonSDK and paho.mqtt.client libraries in Python. The client is intended to subscribe to two topics and publish the received data to AWS IoT Core.

However, I am encountering a "AWSIoTPythonSDK.exception.AWSIoTExceptions.connectTimeoutException" error after a few seconds of successful operation. The client connects to the MQTT broker (IP: 140.114.89.210, Port: 1883) and subscribes to the topics "wt/generator" and "wt/ws3."

The code successfully connects to the broker and receives data from both topics. However, after a few seconds, it throws the mentioned "connectTimeoutException." I've checked my network settings, security groups, and IAM role permissions, and they seem to be correctly configured. The AWS IoT Core endpoint and credentials are also accurate.

Could someone help me identify the possible reasons for this timeout exception and suggest potential solutions? Are there any additional troubleshooting steps I can take to resolve this issue?

Any guidance or insights would be greatly appreciated. Thank you!

Here's the relevant part of my code:

# [Include code snippet from data_fetcher.py where the MQTT client is initialized and on_connect, on_message functions are defined]

mqttServer = "140.114.89.210"
generator_topic = "wt/generator"
ws3_topic = "wt/ws3"

# AWS IoT Core endpoint (replace with your actual endpoint)
IOT_CORE_ENDPOINT = "xxxxxxxxxxxxxx-ats.iot.us-east-1.amazonaws.com"
# [Other relevant configurations]

client.connect(mqttServer, 1883, 120)
client.loop_forever()

I am pasting the terminal log with the output and also the generated error

python3 data_fetcher.py
Connected with result code 0
{'wind_dir': 339, 'wind_speed': 5}
{'wind_dir': 347, 'wind_speed': 0}
{'wind_dir': 185, 'wind_speed': 5}
{'wind_dir': 89, 'wind_speed': 2}
{'wind_dir': 258, 'wind_speed': 1}
{'wind_dir': 334, 'wind_speed': 7}
{'wind_dir': 61, 'wind_speed': 2}
{'wind_dir': 1, 'wind_speed': 4}
{'wind_dir': 147, 'wind_speed': 3}
{'wind_dir': 104, 'wind_speed': 10}
{'wind_dir': 220, 'wind_speed': 8}
{'wind_dir': 124, 'wind_speed': 4}
{'wind_dir': 352, 'wind_speed': 7}
Connect timed out
Traceback (most recent call last):
  File "data_fetcher.py", line 63, in <module>
    mqttc.loop_forever()
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1164, in _loop
    rc = self.loop_read()
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 1556, in loop_read
    rc = self._packet_read()
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 2439, in _packet_read
    rc = self._packet_handle()
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3033, in _packet_handle
    return self._handle_publish()
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3327, in _handle_publish
    self._handle_on_message(message)
  File "/usr/local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3570, in _handle_on_message
    on_message(self, self._userdata, message)
  File "data_fetcher.py", line 43, in on_message
    client_iot_core.connect()
  File "/home/ec2-user/.local/lib/python3.7/site-packages/AWSIoTPythonSDK/MQTTLib.py", line 520, in connect
    return self._mqtt_core.connect(keepAliveIntervalSecond)
  File "/home/ec2-user/.local/lib/python3.7/site-packages/AWSIoTPythonSDK/core/protocol/mqtt_core.py", line 199, in connect
    raise connectTimeoutException()
AWSIoTPythonSDK.exception.AWSIoTExceptions.connectTimeoutException
  • `AWSIoTPythonSDK.exception.AWSIoTExceptions.connectTimeoutException` does not look like an exception that the `paho` library would throw (it's something that [`AWSIoTMQTTClient`](https://stackoverflow.com/q/57922198/11810946) might throw). Can you please provide the full error (including Stack Trace) - it would also be good if you provide a [minimal, reproducible, example](https://stackoverflow.com/help/minimal-reproducible-example) (and verify that it does, in fact, fail). – Brits Jul 31 '23 at 03:16
  • hello @Brits sir, thank you for your response, I have added the full error please check. – Suyash Gaikwad Jul 31 '23 at 04:34
  • You are receiving a message via MQTT; so `on_message` (line 43 in `data_fetcher.py`) callback runs, this attempts to run `client_iot_core.connect()` and **that** fails. Your issue is not `paho` related so you need to focus your question on the AWS connection (as that is where the issue is). Create a [minimal, reproducible example](https://stackoverflow.com/q/57922198/11810946) that just calls `client_iot_core.connect()` (after doing whatever setup you currently do). – Brits Jul 31 '23 at 05:01

0 Answers0