diff --git a/umqtt.simple/example_sub.py b/umqtt.simple/example_sub.py new file mode 100644 index 00000000..f0881848 --- /dev/null +++ b/umqtt.simple/example_sub.py @@ -0,0 +1,30 @@ +import time +from umqtt.simple import MQTTClient + +# Publish test messages e.g. with: +# mosquitto_pub -t foo_topic -m hello + +# Received messages from subscriptions will be delivered to this callback +def sub_cb(topic, msg): + print((topic, msg)) + +def main(server="localhost"): + c = MQTTClient("umqtt_client", server) + c.set_callback(sub_cb) + c.connect() + c.subscribe(b"foo_topic") + while True: + if True: + # Blocking wait for message + c.wait_msg() + else: + # Non-blocking wait for message + c.check_msg() + # Then need to sleep to avoid 100% CPU usage (in a real + # app other useful actions would be performed instead) + time.sleep(1) + + c.disconnect() + +if __name__ == "__main__": + main()