diff --git a/micropython/umqtt.simple/README.rst b/micropython/umqtt.simple/README.rst index d9d09b970..207b547be 100644 --- a/micropython/umqtt.simple/README.rst +++ b/micropython/umqtt.simple/README.rst @@ -53,6 +53,7 @@ follows MQTT control operations, and maps them to class methods: * ``ping()`` - Ping server (response is processed automatically by wait_msg()). * ``publish()`` - Publish a message. * ``subscribe()`` - Subscribe to a topic. +* ``unsubscribe()`` - Unsubscribe to a topic. * ``set_callback()`` - Set callback for received subscription messages. * ``set_last_will()`` - Set MQTT "last will" message. Should be called *before* connect(). diff --git a/micropython/umqtt.simple/manifest.py b/micropython/umqtt.simple/manifest.py index 709a27505..ce639bbad 100644 --- a/micropython/umqtt.simple/manifest.py +++ b/micropython/umqtt.simple/manifest.py @@ -1,4 +1,4 @@ -metadata(description="Lightweight MQTT client for MicroPython.", version="1.6.0") +metadata(description="Lightweight MQTT client for MicroPython.", version="1.7.0") # Originally written by Paul Sokolovsky. diff --git a/micropython/umqtt.simple/umqtt/simple.py b/micropython/umqtt.simple/umqtt/simple.py index d9cdffc47..5d52ee305 100644 --- a/micropython/umqtt.simple/umqtt/simple.py +++ b/micropython/umqtt.simple/umqtt/simple.py @@ -175,6 +175,19 @@ def subscribe(self, topic, qos=0): raise MQTTException(resp[3]) return + def unsubscribe(self, topic): + pkt = bytearray(b"\xa2\0\0\0") + self.pid += 1 + struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic), self.pid) + self.sock.write(pkt) + self._send_str(topic) + while 1: + op = self.wait_msg() + if op == 0xB0: + resp = self.sock.read(3) + assert resp[1] == pkt[2] and resp[2] == pkt[3] + return + # Wait for a single incoming MQTT message and process it. # Subscribed messages are delivered to a callback previously # set by .set_callback() method. Other (internal) MQTT