Merge pull request #2073 from openmv/update_scripts

scripts: Update examples and libraries.
This commit is contained in:
Ibrahim Abdelkader 2024-01-03 12:12:17 +02:00 committed by GitHub
commit 8273574975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 27 deletions

View File

@ -10,7 +10,7 @@
# 3) Install the mosquitto client on PC and run the following command: # 3) Install the mosquitto client on PC and run the following command:
# mosquitto_sub -h test.mosquitto.org -t "openmv/test" -v # 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 time
import network import network

View File

@ -10,7 +10,7 @@
# 3) Install the mosquitto client on PC and run the following command: # 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 # 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 time
import network import network

View File

@ -37,4 +37,6 @@ class LED:
self.pin(not self.pin()) self.pin(not self.pin())
def value(self, v=None): 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)

View File

@ -20,10 +20,12 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE. # 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 socket
import ustruct as struct import struct
import select
import ssl
class MQTTException(Exception): class MQTTException(Exception):
@ -35,26 +37,23 @@ class MQTTClient:
self, self,
client_id, client_id,
server, server,
port=0, port,
ssl_params=None,
user=None, user=None,
password=None, password=None,
keepalive=0, keepalive=0,
ssl=False, callback=None,
ssl_params={},
): ):
if port == 0:
port = 8883 if ssl else 1883
self.client_id = client_id self.client_id = client_id
self.sock = None
self.server = server self.server = server
self.port = port self.port = port
self.ssl = ssl
self.ssl_params = ssl_params self.ssl_params = ssl_params
self.pid = 0
self.cb = None
self.user = user self.user = user
self.pswd = password self.pswd = password
self.keepalive = keepalive self.keepalive = keepalive
self.cb = callback
self.sock = None
self.pid = 0
self.lw_topic = None self.lw_topic = None
self.lw_msg = None self.lw_msg = None
self.lw_qos = 0 self.lw_qos = 0
@ -85,15 +84,27 @@ class MQTTClient:
self.lw_qos = qos self.lw_qos = qos
self.lw_retain = retain self.lw_retain = retain
def connect(self, clean_session=True): def connect(self, clean_session=True, timeout=5.0):
self.sock = socket.socket()
addr = None
addr = socket.getaddrinfo(self.server, self.port)[0][-1] 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") premsg = bytearray(b"\x10\0\0\0\0\0")
msg = bytearray(b"\x04MQTT\x04\x02\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(premsg[0 : i + 2])
self.sock.write(msg) self.sock.write(msg)
# print(hex(len(msg)), hexlify(msg, ":"))
self._send_str(self.client_id) self._send_str(self.client_id)
if self.lw_topic: if self.lw_topic:
self._send_str(self.lw_topic) self._send_str(self.lw_topic)
@ -201,9 +211,8 @@ class MQTTClient:
# messages processed internally. # messages processed internally.
def wait_msg(self): def wait_msg(self):
res = self.sock.read(1) res = self.sock.read(1)
if res == b"" or res is None: if res is None or res == b"":
return None return None
self.sock.setblocking(True)
if res == b"\xd0": # PINGRESP if res == b"\xd0": # PINGRESP
sz = self.sock.read(1)[0] sz = self.sock.read(1)[0]
assert sz == 0 assert sz == 0
@ -233,5 +242,6 @@ class MQTTClient:
# If not, returns immediately with None. Otherwise, does # If not, returns immediately with None. Otherwise, does
# the same processing as wait_msg. # the same processing as wait_msg.
def check_msg(self): def check_msg(self):
self.sock.setblocking(False) r, w, e = select.select([self.sock], [], [], 0.05)
if len(r):
return self.wait_msg() return self.wait_msg()