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
asked a year ago450 views
2 Answers
1
Accepted Answer

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
answered a year ago
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
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions