Introduction

Once the system is set up, you can start extracting positioning data. You can do this by using the popular MQTT protocolMQTT is a publish/subscribe protocol, so you get the newest positions as soon as they are available.

By default, the MQTT packet only includes positions. It's possible to add more sensor data from the tag by using the Pozyx Device Configurator.

Data structure

The full MQTT JSON output data structure can be found on the MQTT data structure page.

Connect with the local stream

On your Local Area Network (LAN), you can connect directly with the Pozyx gateway. We recommend this for real-time applications as it provides a low latency connection.

MQTT libraries exist for most programming languages. The following Python snippet subscribes to the local MQTT stream using paho-mqtt. Make sure to fill in the IP address of the Pozyx gateway below. The Gateway setup page describes how to discover your gateway's IP address.

import paho.mqtt.client as mqtt
import ssl

host = "" # fill in the IP of your gateway
port = 1883
topic = "tags" 

def on_connect(client, userdata, flags, rc):
    print(mqtt.connack_string(rc))

# callback triggered by a new Pozyx data packet
def on_message(client, userdata, msg):
    print("Positioning update:", msg.payload.decode())

def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed to topic!")

client = mqtt.Client()

# set callbacks
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
client.connect(host, port=port)
client.subscribe(topic)

# works blocking, other, non-blocking, clients are available too.
client.loop_forever()
PY

Connect through the cloud

By connecting through the cloud, you can retrieve positioning data from anywhere. The Pozyx cloud offers the MQTT data in a secure way. You can find the connection credentials on the Pozyx web application in Settings > Connectivity > Cloud API. On this page, you can generate secure API keys.

The MQTT service, provided as MQTT over WebSockets, is available at:

host: mqtt.cloud.pozyxlabs.com
port: 443

The remaining connection settings (topic, username and password) are available on the Pozyx web application in Settings > Connectivity > Cloud API.

The following Python program subscribes to the cloud MQTT stream, using the credentials obtained as described above:

import paho.mqtt.client as mqtt
import ssl

host = "mqtt.cloud.pozyxlabs.com"
port = 443
topic = ""  # your mqtt topic
username = ""  # your mqtt username
password = ""  # your generated api key

def on_connect(client, userdata, flags, rc):
    print(mqtt.connack_string(rc))

# Callback triggered by a new Pozyx data packet
def on_message(client, userdata, msg):
    print("Positioning update:", msg.payload.decode())

def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed to topic!")

client = mqtt.Client(transport="websockets")
client.username_pw_set(username, password=password)

# sets the secure context, enabling the WSS protocol
client.tls_set_context(context=ssl.create_default_context())

# set callbacks
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
client.connect(host, port=port)
client.subscribe(topic)

# works blocking, other, non-blocking, clients are available too.
client.loop_forever()
PY

Please note that the cloud MQTT service caps the maximum update rate. This value can be set under settings. By default the update rate is capped to 10 Hz.