From 0f6684a41cd039e11c05b2152e1fdcb59d917594 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 3 Jan 2024 10:57:09 +0100 Subject: [PATCH 1/3] scripts/examples: Update mqtt examples. --- scripts/examples/09-WiFi/mqtt_pub.py | 2 +- scripts/examples/09-WiFi/mqtt_sub.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/examples/09-WiFi/mqtt_pub.py b/scripts/examples/09-WiFi/mqtt_pub.py index e400e4294..677badb37 100644 --- a/scripts/examples/09-WiFi/mqtt_pub.py +++ b/scripts/examples/09-WiFi/mqtt_pub.py @@ -10,7 +10,7 @@ # 3) Install the mosquitto client on PC and run the following command: # mosquitto_sub -h test.mosquitto.org -t "openmv/test" -v # -# NOTE: If the mosquitto broker is unreachable, try another broker (For example: broker.hivemq.com) +# NOTE: If the mosquitto broker is unreachable, try another broker (For example: broker.hivemq.com or broker.emqx.io) import time import network diff --git a/scripts/examples/09-WiFi/mqtt_sub.py b/scripts/examples/09-WiFi/mqtt_sub.py index 861666634..552bde2e5 100644 --- a/scripts/examples/09-WiFi/mqtt_sub.py +++ b/scripts/examples/09-WiFi/mqtt_sub.py @@ -10,7 +10,7 @@ # 3) Install the mosquitto client on PC and run the following command: # mosquitto_pub -t "openmv/test" -m "Hello World!" -h test.mosquitto.org -p 1883 # -# NOTE: If the mosquitto broker is unreachable, try another broker (For example: broker.hivemq.com) +# NOTE: If the mosquitto broker is unreachable, try another broker (For example: broker.hivemq.com or broker.emqx.io) import time import network From d12874c63762fd3e4b522c6cbe9df19aca394d78 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 3 Jan 2024 10:57:48 +0100 Subject: [PATCH 2/3] scripts/libraries: Improve MQTT library. --- scripts/libraries/mqtt.py | 58 +++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/scripts/libraries/mqtt.py b/scripts/libraries/mqtt.py index bc6252880..de20d6d8a 100644 --- a/scripts/libraries/mqtt.py +++ b/scripts/libraries/mqtt.py @@ -20,10 +20,12 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # -# Source: https://github.com/micropython/micropython-lib/tree/master/micropython/umqtt.simple +# Based on: https://github.com/micropython/micropython-lib/tree/master/micropython/umqtt.simple -import usocket as socket -import ustruct as struct +import socket +import struct +import select +import ssl class MQTTException(Exception): @@ -35,26 +37,23 @@ class MQTTClient: self, client_id, server, - port=0, + port, + ssl_params=None, user=None, password=None, keepalive=0, - ssl=False, - ssl_params={}, + callback=None, ): - if port == 0: - port = 8883 if ssl else 1883 self.client_id = client_id - self.sock = None self.server = server self.port = port - self.ssl = ssl self.ssl_params = ssl_params - self.pid = 0 - self.cb = None self.user = user self.pswd = password self.keepalive = keepalive + self.cb = callback + self.sock = None + self.pid = 0 self.lw_topic = None self.lw_msg = None self.lw_qos = 0 @@ -85,15 +84,27 @@ class MQTTClient: self.lw_qos = qos self.lw_retain = retain - def connect(self, clean_session=True): - self.sock = socket.socket() - addr = None + def connect(self, clean_session=True, timeout=5.0): 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) + if self.sock is not None: + self.sock.close() + self.sock = None + + try: + self.sock = socket.socket() + self.sock.settimeout(timeout) + if self.ssl_params is not None: + self.sock = ssl.wrap_socket(self.sock, self.ssl_params) + self.sock.connect(addr) + except Exception: + self.sock.close() + self.sock = socket.socket() + self.sock.settimeout(timeout) + self.sock.connect(addr) + if self.ssl_params is not None: + self.sock = ssl.wrap_socket(self.sock, self.ssl_params) + premsg = bytearray(b"\x10\0\0\0\0\0") msg = bytearray(b"\x04MQTT\x04\x02\0\0") @@ -120,7 +131,6 @@ class MQTTClient: self.sock.write(premsg[0 : i + 2]) self.sock.write(msg) - # print(hex(len(msg)), hexlify(msg, ":")) self._send_str(self.client_id) if self.lw_topic: self._send_str(self.lw_topic) @@ -201,9 +211,8 @@ class MQTTClient: # messages processed internally. def wait_msg(self): res = self.sock.read(1) - if res == b"" or res is None: + if res is None or res == b"": return None - self.sock.setblocking(True) if res == b"\xd0": # PINGRESP sz = self.sock.read(1)[0] assert sz == 0 @@ -233,5 +242,6 @@ class MQTTClient: # If not, returns immediately with None. Otherwise, does # the same processing as wait_msg. def check_msg(self): - self.sock.setblocking(False) - return self.wait_msg() + r, w, e = select.select([self.sock], [], [], 0.05) + if len(r): + return self.wait_msg() From ded100e6c6d14b58a473c8f21e40e5a670c703ce Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 3 Jan 2024 10:58:49 +0100 Subject: [PATCH 3/3] scripts/libraries: Fix machine.LED.value(). --- scripts/libraries/machine.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/libraries/machine.py b/scripts/libraries/machine.py index 600b302d8..b830bd1ff 100644 --- a/scripts/libraries/machine.py +++ b/scripts/libraries/machine.py @@ -37,4 +37,6 @@ class LED: self.pin(not self.pin()) def value(self, v=None): - self.pin(v ^ self.inverted if v is not None else None) + if v is None: + return self.pin() + self.pin(v ^ self.inverted)