error on establishing mqtt connection to IoT Core AwsCrtError: AWS_ERROR_MQTT_UNEXPECTED_HANGUP

0

I created a thing and downloaded the connectivity package to my raspberry pi. I used the Python flavor.
Running the default pubsub.py example works fine, that is no policy or certificate issue.

Now I wanted to create a python only solution that is not triggered by some cli tool. I used below code that I took from knowledge center but it gives me "AwsCrtError: AWS_ERROR_MQTT_UNEXPECTED_HANGUP error."

from awscrt import io, mqtt, auth, http
from awsiot import mqtt_connection_builder
import time as t
import json

# Define ENDPOINT, CLIENT_ID, PATH_TO_CERTIFICATE, PATH_TO_PRIVATE_KEY, PATH_TO_AMAZON_ROOT_CA_1, MESSAGE, TOPIC
ENDPOINT = "xxxx-ats.iot.eu-west-1.amazonaws.com"
CLIENT_ID = "mything"
PATH_TO_CERTIFICATE = "/home/pi/iotexample/connect_device_package/mything.cert.pem"
PATH_TO_PRIVATE_KEY = "/home/pi/iotexample/connect_device_package/mything.private.key"
PATH_TO_AMAZON_ROOT_CA_1 = "/home/pi/iotexample/connect_device_package/root-CA.crt"
TOPIC = "topic/#"

if __name__ == '__main__':
    # Spin up resources
    event_loop_group = io.EventLoopGroup(1)
    host_resolver = io.DefaultHostResolver(event_loop_group)
    client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
    mqtt_connection = mqtt_connection_builder.mtls_from_path(
                endpoint=ENDPOINT,
                cert_filepath=PATH_TO_CERTIFICATE,
                pri_key_filepath=PATH_TO_PRIVATE_KEY,
                client_bootstrap=client_bootstrap,
                ca_filepath=PATH_TO_AMAZON_ROOT_CA_1,
                client_id=CLIENT_ID,
                clean_session=False,
                keep_alive_secs=6
                )
    print("Connecting to {} with client ID '{}'...".format(
            ENDPOINT, CLIENT_ID))
    # Make the connect() call
    connect_future = mqtt_connection.connect()
    # Future.result() waits until a result is available
    connect_future.result()
    print("Connected!")

Anybody got an idea why?

AWS
Marco
已提问 1 年前460 查看次数
2 回答
1
已接受的回答

Ah, this is a policy issue...I changed the client_id and the default policy was scoped to it... Thanks for your comment Philipp

AWS
Marco
已回答 1 年前
1

Hi,

you can try to find more information in CloudWatch logs. You must enable logging for IoT Core.

Some other things to take a look at:

  • Your device certificate is registered in IoT Core?
  • An IoT policy is attached to the device certificate?
  • The policy permits connecting with the client id mything?
  • The root CA you are using is the Amazon Root CA for ATS endpoints?

Please also follow the troubleshooting instructions.

Cheers,
Philipp

AWS
专家
已回答 1 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则