AWS MQTT connection failure

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
nebu_lin
Posts: 1
Joined: Sun May 03, 2020 6:35 pm

AWS MQTT connection failure

Post by nebu_lin » Sun May 03, 2020 7:03 pm

I am trying to connect to connect to aws iot core.. MQTT connection code is this

Code: Select all

CERT_FILE = "/flash/cert.der"
KEY_FILE = "/flash/private.der"
MQTT_CLIENT_ID = "nodething"
MQTT_PORT = 8883
MQTT_TOPIC = "sdk/test/Python"

MQTT_HOST = "**********.iot.******.amazonaws.com "
WIFI_SSID = "*****"
WIFI_PW = "*******"
mqtt_client = None

def pub_msg(msg):
    global mqtt_client
    try:    
        mqtt_client.publish(MQTT_TOPIC, msg)
        print("Sent: " + msg)
    except Exception as e:
        print("Exception publish: " + str(e))
        raise
def connect_mqtt():    
    global mqtt_client

    try:
        with open(KEY_FILE, "r") as f: 
            key = f.read()

        print("Got Key")
            
        with open(CERT_FILE, "r") as f: 
            cert = f.read()

        print("Got Cert")	

        mqtt_client = MQTTClient(client_id=MQTT_CLIENT_ID, server=MQTT_HOST, port=MQTT_PORT, keepalive=5000, ssl=True, ssl_params={"cert":cert, "key":key, "server_side":False})
        mqtt_client.connect()
        print('MQTT Connected')

        
    except Exception as e:
        print('Cannot connect MQTT: ' + str(e))
        raise
But i am getting an error while trying to connect.

"""Connecting WIFI
connected: ('192.168.0.2', '255.255.255.0', '192.168.0.1', '103.199.160.80')
Connecting MQTT
Got Key
Got Cert
Cannot connect MQTT: -2
-2"""

How to solve this error and make the mqtt connection successful...

marioard
Posts: 3
Joined: Fri May 15, 2020 1:22 am

Re: AWS MQTT connection failure

Post by marioard » Fri May 15, 2020 2:35 am

I was having the same problem with the latest releases. It works fine using the V11. I'm currently using the https://micropython.org/resources/firmw ... -v1.11.bin
I tried to use Peter Hinch's async MQTT, but unfortunately, it's based on the latest firmware release, which doesn't work with the certificates.

These are the errors I get:
Using umqtt/simple

Code: Select all

mbedtls_ssl_handshake error: -10
Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
  File "mqtt.py", line 20, in __init__
  File "umqtt/simple.py", line 61, in connect
OSError: [Errno 5] EIO
Using Peter Hinch's async lib:

Code: Select all

mbedtls_ssl_handshake error: -77
Traceback (most recent call last):
  File "<stdin>", line 64, in <module>
  File "uasyncio/core.py", line 1, in run_until_complete
  File "uasyncio/core.py", line 1, in run_until_complete
  File "uasyncio/core.py", line 1, in run_until_complete
  File "<stdin>", line 38, in main
  File "/lib/mqtt_as.py", line 519, in connect
  File "/lib/mqtt_as.py", line 225, in _connect
OSError: [Errno 5] EIO

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: AWS MQTT connection failure

Post by pythoncoder » Fri May 15, 2020 7:48 am

Quite. My README has:
SSL/TLS connectionfor ESP8266. Fails with ssl_handshake_status: -4
SSL/TLS connection for ESP32. Fails with mbedtls_ssl_handshake error: -77
There are known problems with TLS on nonblocking sockets. Until the firmware is fixed my library only supports TLS on the Pyboard D.

TLS is a longstanding problem.
Peter Hinch
Index to my micropython libraries.

Post Reply