# How to Publish and Decode Multiple Hex Payloads via MQTT on Qubitro

This guide shows how to:

* ✅ Send multiple hex payloads using **MQTTX**
* ✅ Use a custom **Qubitro** [**Decoder Function** ](https://app.gitbook.com/s/fQfo4zX1x0hfOimS9E2u/platform/functions/decoder-function#hex-payload-decoder)to decode GPS and sensor data
* ✅ View structured decoded data in the Qubitro portal

***

### 🧩 Prerequisites

* A [Qubitro account](https://portal.qubitro.com/)
* An existing **Device**
* [MQTTX Desktop App](https://mqttx.app/)
* Your device's **MQTT credentials**

***

### Step 1: Get Your Qubitro MQTT Credentials

1. Go to your device in the **Qubitro Portal**
2. Click the **MQTT Credentials** tab
3. Copy:

   * **Device ID** (used as `Username` and topic)
   * **Device Token** (used as `Password`)
   * **Host**: `broker.qubitro.com`
   * **Port**: `8883` (SSL) or `1883` (non-SSL)
   * **Topic**: `Device ID`

   <figure><img src="https://2085333718-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FllOFTfRA5L4YRnakI84m%2Fuploads%2Fs5GE9E8y9yYw4tH7smOt%2FCleanShot%202025-03-25%20at%2019.54.15%402x.png?alt=media&#x26;token=d9e61585-e5d6-4a34-9c47-bc9504665156" alt=""><figcaption></figcaption></figure>

***

### Step 2: Configure MQTTX (Secure Connection)

Open MQTTX and configure your connection as shown below:

* **Name**: `broker.qubitro.com`
* **Host**: `mqtts://broker.qubitro.com`
* **Port**: `8883`
* **Client ID**: (same as Device ID)
* **Username**: Device ID
* **Password**: Device Token
* **SSL/TLS**: ✅ Enabled
* **SSL Secure**: ✅ Enabled
* **Certificate**: `CA signed server certificate`

{% hint style="success" %}
Click **Connect** to establish a secure MQTT connection.
{% endhint %}

<figure><img src="https://2085333718-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FllOFTfRA5L4YRnakI84m%2Fuploads%2FfYZIXUbmLECtdK0HcPYu%2FCleanShot%202025-03-25%20at%2019.56.37%402x.png?alt=media&#x26;token=45611ac1-a626-42b9-af47-45b863f36c91" alt=""><figcaption></figcaption></figure>

***

### Step 3: Add Decoder Function in Qubitro

1\. Go to your device → Functions tab

2\. Click Add Function → Choose Decoder Function

3\. Paste the following JavaScript code:

```javascript
function decoder(input) {
  const longitude = ((input.bytes[0] << 24) | (input.bytes[1] << 16) | (input.bytes[2] << 8) | (input.bytes[6] & 0xC0)) /1000000;
  const latitude = ((input.bytes[3] << 24) | (input.bytes[4] << 16) | (input.bytes[5] << 8) | ((input.bytes[6] & 0x30) << 2)) /1000000;
  const hMSL32 = ((((input.bytes[6] & 0x0F) << 8 ) + input.bytes[7]) * 2) - 191;
  const hAccCoeff = (input.bytes[8] & 0xE0) >> 5;
  const vAccCoeff = (input.bytes[8] & 0x1C) >> 2;
  const heading = ((input.bytes[8] & 0x03) << 2) + ((input.bytes[9] & 0xC0) >> 6);
  const speed = (input.bytes[9] & 0x3F);
  const battery = ((input.bytes[10]*256) + input.bytes[11]);
  const year = (input.bytes[12] >> 2) + 2000;
  const month = ((input.bytes[12] & 0x03) << 2) + ((input.bytes[13] & 0xC0) >> 6);
  const day = (input.bytes[13] & 0x3E) >> 1;
  const hour = ((input.bytes[13] & 0x01) << 4) + ((input.bytes[14] & 0xF0) >> 4);
  const minute = ((input.bytes[14] & 0x0F) << 2) + ((input.bytes[15] & 0xC0) >> 6);
  const second = (input.bytes[15] & 0x3F);
  const timestampSeconds = Math.floor(new Date(year, month, day, hour, minute, second).getTime() / 1000);

  return {
    longitude,
    latitude,
    coordinates: [latitude, longitude],
    hMSL32,
    hAccCoeff,
    vAccCoeff,
    heading,
    speed,
    battery,
    time: timestampSeconds
  };
}
```

{% hint style="success" %}
You can also test a single hex payload on the Qubitro Portal to verify your decoder.
{% endhint %}

<figure><img src="https://2085333718-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FllOFTfRA5L4YRnakI84m%2Fuploads%2FFw2kppvhMOLkVGOEFFyO%2FCleanShot%202025-03-25%20at%2019.59.40%402x.png?alt=media&#x26;token=6ae0f9ae-92a7-4cd8-8009-6c58d9778490" alt=""><figcaption></figcaption></figure>

4. Save and activate the function.

### Step 4: Publish Multiple Hex Payloads

1. Go to the **Publish** tab in MQTTX.
2. In the **topic** field, paste your **Device ID**.
3. Set the payload type to **JSON**.
4. Use the following payload:

```json
{
  "payload_hex": "00C08802808560A184000FFB648EAD0F,00c088028085909784000ffb648ead46,00c089028085109284000ffe648ead89,00c088028085e09c84000ffe648eadcb"
}
```

5. Click the green send button to publish the message.

<figure><img src="https://2085333718-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FllOFTfRA5L4YRnakI84m%2Fuploads%2FF0IoI16TFisOdSNLMKzE%2FCleanShot%202025-03-25%20at%2019.58.26%402x.png?alt=media&#x26;token=e74efe74-2cd6-4fe0-ab25-d0d2f72d86b3" alt=""><figcaption></figcaption></figure>

### Step 5: View Decoded Data

Open the Storage tab in your Qubitro device and you’ll see structured decoded values like:

<figure><img src="https://2085333718-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FllOFTfRA5L4YRnakI84m%2Fuploads%2Fgqo4vlwf54MMnwnOisHb%2FCleanShot%202025-03-25%20at%2020.02.03%402x.png?alt=media&#x26;token=d6831e78-3847-4b2b-b630-741fcd0b39fa" alt=""><figcaption></figcaption></figure>

***

You’ve successfully:

• Connected MQTTX to Qubitro securely

• Published multiple hex payloads

• Used a custom decoder to parse sensor/GPS values

• Verified decoded output in Qubitro
