Connecting to AWS with MQTT

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
flaviosteimacher
Posts: 1
Joined: Wed Jul 10, 2019 2:18 am

Re: Connecting to AWS with MQTT

Post by flaviosteimacher » Wed Jul 10, 2019 2:20 am

how you generate the certs files?

Jimmy_Hedman
Posts: 1
Joined: Fri Nov 18, 2016 6:16 am

Re: Connecting to AWS with MQTT

Post by Jimmy_Hedman » Tue Sep 03, 2019 8:30 pm

To be able to connect to AWS with an ESP8266 you need to convert the certificatefile and the keyfile to DER format. See https://github.com/micropython/micropyt ... -453802236

shafik
Posts: 1
Joined: Wed Oct 02, 2019 11:49 am

Re: Connecting to AWS with MQTT

Post by shafik » Sun Oct 06, 2019 4:06 am

IndexError: list index out of range
This is my code:

Code: Select all

# certificate and private key 
KEY_PATH = "/619e3d582c-private.pem.key"
CERT_PATH = "/619e3d582c-certificate.pem"

with open(KEY_PATH, 'r') as f:
  PVT_KEY = f.read()
  
with open(CERT_PATH, 'r') as f:
  CERT_KEY = f.read()


client = MQTTClient(client_id="esp32_micropython_shafik",
                   server="xxxxxxxxxxxxxxxx-ats.iot.eu-west-1.amazonaws.com",
                   port = 8883,
                   keepalive = 10000,
                   ssl = True,
                   ssl_params = {
                     "cert": CERT_KEY,
                     "key": PVT_KEY,
                     "server_side":False
                   } )
I am getting continuously error in umqtt/simple.py in 57 no line, where socket.getaddrinfo(hostname, port)[0][-1] unable to parse ip address of the host name and returns a empty list. so it is giving IndexError: list index out of range !
How can I solve this?

sspaman
Posts: 16
Joined: Fri Nov 02, 2018 5:03 pm

Re: Connecting to AWS with MQTT

Post by sspaman » Thu Jan 30, 2020 1:07 am

Has anyone used this code recently? I can make the connection but cannot publish. Has there been changes to the AWS IoT requirements?

sspaman
Posts: 16
Joined: Fri Nov 02, 2018 5:03 pm

Re: Connecting to AWS with MQTT

Post by sspaman » Thu Jan 30, 2020 8:52 pm

A couple hard days on the ide and I solved my problem. Turns out AWS IoT wouldn't post my published topic until I issued client.disconnect(). Strange since none of the code examples I found ever issued the disconnect routine. Anyway, it works.

I have working code if anyone is interested.

kevinkk525
Posts: 765
Joined: Sat Feb 03, 2018 7:02 pm

Re: Connecting to AWS with MQTT

Post by kevinkk525 » Thu Jan 30, 2020 9:54 pm

if you need to disconnect then you don't have working code.. how is that code supposed to work when you subscribe to a message and wait for a message on that topic?
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

sspaman
Posts: 16
Joined: Fri Nov 02, 2018 5:03 pm

Re: Connecting to AWS with MQTT

Post by sspaman » Thu Jan 30, 2020 10:58 pm

Well, that is a good point and one that I have considered. I'm still working on it.

forum_user
Posts: 4
Joined: Thu Jan 30, 2020 11:14 pm

Re: Connecting to AWS with MQTT

Post by forum_user » Fri Jan 31, 2020 1:22 am

sspaman wrote:
Thu Jan 30, 2020 8:52 pm
I have working code if anyone is interested.
I would be interested. I'm encountering a similar issue.

sspaman
Posts: 16
Joined: Fri Nov 02, 2018 5:03 pm

Re: Connecting to AWS with MQTT

Post by sspaman » Fri Jan 31, 2020 1:37 pm

Hi,
As pointed out above this is "working" code only in the sense that the published message is received and shown in the AWS IoT dashboard console. And this is the case only after the mqtt_client.disconnect() is sent. I think this might be a problem on the AWS IoT side. Maybe with the permissions? Although I have tried many variations of the permissions nothing seems to make a difference. Except some permissions prevent connection all together.

Anyhow, here is the code I am running to connect and publish. Maybe you can help me solve this.

Code: Select all


from umqtt.robust import MQTTClient
import peripherals as prph
import sys


def broker_connect_aws():
    #  Certificates, keys and MQTT variables
    certificate_file = "/xxxx1be767-certificate.pem.crt"
    private_key_file = "/xxxx1be767-private.pem.key"
    mqtt_client_id = "esp32"
    mqtt_port = 8883
    mqtt_topic = "ESP32_Temp_F"
    mqtt_host = "xxxx80on2gc4gt-ats.iot.us-west-2.amazonaws.com"  # Rest API Endpoint
    #  Open and read certificates and keys
    with open(private_key_file, "r") as f:
        private_key = f.read()

    with open(certificate_file, "r") as f:
        certificate = f.read()
    #  Set up MQTT client
    mqtt_client = MQTTClient(client_id=mqtt_client_id,
                             server=mqtt_host,
                             port=mqtt_port,
                             keepalive=10000,
                             ssl=True,
                             ssl_params={"cert": certificate,
                                         "key": private_key,
                                         "server_side": False
                                         })
    # Connect to AWS IoT Broker and Publish
    try:
        print('try to connect to mqtt on aws')
        mqtt_client.connect()
        print('****PUBLISH TO AWS IoT****')
        mqtt_client.publish(mqtt_topic, "{}F".format(str(prph.esp32_temp())))  #  Read & Pub ESP32 internal temp
        mqtt_client.disconnect()  # <-- without this line the code connects and publishes but the message doesn't seem to be received
    except Exception as e:
        print('Could not connect to AWS IoT Broker {}{}'.format(type(e).__name__, e))
        print('<<<Going to esp32 Deep Sleep mode>>>')
        prph.esp32_dpslp(10)
        sys.exit()

Regards,
Chris

kevinkk525
Posts: 765
Joined: Sat Feb 03, 2018 7:02 pm

Re: Connecting to AWS with MQTT

Post by kevinkk525 » Fri Jan 31, 2020 2:39 pm

Did you try using the AWS IOT mqtt with a CPython mqtt client like paho.mqtt?
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply