ESP32 port + mqtt_as + mbedtls_ssl_handshake error: -77

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
User avatar
tsjoiner
Posts: 22
Joined: Tue Sep 05, 2017 3:09 pm
Location: Alberta Beach, Alberta, Canada

ESP32 port + mqtt_as + mbedtls_ssl_handshake error: -77

Post by tsjoiner » Sat May 23, 2020 3:24 pm

I am taking Peter Hinche's mqtt_as.py for a test drive. It works like a champ and stays connected for many hours.
Trouble is, connections succeed only 1/10 times or so. Mostly I get this:

Checking WiFi integrity.
Got reliable connection
Connecting to broker.
mbedtls_ssl_handshake error: -77
Traceback (most recent call last):
File "main.py", line 52, in <module>
File "uasyncio/core.py", line 1, in run_until_complete
File "uasyncio/core.py", line 1, in run_until_complete
File "uasyncio/core.py", line 1, in run_until_complete
File "main.py", line 12, in main
File "mqtt_as.py", line 519, in connect
File "mqtt_as.py", line 225, in _connect
OSError: [Errno 5] EIO
MicroPython v1.12-464-gcae77daf0 on 2020-05-22; ESP32 module with ESP32
Type "help()" for more information.

main.py

Code: Select all

from mqtt_as import MQTTClient
from mqtt_as import config  # changed to import from mqtt_as instead of config
import uasyncio as asyncio

def callback(topic, msg, retained):
    print((topic, msg, retained))

async def conn_han(client):
    await client.subscribe('foo_topic', 1)

async def main(client):
    await client.connect()
    n = 0
    while True:
        await asyncio.sleep(5)
        print('publish', n)
        # If WiFi is down the following will pause for the duration.
        await client.publish('result', 'count_{}'.format(n), qos = 1)
        n += 1

with open('key', "r") as f:
    key = f.read()

with open('cert', "r") as f:
    cert = f.read()

config['subs_cb'] = callback
config['connect_coro'] = conn_han

config['ssid'] = 'my-ssid'
config['wifi_pw'] = 'my-wifi-pa$$word'
config['server'] = 'my-mqtt.com'
config['port'] = 8883  # 1883
config['user'] = 'my-mqtt-user_name'
config['password'] = 'my-mqtt-pa$$word'
config['ssl'] = True
config['ssl_params'] = {'cert':cert, 'key':key, 'server_side':False}

# mbedtls_ssl_handshake error: -77
# https://tls.mbed.org/api/ssl_8h.html#ada7521232ac79a0cc64e8f5325fc7574

MQTTClient.DEBUG = True  # Optional: print diagnostic messages
client = MQTTClient(config)
loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(main(client))
finally:
    client.close()  # Prevent LmacRxBlk:1 errors
Soft reset, hardware reset and power cycle all work about the same. Connection success rates seem better if some time elapses between attempts.

User avatar
tsjoiner
Posts: 22
Joined: Tue Sep 05, 2017 3:09 pm
Location: Alberta Beach, Alberta, Canada

Re: ESP32 port + mqtt_as + mbedtls_ssl_handshake error: -77

Post by tsjoiner » Sun May 24, 2020 5:00 pm

More tests using these firmware:
esp32-idf3-20200522-unstable-v1.12-464-gcae77daf0.bin
esp32-idf3-20200516-unstable-v1.12-462-gcd9a8c174.bin
esp32-idf3-20200511-unstable-v1.12-450-gc9611b280.bin

More tests using these boards:
Lolin D32 Pro V2.0 (Wrover)
ESP32-PICO-KIT_V4.1

Connection is made every time using the baked in umqtt but only 10% of time using mqtt_as :?

User avatar
tsjoiner
Posts: 22
Joined: Tue Sep 05, 2017 3:09 pm
Location: Alberta Beach, Alberta, Canada

Re: ESP32 port + mqtt_as + mbedtls_ssl_handshake error: -77

Post by tsjoiner » Sun May 24, 2020 6:13 pm

Got it. Line 31 in mqtt_as.py seems to effect my connection rate.
_DEFAULT_MS = const(20)

bumping this up to 500 gives closer to 99% successful connection rate.
_DEFAULT_MS = const(500)

Not sure what the downside will be, however.

Post Reply