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
질문됨 일 년 전458회 조회
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

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
전문가
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인