JavaScript Function payload

Hi Guys, I am new here. I have a project for livestock tracking system. I have installed Chirpstack and a gateway. My challenge is JavaScript Function payload for mokosmart LW001-BG Pro device location (Correct). anyone who can assist?

Sure do you have any protocol ? give some example data.

1.0.3 on EU868 and below is the script

function decodeUplink(input) {
let bytes = Buffer.from(input.bytes, ‘base64’);

if (bytes.length < 11) {
    return {
        errors: ["Invalid payload length"]
    };
}

let decoded = {};

// Battery Voltage (0.1V steps)
decoded.battery = bytes[0] * 0.1;

// Temperature (Signed Integer)
decoded.temperature = (bytes[1] & 0x7F) - ((bytes[1] & 0x80) ? 128 : 0);

// GPS Fix Status
decoded.gpsFix = bytes[2] === 1 ? "Valid" : "Invalid";

// GPS Coordinates
decoded.latitude = ((bytes[3] << 16) | (bytes[4] << 8) | bytes[5]) / 1000000;
decoded.longitude = ((bytes[6] << 16) | (bytes[7] << 8) | bytes[8]) / 1000000;

// Motion Status (0 = Static, 1 = Moving)
decoded.motionStatus = bytes[9] === 1 ? "Moving" : "Static";

// Step Count
decoded.steps = (bytes[10] << 8) | bytes[11];

// Alarm Flags (Bitwise)
decoded.alarm = {
    lowBattery: (bytes[12] & 0x01) ? true : false,
    motionDetected: (bytes[12] & 0x02) ? true : false,
    geoFenceAlert: (bytes[12] & 0x04) ? true : false
};

return {
    data: decoded
};

}

// ChirpStack v4 Integration
function Decode(fPort, bytes, variables) {
return decodeUplink({ bytes: Buffer.from(bytes).toString(‘base64’) });
}

what do you see in the uplinks in chirpstack? do you see any error?

The GPS is pointing to the incorrect location. I am based in Joburg South Africa and also my Gateway. For some reason the Latitude and Longitude are somewhere in the Sea Equatorial Guinea

send raw data and also the protocol!

  • deduplicationId:“d9fb499b-305a-4553-af2a-2ce468d6ffe3”
  • time:“2025-06-25T14:22:11.778837+00:00”
  • :arrow_forward:

deviceInfo:{} 10 keys

  • tenantId:“300e5b00-a6e8-42c6-ac07-bd0fe4a11527”
  • tenantName:“Charis Place”
  • applicationId:“f98a62ee-0c11-473b-b00c-036c6b3e6040”
  • applicationName:“lw001-bg-pro1”
  • deviceProfileId:“5b34e667-d3c2-4f41-bf36-641a19004ec3”
  • deviceProfileName:"Mokosmart "
  • deviceName:“LW01”
  • devEui:“dd0ab2ffff591265”
  • deviceClassEnabled:“CLASS_A”
  • tags:{} 0 keys
  • devAddr:“01134f3f”
  • adr:true
  • dr:5
  • fCnt:6
  • fPort:2
  • confirmed:false
  • data:“AxDQAgfpBhkQFg8CCfBrDswQmETFHw==”
  • :arrow_forward:

object:{} 8 keys

  • temperature:16
  • latitude:0.133097
  • :arrow_forward:

alarm:{} 3 keys
* lowBattery:true
* geoFenceAlert:false
* motionDetected:false

  • motionStatus:“Static”
  • steps:3842
  • gpsFix:“Invalid”
  • battery:0.30000000000000004
  • longitude:0.399632
  • :arrow_forward:

rxInfo: 1 item

  • :arrow_forward:

0:{} 10 keys
* gatewayId:“d401c3ffff910d63”
* uplinkId:38145
* gwTime:“2025-06-25T14:22:11.778837+00:00”
* nsTime:“2025-06-25T14:22:15.962391686+00:00”
* rssi:-79
* snr:10.5
* channel:3
* location:{} 0 keys
* context:“1U+7uw==”
* crcStatus:“CRC_OK”

  • :arrow_forward:

txInfo:{} 2 keys

  • frequency:867100000
  • :arrow_forward:

modulation:{} 1 key
* :arrow_forward:

lora:{} 3 keys
* bandwidth:125000
* spreadingFactor:7
* codeRate:“CR_4_5”

  • regionConfigId:“eu868”

Do you have any documentation or mapping that explains how these fields—like temperature, battery, gpsFix, or alarm.lowBatteryare coded in the bytes?

No, I only have the product documentation.

Issue is not specific to ChirpStack so you should search for “decoding GPS payload” for your sensor and skim at least first 2 pages of results. Doing that, I found decoding GPS payload from 2020.

Please advise whether that worked - sensor is interesting (to me, too). Thanks.