We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent df60404 commit f1a65bcCopy full SHA for f1a65bc
umqtt.simple/example_sub.py
@@ -0,0 +1,30 @@
1
+import time
2
+from umqtt.simple import MQTTClient
3
+
4
+# Publish test messages e.g. with:
5
+# mosquitto_pub -t foo_topic -m hello
6
7
+# Received messages from subscriptions will be delivered to this callback
8
+def sub_cb(topic, msg):
9
+ print((topic, msg))
10
11
+def main(server="localhost"):
12
+ c = MQTTClient("umqtt_client", server)
13
+ c.set_callback(sub_cb)
14
+ c.connect()
15
+ c.subscribe(b"foo_topic")
16
+ while True:
17
+ if True:
18
+ # Blocking wait for message
19
+ c.wait_msg()
20
+ else:
21
+ # Non-blocking wait for message
22
+ c.check_msg()
23
+ # Then need to sleep to avoid 100% CPU usage (in a real
24
+ # app other useful actions would be performed instead)
25
+ time.sleep(1)
26
27
+ c.disconnect()
28
29
+if __name__ == "__main__":
30
+ main()
0 commit comments