Skip to content

Commit 6f1b5ad

Browse files
committed
umqtt.simple: Add unsubscribe method.
Signed-off-by: Pedro Araoz <paraoz22@gmail.com>
1 parent bdc4706 commit 6f1b5ad

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

micropython/umqtt.simple/README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ follows MQTT control operations, and maps them to class methods:
5353
* ``ping()`` - Ping server (response is processed automatically by wait_msg()).
5454
* ``publish()`` - Publish a message.
5555
* ``subscribe()`` - Subscribe to a topic.
56+
* ``unsubscribe()`` - Unsubscribe to a topic.
5657
* ``set_callback()`` - Set callback for received subscription messages.
5758
* ``set_last_will()`` - Set MQTT "last will" message. Should be called
5859
*before* connect().

micropython/umqtt.simple/manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
metadata(description="Lightweight MQTT client for MicroPython.", version="1.6.0")
1+
metadata(description="Lightweight MQTT client for MicroPython.", version="1.7.0")
22

33
# Originally written by Paul Sokolovsky.
44

micropython/umqtt.simple/umqtt/simple.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ def subscribe(self, topic, qos=0):
175175
raise MQTTException(resp[3])
176176
return
177177

178+
def unsubscribe(self, topic):
179+
pkt = bytearray(b"\xa2\0\0\0")
180+
self.pid += 1
181+
struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic), self.pid)
182+
self.sock.write(pkt)
183+
self._send_str(topic)
184+
while 1:
185+
op = self.wait_msg()
186+
if op == 0xB0:
187+
resp = self.sock.read(3)
188+
assert resp[1] == pkt[2] and resp[2] == pkt[3]
189+
return
190+
178191
# Wait for a single incoming MQTT message and process it.
179192
# Subscribed messages are delivered to a callback previously
180193
# set by .set_callback() method. Other (internal) MQTT

0 commit comments

Comments
 (0)