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