Skip to main content
Skip table of contents

MQTT connection info

Connect through the local network

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.

Connection info

The connection info can be found in the Pozyx web application under Settings > Connectivity > Local API.

Receiving data from a single tag

Connecting to the tags topic will give the information of all tags in one continuous stream of data. It is also possible to receive only the MQTT packets related to a tag with a certain ID. In order to do so append the topic with ‘/tagID' and fill in the correct ID of the tag. So the complete topic may look like this “tags/1000001“.

Examples

Python

The following Python program subscribes to the local MQTT stream using the paho-mqtt MQTT library:

PY
import paho.mqtt.client as mqtt
import ssl

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

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

# 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)

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

The following Java program subscribes to the local MQTT stream using the Paho MQTT library:

pom.xml

XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>io.pozyx</groupId>
  <artifactId>mqtt-cloud</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
        <groupId>org.eclipse.paho</groupId>
        <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
        <version>1.2.2</version>
    </dependency>
</dependencies>
</project>

main.java

JAVA
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class main {

	public static void main(String[] args) {
		
        String broker       = "tcp://192.168.82.54:1883"; // Replace IP with the uplink IP address of your gateway
        String topic        = "tags";
        String clientId		= "java1";
        
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            MqttClient mqttClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
            mqttConnectOptions.setCleanSession(true);

            System.out.println("Connecting to broker: "+broker);
            mqttClient.connect(mqttConnectOptions);
            System.out.println("Connected");
            
            mqttClient.setCallback(new MqttCallback() {
				public void connectionLost(Throwable cause) {
					System.out.println("Connection lost");
					System.out.println(cause);
				}
				public void messageArrived(String topic, MqttMessage message) throws Exception {
					System.out.println("Positioning update: " + message.toString());
				}
				public void deliveryComplete(IMqttDeliveryToken token) {
					System.out.println("Delivery complete");
				}
            });
            
            System.out.println("Subscribing to topic: "+topic);
            mqttClient.subscribe(topic);
            System.out.println("Subscribed to topic: "+topic);
        } catch(MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
        }
	}

}

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.

Please note that using the cloud MQTT the maximum update rate is 20 Hz.
More information about this can be found below under MQTT data rate.

Connection info

The cloud MQTT uses a WebSocket connection for which the connection information can be found in the Pozyx web application under Settings > Connectivity > Cloud API. The topic and username can be copied directly from this page. The password corresponds to an API key that has to be generated. Enter a name and press “Generate key“ in order to create a key. This key is the password that has to be used.

Receiving data from a single tag

Connecting to the topic you can find in the web application, will give the information of all tags in one continuous stream of data. It is also possible to receive only the MQTT packets related to a tag with a certain ID. In order to do so, enter the normal topic and append it with '/tagID' and fill in the correct ID of the tag.

Examples

Python

The following Python program subscribes to the cloud MQTT stream using the paho-mqtt MQTT library:

PY
import paho.mqtt.client as mqtt
import ssl

tenant_id = ""
api_key = ""

host = "mqtt.cloud.pozyxlabs.com"
port = 443
topic = f"{tenant_id}/tags"
username = tenant_id
password = api_key

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

# 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)

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

The following Java program subscribes to the cloud MQTT stream using the Paho MQTT library:

pom.xml

XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>io.pozyx</groupId>
  <artifactId>mqtt-cloud</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
        <groupId>org.eclipse.paho</groupId>
        <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
        <version>1.2.2</version>
    </dependency>
</dependencies>
</project>

main.java

JAVA
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class main {

	public static void main(String[] args) {
		
        String broker       = "wss://mqtt.cloud.pozyxlabs.com:443";
        String topic        = ""; // Generate on https://app.pozyx.io/settings/connectivity/cloud
        String apiKey		= ""; // Generate on https://app.pozyx.io/settings/connectivity/cloud
        String clientId		= "java1";
        
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            MqttClient mqttClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
            mqttConnectOptions.setCleanSession(true);
            mqttConnectOptions.setUserName(topic);
            mqttConnectOptions.setPassword(apiKey.toCharArray());

            System.out.println("Connecting to broker: "+broker);
            mqttClient.connect(mqttConnectOptions);
            System.out.println("Connected");
            
            mqttClient.setCallback(new MqttCallback() {
				public void connectionLost(Throwable cause) {
					System.out.println("Connection lost");
					System.out.println(cause);
				}
				public void messageArrived(String topic, MqttMessage message) throws Exception {
					System.out.println("Positioning update: " + message.toString());
				}
				public void deliveryComplete(IMqttDeliveryToken token) {
					System.out.println("Delivery complete");
				}
            });
            
            System.out.println("Subscribing to topic: "+topic);
            mqttClient.subscribe(topic);
            System.out.println("Subscribed to topic: "+topic);
        } catch(MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
        }
	}

}

MQTT data rate

The data rate of the cloud MQTT stream can be defined in the settings: Settings > Connectivity > Push to cloud, with a maximum of 20 Hz. The position updates from multiple tags are then batched in MQTT packets according to this frequency. Only the last position update of each tag is kept in memory for batching, so each MQTT packet will have maximum 1 position update per tag ID. This means that as long as an individual tag does not update at a faster rate then the MQTT data rate you should not lose any data, but if your tag is updating at a faster rate than the data rate of the MQTT stream then you will only receive the latest position for a tag:

ℹ️ Example without data loss

In this example we have 40 tags operating at 1 Hz and the MQTT data rate set to 5 Hz. The MQTT data rate is set higher than the update rate of each individual tag, so there won't be any data loss. Every 200ms an MQTT packet will be sent over the MQTT stream which will contain on average 8 tag updates each. On average because this depends on when each tag blinks: If each tag would blink at the same time then it could be that 1 MQTT packet contains the updates for all 40 tags and the other 4 packets within that second do not contain any information. But this is of course highly improbable. The update rate of the tags also have a variation mechanism built in, so they don't blink at exact 1 Hz. Which means that even if this event would occur, it would not continuously occur.

⚠️ Example with data loss

In this example we have 2 tags operating at 20 Hz and the MQTT data rate set to 5 Hz. The MQTT data rate is set lower than the update rate of each individual tag, so there will be data loss. Every 200ms an MQTT packet will be sent over the MQTT stream which will contain 2 tag updates each. Each MQTT packet will only contain the last position update for each tag, and not all the position updates since the last MQTT packet was sent out.

MQTT example

The below MQTT excerpt was taken from a cloud MQTT stream with:

  • The data rate set to 2 Hz

  • Tag #222 running at 2 Hz

  • Tag #7051 running at 10 Hz

MQTT packet 1
JSON
[
  {
    "version": "2.0",
    "tagId": "222",
    "timestamp": 1616000449.155912,
    "data": {
      "tagData": {
        "blinkIndex": 26401976
      },
      "metrics": {
        "latency": 42,
        "rates": {
          "success": 0,
          "update": 2.06,
          "packetLoss": 0
        }
      }
    }
  },
  {
    "version": "2.0",
    "tagId": "7051",
    "timestamp": 1616000449.5236182,
    "data": {
      "tagData": {
        "blinkIndex": 390568
      },
      "metrics": {
        "latency": 19,
        "rates": {
          "success": 9.69,
          "update": 9.66,
          "packetLoss": 0
        }
      }
    }
  }
]
MQTT packet 2
JSON
[
  {
    "version": "2.0",
    "tagId": "222",
    "timestamp": 1616000450.1082623,
    "data": {
      "tagData": {
        "blinkIndex": 26401978
      },
      "metrics": {
        "latency": 39,
        "rates": {
          "success": 0,
          "update": 2.07,
          "packetLoss": 0
        }
      }
    }
  },
  {
    "version": "2.0",
    "tagId": "7051",
    "timestamp": 1616000450.040604,
    "data": {
      "tagData": {
        "blinkIndex": 390572
      },
      "metrics": {
        "latency": 26,
        "rates": {
          "success": 8.81,
          "update": 8.79,
          "packetLoss": 0
        }
      }
    }
  }
]
MQTT packet 3
JSON
[
  {
    "version": "2.0",
    "tagId": "222",
    "timestamp": 1616000450.584033,
    "data": {
      "tagData": {
        "blinkIndex": 26401979
      },
      "metrics": {
        "latency": 29,
        "rates": {
          "success": 0,
          "update": 2.07,
          "packetLoss": 0
        }
      }
    }
  },
  {
    "version": "2.0",
    "tagId": "7051",
    "timestamp": 1616000450.612532,
    "data": {
      "tagData": {
        "blinkIndex": 390578
      },
      "metrics": {
        "latency": 25,
        "rates": {
          "success": 9.51,
          "update": 9.48,
          "packetLoss": 0
        }
      }
    }
  }
]
MQTT packet 4
JSON
[
  {
    "version": "2.0",
    "tagId": "222",
    "timestamp": 1616000451.0252686,
    "data": {
      "tagData": {
        "blinkIndex": 26401980
      },
      "metrics": {
        "latency": 25,
        "rates": {
          "success": 0,
          "update": 2.09,
          "packetLoss": 0
        }
      }
    }
  },
  {
    "version": "2.0",
    "tagId": "7051",
    "timestamp": 1616000451.1700604,
    "data": {
      "tagData": {
        "blinkIndex": 390584
      },
      "metrics": {
        "latency": 20,
        "rates": {
          "success": 10.12,
          "update": 10.09,
          "packetLoss": 0
        }
      }
    }
  }
]

We can see that:

  • Every MQTT packet contains a tag position update for each tag because there is no tag operating at a lower update rate than the MQTT data rate (if we waited long enough it could be that tag #222 is missing in some MQTT packets because of slight variations in the actual rates).

  • For tag #222 almost all blinks are sent out over the MQTT stream because the update rates of the tag is very similar to the data rate of the MQTT stream (they're set to the same value but there's always a slight variation in the actual rates).

  • The blinkIndex of tag #7051 is 390568 in the first MQTT packet and 390584 in the fourth MQTT packet. It went up by 16, which is in line with an expected blink increase of ± 5 per MQTT packet as the update rate of the tag (10 Hz) is 5 times the data rate of the MQTT stream (2 Hz).

Using your own MQTT broker with AWS Greengrass

It's also possible to configure the Gateway to use your own MQTT broker, but only AWS Greengrass is supported at the moment.

If you want to use your own MQTT broker, feel free to reach out to us via http://support.pozyx.io to configure this for you.

We'll need the following information / files:

  • The MQTT endpoint

  • The CA file

  • The CERT file

  • The KEY file

  • The Client ID

  • The MQTT topic

When you use your own MQTT broker the positioning data will also be batched in MQTT packets, and these MQTT packets will go out at a data rate of 10 Hz (or in batches of 50 position updates, whichever comes first). These parameters can be configured by us, the settings defined on the Push to cloud page do not apply when you're using your own MQTT broker. In contrast to when you use our cloud MQTT broker there's never data loss when you use your own broker: A single MQTT packet can contain multiple position updates from a single tag.


JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.