Very slow SSL connection to MQTT broker

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
dansiwiec
Posts: 3
Joined: Wed Dec 16, 2020 4:09 am

Very slow SSL connection to MQTT broker

Post by dansiwiec » Wed Dec 16, 2020 4:23 am

Hi everyone, this is my first post on this forum and I'm excited about becoming part of this community.

I've been working on connecting my ESP8266 to AWS IoT over SSL. I've managed to overcome all the obstacles related to certificates and my code 'works'. The only issue is that the initial connection initialization takes about 30 seconds. As my use case is to send a single measurement to the broker, go to deep sleep, and send it again, the MQTT connection is re-established every time.
I tested the same solution against a local mosquitto, without SSL and it works like a charm. (Un)fortunately, AWS only allows TLS connections.

My code is very simple:

Code: Select all

from umqtt.simple import MQTTClient

# Read cert and private key files
certificate = ...
private_key = ...

mqtt_client = MQTTClient(
            client_id=mqtt_client_id,
            server=mqtt_host,
            port=8883,
            ssl=True,
            ssl_params={
              "cert": certificate,
              "key": private_key
            }
        )
print('Connecting to AWS IoT Core...')
mqtt_client.connect()
client.publish('foo', 'bar', qos=1)

Does anyone have suggestions on how to improve this?

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

Re: Very slow SSL connection to MQTT broker

Post by pythoncoder » Thu Dec 17, 2020 6:03 am

I rather think this has been reported before, and that the conclusion was that the heavy-duty encryption involved simply taxes the CPU excessively. The solution may only be to use better (more expensive) hardware. You've done better than me, the only platform I've persuaded to run SSL/TLS is the Pyboard D, which worked like a charm.
Peter Hinch
Index to my micropython libraries.

User avatar
dansiwiec
Posts: 3
Joined: Wed Dec 16, 2020 4:09 am

Re: Very slow SSL connection to MQTT broker

Post by dansiwiec » Thu Dec 17, 2020 6:53 am

Thanks, Peter. Yes, I had a feeling this might have been too much for the simple ESP8266.

I ordered an ESP32, so I'll try on that board for comparison and will report back here. Thanks!

User avatar
dansiwiec
Posts: 3
Joined: Wed Dec 16, 2020 4:09 am

Re: Very slow SSL connection to MQTT broker

Post by dansiwiec » Sat Dec 19, 2020 7:33 am

Reporting back - I tried the same code on ESP32 which arrived today. The SSL handshake time went from 30 seconds on the ESP8266 down to 2 seconds on the ESP32.

marine_hm
Posts: 16
Joined: Mon Oct 18, 2021 10:45 am
Location: USA: Eastern NC
Contact:

Re: Very slow SSL connection to MQTT broker

Post by marine_hm » Sat Oct 23, 2021 12:02 am

Dan; I am new to MicroPython. Coming over from Arduino programming. Just a hobyist!

I have had several projects where I used the ESP8266, published to a MQTT broker residing on my RaspberryPi which was connected to my home router. Everything worked well.

But what I'm understanding from your post; Microphython is rather slow on an ESP8266, or just the initial connection to the MQTT broker?

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

Re: Very slow SSL connection to MQTT broker

Post by pythoncoder » Sun Oct 24, 2021 8:27 am

Micropython is quite fast on ESP8266, but the device has limited RAM. I don't know the details but the specific task of establishing an encrypted TLS connection is known to be slow on ESP8266.
Peter Hinch
Index to my micropython libraries.

Post Reply