Move to umqtt.robust
Add more info about device Freeze lib with mpy-cross Some code factorization
This commit is contained in:
BIN
code/lib/bme280_i2c.mpy
Normal file
BIN
code/lib/bme280_i2c.mpy
Normal file
Binary file not shown.
BIN
code/lib/umqtt/robust.mpy
Normal file
BIN
code/lib/umqtt/robust.mpy
Normal file
Binary file not shown.
43
code/lib/umqtt/robust.py
Normal file
43
code/lib/umqtt/robust.py
Normal file
@ -0,0 +1,43 @@
|
||||
import utime
|
||||
from . import simple
|
||||
|
||||
class MQTTClient(simple.MQTTClient):
|
||||
|
||||
DELAY = 2
|
||||
DEBUG = False
|
||||
|
||||
def delay(self, i):
|
||||
utime.sleep(self.DELAY)
|
||||
|
||||
def log(self, in_reconnect, e):
|
||||
if self.DEBUG:
|
||||
if in_reconnect:
|
||||
print("mqtt reconnect: %r" % e)
|
||||
else:
|
||||
print("mqtt: %r" % e)
|
||||
|
||||
def reconnect(self):
|
||||
i = 0
|
||||
while 1:
|
||||
try:
|
||||
return super().connect(False)
|
||||
except OSError as e:
|
||||
self.log(True, e)
|
||||
i += 1
|
||||
self.delay(i)
|
||||
|
||||
def publish(self, topic, msg, retain=False, qos=0):
|
||||
while 1:
|
||||
try:
|
||||
return super().publish(topic, msg, retain, qos)
|
||||
except OSError as e:
|
||||
self.log(False, e)
|
||||
self.reconnect()
|
||||
|
||||
def wait_msg(self):
|
||||
while 1:
|
||||
try:
|
||||
return super().wait_msg()
|
||||
except OSError as e:
|
||||
self.log(False, e)
|
||||
self.reconnect()
|
BIN
code/lib/umqtt/simple.mpy
Normal file
BIN
code/lib/umqtt/simple.mpy
Normal file
Binary file not shown.
@ -1,11 +1,3 @@
|
||||
# Copyright (c) 2019, Pycom Limited.
|
||||
#
|
||||
# This software is licensed under the GNU GPL version 3 or any
|
||||
# later version, with permitted additional terms. For more information
|
||||
# see the Pycom Licence v1.0 document supplied with this file, or
|
||||
# available at https://www.pycom.io/opensource/licensing
|
||||
#
|
||||
|
||||
import usocket as socket
|
||||
import ustruct as struct
|
||||
from ubinascii import hexlify
|
||||
@ -21,7 +13,8 @@ class MQTTClient:
|
||||
port = 8883 if ssl else 1883
|
||||
self.client_id = client_id
|
||||
self.sock = None
|
||||
self.addr = socket.getaddrinfo(server, port)[0][-1]
|
||||
self.server = server
|
||||
self.port = port
|
||||
self.ssl = ssl
|
||||
self.ssl_params = ssl_params
|
||||
self.pid = 0
|
||||
@ -61,24 +54,36 @@ class MQTTClient:
|
||||
|
||||
def connect(self, clean_session=True):
|
||||
self.sock = socket.socket()
|
||||
self.sock.connect(self.addr)
|
||||
addr = socket.getaddrinfo(self.server, self.port)[0][-1]
|
||||
self.sock.connect(addr)
|
||||
if self.ssl:
|
||||
import ussl
|
||||
self.sock = ussl.wrap_socket(self.sock, **self.ssl_params)
|
||||
msg = bytearray(b"\x10\0\0\x04MQTT\x04\x02\0\0")
|
||||
msg[1] = 10 + 2 + len(self.client_id)
|
||||
msg[9] = clean_session << 1
|
||||
premsg = bytearray(b"\x10\0\0\0\0\0")
|
||||
msg = bytearray(b"\x04MQTT\x04\x02\0\0")
|
||||
|
||||
sz = 10 + 2 + len(self.client_id)
|
||||
msg[6] = clean_session << 1
|
||||
if self.user is not None:
|
||||
msg[1] += 2 + len(self.user) + 2 + len(self.pswd)
|
||||
msg[9] |= 0xC0
|
||||
sz += 2 + len(self.user) + 2 + len(self.pswd)
|
||||
msg[6] |= 0xC0
|
||||
if self.keepalive:
|
||||
assert self.keepalive < 65536
|
||||
msg[10] |= self.keepalive >> 8
|
||||
msg[11] |= self.keepalive & 0x00FF
|
||||
msg[7] |= self.keepalive >> 8
|
||||
msg[8] |= self.keepalive & 0x00FF
|
||||
if self.lw_topic:
|
||||
msg[1] += 2 + len(self.lw_topic) + 2 + len(self.lw_msg)
|
||||
msg[9] |= 0x4 | (self.lw_qos & 0x1) << 3 | (self.lw_qos & 0x2) << 3
|
||||
msg[9] |= self.lw_retain << 5
|
||||
sz += 2 + len(self.lw_topic) + 2 + len(self.lw_msg)
|
||||
msg[6] |= 0x4 | (self.lw_qos & 0x1) << 3 | (self.lw_qos & 0x2) << 3
|
||||
msg[6] |= self.lw_retain << 5
|
||||
|
||||
i = 1
|
||||
while sz > 0x7f:
|
||||
premsg[i] = (sz & 0x7f) | 0x80
|
||||
sz >>= 7
|
||||
i += 1
|
||||
premsg[i] = sz
|
||||
|
||||
self.sock.write(premsg, i + 2)
|
||||
self.sock.write(msg)
|
||||
#print(hex(len(msg)), hexlify(msg, ":"))
|
||||
self._send_str(self.client_id)
|
Reference in New Issue
Block a user