Page 2 of 3

Re: Connecting to AWS with MQTT

Posted: Wed Jul 10, 2019 2:20 am
by flaviosteimacher
how you generate the certs files?

Re: Connecting to AWS with MQTT

Posted: Tue Sep 03, 2019 8:30 pm
by Jimmy_Hedman
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

Re: Connecting to AWS with MQTT

Posted: Sun Oct 06, 2019 4:06 am
by shafik
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?

Re: Connecting to AWS with MQTT

Posted: Thu Jan 30, 2020 1:07 am
by sspaman
Has anyone used this code recently? I can make the connection but cannot publish. Has there been changes to the AWS IoT requirements?

Re: Connecting to AWS with MQTT

Posted: Thu Jan 30, 2020 8:52 pm
by sspaman
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.

Re: Connecting to AWS with MQTT

Posted: Thu Jan 30, 2020 9:54 pm
by kevinkk525
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?

Re: Connecting to AWS with MQTT

Posted: Thu Jan 30, 2020 10:58 pm
by sspaman
Well, that is a good point and one that I have considered. I'm still working on it.

Re: Connecting to AWS with MQTT

Posted: Fri Jan 31, 2020 1:22 am
by forum_user
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.

Re: Connecting to AWS with MQTT

Posted: Fri Jan 31, 2020 1:37 pm
by sspaman
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

Re: Connecting to AWS with MQTT

Posted: Fri Jan 31, 2020 2:39 pm
by kevinkk525
Did you try using the AWS IOT mqtt with a CPython mqtt client like paho.mqtt?