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: 4792
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

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.

Post Reply