kopia lustrzana https://github.com/micropython/micropython-lib
umqtt.simple: Add standalone sub example.
rodzic
df604048fd
commit
f1a65bc68b
|
@ -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()
|
Ładowanie…
Reference in New Issue