ANassar
ANassar

Reputation: 11

Cant connect to AWS IoT Core via MQTT using AWSIoTPythonSDK

I have followed the AWS tutorial step by step. https://aws.amazon.com/premiumsupport/knowledge-center/iot-core-publish-mqtt-messages-python/

I have created the open-ended policy with the *, registered a thing and attached it to the policy, generated, downloaded, and activated the certificates. I have tried to connect and publish to a subscription using both the AWS IoT SDK for Python v2 and the original sdk but neither work. The code I'm using is straight from AWS's demo example connection code but they just wont connect.

While using the AWS IoT SDK for Python v2 I get this error message:

RuntimeError: 1038 (AWS_IO_FILE_VALIDATION_FAILURE): A file was read and the input did not match the expected value

While using the original SDK I get this error message:

TimeoutError: [Errno 60] Operation timed out

The python code I'm using:

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

import time as t
import json
import AWSIoTPythonSDK.MQTTLib as AWSIoTPyMQTT

# Define ENDPOINT, CLIENT_ID, PATH_TO_CERT, PATH_TO_KEY, PATH_TO_ROOT, MESSAGE, TOPIC, and RANGE
ENDPOINT = "XXXXX-ats.iot.ap-southeast-2.amazonaws.com"
CLIENT_ID = "testDevice"
PATH_TO_CERT = "certs/XXXX-certificate.pem.crt"
PATH_TO_KEY = "certs/XXXX-private.pem.key"
PATH_TO_ROOT = "certs/root.pem"
MESSAGE = "Hello World"
TOPIC = "test/testing"
RANGE = 20

myAWSIoTMQTTClient = AWSIoTPyMQTT.AWSIoTMQTTClient(CLIENT_ID)
myAWSIoTMQTTClient.configureEndpoint(ENDPOINT, 8883)
myAWSIoTMQTTClient.configureCredentials(PATH_TO_ROOT, PATH_TO_KEY, PATH_TO_CERT)

myAWSIoTMQTTClient.connect()
print('Begin Publish')
for i in range (RANGE):
    data = "{} [{}]".format(MESSAGE, i+1)
    message = {"message" : data}
    myAWSIoTMQTTClient.publish(TOPIC, json.dumps(message), 1) 
    print("Published: '" + json.dumps(message) + "' to the topic: " + "'test/testing'")
    t.sleep(0.1)
print('Publish End')
myAWSIoTMQTTClient.disconnect()

(I censored the endpoint and the certificate ID)

(I'm using a macbook air and on a public school network)

Upvotes: 0

Views: 1288

Answers (3)

Saidi Mahmud
Saidi Mahmud

Reputation: 1

I've changed OpenSSL version to 1.1.1 and created device certs, and it didn't throw this message. If you're using OpenSSL 3.x, you can try with OpenSSL 1.1.1

Upvotes: 0

MQTT works with the particular port number 8883 which you will configure in myAWSIoTMQTTClient.configureEndpoint(ENDPOINT, 8883).

In one of my AWS IOT course I learnt that some network administrators will block all ports which are not commonly used, to avoid unwanted traffic and MQTT is something which is specific to IOT industry. This could be the reason why it did not worked when you tried in school network and it worked when you tried in your home.

Upvotes: 0

ANassar
ANassar

Reputation: 11

I went home and tested it and it works perfectly. If you have this same problem, try troubleshooting your network. I think my school blocks MQTT or something.

Upvotes: 1

Related Questions