Trying to make TLS connection to MQTT broker

I’m trying to connect an H7 Plus to a HiveMQ cloud MQTT broker. The only connection option is via TLS, and the documentation for the MQTT library doesn’t provide much guidance about how to create a TLS connection. Through trial and error (all error thus far), I’m trying this code that I found elsewhere in the forum, but no luck.

KEY_PATH = “uberbroker.pem”
CERT_PATH = “uberbroker.der”
with open(KEY_PATH, ‘r’) as f:
key1 = f.read()
with open(CERT_PATH, ‘r’) as f:
cert1 = f.read()

client = MQTTClient(“openmv”, “01d1669a1147487bb24c993bec906228.s2.eu.hivemq.cloud”, port=8883, ssl=True, user=“<MyUser.”, password=“”,ssl_params={ “key”:key1, “cert”:cert1, “server_side”:False })
client.connect()

I generated a private key using openssl, then generated a PEM file, then a DER file.

At this point I have literally no idea what the issue might be.

@iabdalkader

1 Like

Figured it out - I didn’t need the certs and keys at all. Using:

ssl_params={ “server_hostname”: “yourserverdomain.com” }

…worked fine.

If need keys/certs later, note that MicroPython only supports DER format.

1 Like

Thanks - so how would I generate the two needed keys? And it seemed that the above code (which I found elsewhere) gacked when trying to open a binary key file with a UnicodeError, so I tried opening with ‘r+b’. Then I got key errors.

If you need a key and certificate, you generate them with openssl, then you need to convert them both to DER first.

openssl ec -in key.pem -out key.der -outform DER
openssl x509 -in cert.pem -out cert.der -outform DER
2 Likes