0
votes

I got my RaspberryPi registered as a Thing in Aws IoT.

Im executing this python script:

from __future__ import print_function
import sys
import ssl
import time
import datetime
import logging, traceback
import paho.mqtt.client as mqtt

IoT_protocol_name = "x-amzn-mqtt-ca"
aws_iot_endpoint = "a132ag21b212..." # <random>.iot.<region>.amazonaws.com
url = "https://{}".format(aws_iot_endpoint)

ca = "/root/awsiotNew/root-CA.crt" 
cert = "/root/awsiotNew/6d9asad65a-certificate.pem.crt"
private = "/root/awsiotNew/6d9aadas5a-private.pem.key"

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(log_format)
logger.addHandler(handler)

def ssl_alpn():
    try:
        #debug print opnessl version
        logger.info("open ssl version:{}".format(ssl.OPENSSL_VERSION))
        ssl_context = ssl.create_default_context()
        ssl_context.set_alpn_protocols([IoT_protocol_name])
        ssl_context.load_verify_locations(cafile=ca)
        ssl_context.load_cert_chain(certfile=cert, keyfile=private)

        return  ssl_context
    except Exception as e:
        print("exception ssl_alpn()")
        raise e

if __name__ == '__main__':
    topic = "test/date"
    try:
        mqttc = mqtt.Client()
        ssl_context= ssl_alpn()
        mqttc.tls_set_context(context=ssl_context)
        logger.info("start connect")
        mqttc.connect(aws_iot_endpoint, port=8883)
        logger.info("connect success")
        mqttc.loop_start()

        while True:
            now = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
            logger.info("try to publish into:{}".format(now))
            mqttc.publish(topic, now)
            time.sleep(1)

    except Exception as e:
        logger.error("exception main()")
        logger.error("e obj:{}".format(vars(e)))
        logger.error("message:{}".format(e.message))
        traceback.print_exc(file=sys.stdout)

When I now try to run this script via putty on my raspberry it seems to work correctly since The messages I get in the raspberry console look like the following:

root@DietPi:~/awsiotNew# python3 pythoniotnew.py
2020-06-25 12:08:52,944 - root - INFO - open ssl version:OpenSSL 1.1.1d  10 Sep 2019
2020-06-25 12:08:52,947 - root - INFO - start connect
2020-06-25 12:08:53,019 - root - INFO - connect success
2020-06-25 12:08:53,020 - root - INFO - try to publish into:2020-06-25T12:08:53
2020-06-25 12:08:54,022 - root - INFO - try to publish into:2020-06-25T12:08:54
2020-06-25 12:08:55,024 - root - INFO - try to publish into:2020-06-25T12:08:55
2020-06-25 12:08:56,026 - root - INFO - try to publish into:2020-06-25T12:08:56
2020-06-25 12:08:57,027 - root - INFO - try to publish into:2020-06-25T12:08:57

Now I want to check, if the messages are correctly inserted to aws IoT - so I followed these steps: Aws Guide view mqtt messages. Basically I just need to insert a '#' as topic, so every received message gets displayed. But that is not the case. I dont get any messages displayed in the console. Screenshot of my subscribed topics in the mqtt client / Aws IoT Console

How can I make sure that the connection is working?

Thanks!

1
It looks no problems with your code. There might be something missing in the settings with AWS IoT. You can use CloudWatch for logs. docs.aws.amazon.com/iot/latest/developerguide/cwl-format.htmlshimo

1 Answers

0
votes

I figured out what the problem was:

I did not know that I have to attach the thing and the policy to a certificate. That seems like it was the problem, now I am getting the messages straight to my test console.

If someone runs into this issue, check out this Blog Entry which helped me fix it.