0

I'm trying to connect my NodeMCU (ESP8266) device running Micropython to AWS IoT but wwhile running the code I'm getting the following error:

start connecting
Connected:  True
Key and Certificate files Loaded
Connecting to AWS IoT...
Traceback (most recent call last):
    File "<stdin>", line 49, in <module>
    File "<stdin>", line 45, in mqtt_connect
    File "umqtt/simple.py", line 74, in connect
    File "ssl.py", line 1, in wrap_socket
    File "ssl.py", line 1, in wrap_socket
ValueError: invalid key

I read somewhere that key should be in 'der' format, so I converted the downloaded key from AWS to der format using OPENSSL but still I'm getting the same error. Please help.

I'm running the following code:

import time
import network
import urequests
SSID = "SHEKHAR"
PASSWORD = "shekhar123"

print("start connecting")
    
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)

while wlan.isconnected() == False:
    pass

print("Connected: ", wlan.isconnected())


import sys
import machine
from umqtt.simple import MQTTClient

clientId = 'nodemcu'
AWS_ENDPOINT = 'a2xf5sr3dys5uo-ats.iot.us-east-1.amazonaws.com'
PORT = 8883

certfile = '/certificate.crt.der'
with open(certfile, 'rb') as f:
    cert = f.read()
f.close()

keyfile = '/private.key.der'
with open(keyfile, 'rb') as f:
    key = f.read()
f.close()

print("Key and Certificate files Loaded")

SSL_PARAMS = {'key': key, 'cert': cert, 'server_side': False}

# client = MQTTClient(clientId, AWS_ENDPOINT, port=PORT, keepalive=10000, ssl=True, ssl_params=SSL_PARAMS)
def mqtt_connect(client=clientId, endpoint=AWS_ENDPOINT, sslp=SSL_PARAMS):
    mqtt = MQTTClient(client_id=client, server=endpoint, port=8883, keepalive=1200, ssl=True, ssl_params=sslp)
    print("Connecting to AWS IoT...")
    mqtt.connect()
    print("Done")
    return mqtt

mqtt_connect()
1
  • I would guess that the key/value pairs in SSL_PARAMS are not correct but the umqtt.simple library's documentation is poor and seems to provide no clue as to what ssl_params should contain. Also note that you should write wlan.isconnected() == False as not wlan.isconnected(). Never test directly against True/False. Commented Jul 10 at 16:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.