From 9b883f5d23bf76ce12ce142911523d4ca54d8a59 Mon Sep 17 00:00:00 2001 From: Khrystyna Olkhovetska Date: Wed, 28 Feb 2024 04:24:07 +0200 Subject: [PATCH] umqtt.simple: Fix MQTTClient publish for connection disruption. Method now handles OSError: -1 after many packets, attempting reconnection. Issue #754 Signed-off-by: Khrystyna Olkhovetska --- micropython/umqtt.simple/umqtt/simple.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/micropython/umqtt.simple/umqtt/simple.py b/micropython/umqtt.simple/umqtt/simple.py index e84e585c4..79b526808 100644 --- a/micropython/umqtt.simple/umqtt/simple.py +++ b/micropython/umqtt.simple/umqtt/simple.py @@ -133,7 +133,15 @@ def publish(self, topic, msg, retain=False, qos=0): self.pid += 1 pid = self.pid struct.pack_into("!H", pkt, 0, pid) - self.sock.write(pkt, 2) + try: + self.sock.write(pkt, 2) + except OSError as e: + if e.args[0] == -1: + print("OSError: -1 occurred. Reconnecting...") + self.connect() + self.sock.write(pkt, 2) + else: + raise self.sock.write(msg) if qos == 1: while 1: