-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmqtt_wrapper.py
More file actions
47 lines (38 loc) · 1.3 KB
/
mqtt_wrapper.py
File metadata and controls
47 lines (38 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import time
from umqtt.simple import MQTTClient
# Publish test messages e.g. with:
# mosquitto_pub -t foo_topic -m hello
blocking_wait = False
mqtt_client = None
# Received messages from subscriptions will be delivered to this callback
def simple_sub_cb(topic, msg):
print((topic, msg))
def init(client_id, hostname='alpcer0.local', sub_topic='kybIntcpt', callback=simple_sub_cb):
global mqtt_client
mqtt_client = MQTTClient(client_id, hostname)
mqtt_client.set_callback(callback)
mqtt_client.connect()
mqtt_client.subscribe(sub_topic)
mqtt_client.ping()
mqtt_client.wait_msg()
def main(server="alpcer0.local"):
global blocking_wait
c = MQTTClient("umqtt_client", server)
c.set_callback(simple_sub_cb)
c.connect()
c.subscribe(b"foo_topic")
while True:
if blocking_wait:
# Blocking wait for message
c.wait_msg()
c.publish(b'bar_topic', b'received a message')
else:
c.publish(b'bar_topic', b'checking for messages')
# 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()