Periodic MQTT SSL Connection Failure (AWS IoT Core)

I wonder if anyone has any insights into some unpredictable


errors when using MQTTClient with AWS IoT Core.

I generally get connected and send a 4 or 5 messages successfully, but at some point publish() starts raising these exceptions. They do sometimes get raised on connect() as well.

IoT Core usually receives the first few messages and then logs something like the following:

  "clientId": "...",
  "timestamp": "...",
  "eventType": "disconnected",
  "clientInitiatedDisconnect": false,
  "sessionIdentifier": "...",
  "principalIdentifier": "...",
  "disconnectReason": "CONNECTION_LOST",
  "versionNumber": 37

I’ve tried extending time between messages and increasing the keepalive value with no luck. Maybe there’s a network issue of some sort?

Here’s some simplified source code, if that’s useful:

from mqtt import MQTTClient
from secret import SSID, KEY, thing_id, server, topic
import network
import time
import json

KEY_PATH = "private.der"
CERT_PATH = "certificate.der"

# Init wlan module and connect to network
wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)
print("WiFi Connected ", wlan.ifconfig())

with open(KEY_PATH, "r") as f:
    key1 =
with open(CERT_PATH, "r") as f:
    cert1 =

def connect():
    client = MQTTClient(
        ssl_params={"key": key1, "cert": cert1, "server_side": False},
    print("Connected!", client)
    return client

client = connect()

count = 0
while True:
    count += 1
        payload = json.dumps({"openmvTest": count})
        client.publish("openmv/send", payload)
        print("message sent", payload)
    except Exception as e:


And the output:

MicroPython: v1.18-omv-r13 OpenMV: v4.3.3 HAL: v1.9.0 BOARD: OPENMV4-STM32H743
Type "help()" for more information.
>>> WiFi Connected  ('', '', '', '')
Connected! <MQTTClient object at 30004cb0>
message sent {"openmvTest": 1}
message sent {"openmvTest": 2}
message sent {"openmvTest": 3}
message sent {"openmvTest": 4}

Again, AWS core receives the first few messages, but then I see the $aws/events/presence/disconnected/<client_id> event.

For anyone finding this thread. The connection remains intact if I remove the LCD Shield.

I should have read this thread! :roll_eyes:

Might I request an update to at least one of the documentation areas?

I was waiting to get the WiFi shield to test your code, but I see you already found the problem, great!

You can post an issue here or here so we don’t lose track of this, and link the the this forums post: