Page 1 of 1

Very slow SSL connection to MQTT broker

Posted: Wed Dec 16, 2020 4:23 am
by dansiwiec
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?

Re: Very slow SSL connection to MQTT broker

Posted: Thu Dec 17, 2020 6:03 am
by pythoncoder
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.

Re: Very slow SSL connection to MQTT broker

Posted: Thu Dec 17, 2020 6:53 am
by dansiwiec
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!

Re: Very slow SSL connection to MQTT broker

Posted: Sat Dec 19, 2020 7:33 am
by dansiwiec
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.

Re: Very slow SSL connection to MQTT broker

Posted: Sat Oct 23, 2021 12:02 am
by marine_hm
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?

Re: Very slow SSL connection to MQTT broker

Posted: Sun Oct 24, 2021 8:27 am
by pythoncoder
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.