-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt-general.py
57 lines (38 loc) · 1.3 KB
/
mqtt-general.py
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
48
49
50
51
52
53
54
55
56
57
API_KEY = "3495b03e-6d9d-441d-8f42-00c47bd81033"
import paho.mqtt.client as mqtt
import ssl
import json
host = "mqtt.cloud.pozyxlabs.com"
port = 443
topic = "5aa3ab594e136e0005b1cdd6"
username = "5aa3ab594e136e0005b1cdd6"
password = "3495b03e-6d9d-441d-8f42-00c47bd81033"
ip = "127.0.0.1" # IP for the OSC UDP
network_port = 8888 # network port for the OSC UDP
def on_connect(client, userdata, flags, rc):
print(mqtt.connack_string(rc))
def on_message(client, userdata, msg):
print("Received message")
tag_data = json.loads(msg.payload.decode())
print(tag_data)
for tag in tag_data:
try:
network_id = tag["tagId"]
position = tag["data"]["coordinates"]
# YOUR CODE GOES HERE
except:
pass
# print("Positioning update:", msg.payload.decode())
# print(coordinates)
def on_subscribe(client, userdata, mid, granted_qos):
print("Subscribed to topic!")
client = mqtt.Client(transport="websockets")
client.username_pw_set(username, password=password)
client.tls_set_context(context=ssl.create_default_context())
client
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
client.connect(host, port=port)
client.subscribe(topic)
client.loop_forever()