Introduction

In this tutorial you will learn how to include custom data in the tag transmission payload. This will essentially transform the Enterprise system in a wireless sensor network. The data transmitted will be available in the local MQTT stream on the gateway, with the position included. This will allow you to capture location-based events or location-based sensor data for your application.

The custom payload will only be available locally, not over the cloud


The developer tag can be used both for the Creator and the Enterprise series. For the Enterprise series, the tag must be configured to be in the Enterprise TDOA Aloha mode. Using the device configurator, it is already possible to change the behavior of the tag such as positioning update rate, or by selecting between some standard payloads. The standard payloads capture sensor data from the developer tag and include it in the transmission packet. Sensors to choose from are the on-board accelerometer, gyroscope, magnetometer or pressure sensor.

We will see now how to customize the payload even further.

Python library setup

First we’re going to need the Pozyx python library to communicate with the developer-tag over USB. You can download it through git. When cloning the repository, make sure you checkout the develop branch.

Make sure you have the prerequisites as stated in the readme.rst

  • python installed

  • pyserial package installed

  • ST’s virtual COM driver installed

Custom payload

Here we will create a script that connects to the developer tag. It will enable the ALOHA-mode and write the custom data that needs to be transmitted as payload. In the script below, the custom payload is an incrementing integer which is transmitted every 0.5 seconds. To encapsulate other types of data, please check the Python function pack.

from pypozyx import *
from pypozyx.definitions.registers import *
from time import *
import struct

p = PozyxSerial(get_first_pozyx_serial_port())
p.startAloha()
index = 0
while True:
    transmit_data = struct.pack("I", index)
    index += 1
    p.writeTXBufferData(transmit_data)
    p.sendAloha(POZYX_SEND_CUSTOM_ALOHA_IMMEDIATE)
    sleep(0.5)
PY

Other custom payload options

The struct.pack(fmt, v1, v2, …) function packs the variables v1, v2, … according to the given format. Changing the format allows you to cover a wide range of payload options. Some useful formats are summarized below:

Format

Python type

?

bool

I

integer

f

float

s

string

Grabbing the data through MQTT

Finally, we can capture the transmitted data by connecting to the MQTT stream. For the example above, the output looks as follows. The custom payload can be found under data > tagData > customSensors. There you can find the custompayload and the value we added with the script. Remember that the data is represented as bytes in BigEndian. Use the unpack function if you want the data back in it’s original form.

[
{
	"version":"2.0",
	"tagId":"30280",
	"timestamp":1655829019.3627698,
	"data":{
		"metrics":{
			"latency":19,
			"rates":{
				"success":5.28,
				"update":5.28,
				"packetLoss":0.37
			}
		},
		"tagData":{
			"blinkIndex":388994,
			"customSensors":[
				{
					"name":"customPayload",
					"value":"51010000"
				}
			]
		},
		"anchorData":[
			{
				"tagId":"30280",
				"anchorId":"6634",
				"rss":-80.77
			},
			{
				"tagId":"30280",
				"anchorId":"18193",
				"rss":-79.27
			},
			{
				"tagId":"30280",
				"anchorId":"32497",
				"rss":-80.85
			},
			{
				"tagId":"30280",
				"anchorId":"39229",
				"rss":-80.32
			},
			{
				"tagId":"30280",
				"anchorId":"60704",
				"rss":-80.38
			},
			{
				"tagId":"30280",
				"anchorId":"64403",
				"rss":-80.74
			}
		],
		"coordinates":{
			"x":1373,
			"y":185,
			"z":1000
		},
		"position_score":94.8087073265113,
		"zones":[
			{
				"name":"zone 2",
				"id":"62669d6092484ce7951d1b19"
			}
		],
		"type":1
	},
	"success":true
}
]
JSON