File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
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 ()
You can’t perform that action at this time.
0 commit comments