Payload Decode for TTN - Smart Parking

Questions about the Waspmote board and its modules
Post Reply
newtonx
Posts: 5
Joined: Wed Apr 03, 2019 1:27 am
Company: OutDone

Payload Decode for TTN - Smart Parking

Post by newtonx » Sat Sep 14, 2019 1:24 pm

Hello,

I wrote a code to decode the smart parking payload for TTN can anyone confirm is this correct? not sure but looks there is some mistake

Code: Select all

function Decoder(bytes, port) {
  // Decode an uplink message from a buffer
  // (array) of bytes to an object of fields.
  var decoded = {};

  if(bytes[0] === 0){
    decoded.BasicData = "Info Frame";
    decoded.FrameCounter = bytes[1];
    decoded.Temperature = bytes[2];
    decoded.X = (bytes[3] * 256) + bytes[4]; // combine MSB + LSB
    decoded.Y = (bytes[5] * 256) + bytes[6];
    decoded.Z = (bytes[7] * 256) + bytes[8];    
    decoded.Battery = (((bytes[10] * 4) + 2800) / 1000) + " Volts";
  }else if(bytes[0] === 1){
    decoded.BasicData = "Keep Alive";
    decoded.FrameCounter = bytes[1];
    decoded.Timestamp = bytes[2] + ":" + bytes[3];
    decoded.Temperature = bytes[4];
    decoded.X = (bytes[5] * 256) + bytes[6];
    decoded.Y = (bytes[7] * 256) + bytes[8];
    decoded.Z = (bytes[9] * 256) + bytes[10];    
  }else if(bytes[0] === 2){
    decoded.BasicData = "Daily Frame";
    decoded.FrameCounter = bytes[1];
    decoded.Reset = bytes[8];
    decoded.ConfigId = bytes[9];
  }else if(bytes[0] === 4){
    var battey = bytes[10] + (bytes[9] << 8);
    decoded.BasicData = "Start Frame";
    decoded.FrameCounter = bytes[1];
    decoded.Temperature = bytes[2];
    decoded.X = (bytes[3] * 256) + bytes[4];
    decoded.Y = (bytes[5] * 256) + bytes[6];
    decoded.Z = (bytes[7] * 256) + bytes[8];
    decoded.Battery = (battey / 1000) + " Volts";
  }else if(bytes[0] === 5){
    decoded.BasicData = "Start Frame 2";
    decoded.FrameCounter = bytes[1];
    decoded.FIRMWARE_VERSION = bytes[2];
    decoded.NM_START = bytes[3];
    decoded.NM_PERIOD = bytes[4];
    decoded.NM_SLEEP_TIME = bytes[5];
    decoded.NM_KEEP_ALIVE = bytes[6];
    decoded.RADIO_MODE = bytes[7];
    decoded.SLEEP_TIME = bytes[8];
    decoded.KEEP_ALIVE = bytes[9];
    decoded.THRESHOLD = bytes[10];
  }else if(bytes[0] === 6){
    decoded.BasicData = "Service Frame";
    decoded.FrameCounter = bytes[1];
    decoded.Timestamp = bytes[2] + ":" + bytes[3];
    decoded.Temperature = bytes[4];
    decoded.X = (bytes[5] * 256) + bytes[6];
    decoded.Y = (bytes[7] * 256) + bytes[8];
    decoded.Z = (bytes[9] * 256) + bytes[10];    
  }else if(bytes[0] === 7){
    decoded.BasicData = "Downlink Frame";
    decoded.FrameCounter = bytes[1];
    decoded.Timestamp = bytes[2] + ":" + bytes[3];
    decoded.Temperature = bytes[4];
    decoded.X = (bytes[5] * 256) + bytes[6];
    decoded.Y = (bytes[7] * 256) + bytes[8];
    decoded.Z = (bytes[9] * 256) + bytes[10];    
  }else if(bytes[0] === 8){
    decoded.BasicData = "RSSI Frame";
    decoded.FrameCounter = bytes[1];
    decoded.Timestamp = bytes[2] + ":" + bytes[3];
    decoded.Temperature = bytes[4];
    decoded.X = (bytes[5] * 256) + bytes[6];
    decoded.Y = (bytes[7] * 256) + bytes[8];
    decoded.Z = (bytes[9] * 256) + bytes[10];    
  }

  return decoded;
}

newtonx
Posts: 5
Joined: Wed Apr 03, 2019 1:27 am
Company: OutDone

Re: Payload Decode for TTN - Smart Parking

Post by newtonx » Sun Sep 15, 2019 2:45 am

Sorry this is an updated one

Code: Select all

function Decoder(bytes, port) {
  // Decode an uplink message from a buffer
  // (array) of bytes to an object of fields.
  var decoded = {};

  var bin = bytes[0].toString(2);
  
  // Byte 0 and Bit 0 to Bit 4
  var frameBits = bin.substring(0, 4);
  var frameType = parseInt(frameBits, 2);
  
  // Byte 0 and Bit 6
  var needBatteryReplacement = bin.substring(6,7) == "0" ? false: true;
  
  // Byte 0 and Bit 7
  var isSlotEmpty = bin.substring(7,8) == "0" ? false: true;
  
  
  if(frameType === 0){
    decoded.BasicData = { Name: "Info Frame", ChangeBatteryRequired: needBatteryReplacement, IsSlotEmpty:  isSlotEmpty};
    decoded.FrameCounter = bytes[1];
    decoded.Temperature = bytes[2];
    decoded.X = (bytes[3] * 256) + bytes[4];
    decoded.Y = (bytes[5] * 256) + bytes[6];
    decoded.Z = (bytes[7] * 256) + bytes[8];    
    decoded.Battery = (((bytes[10] * 4) + 2800) / 1000) + " Volts";
  }else if(frameType === 1){
    decoded.BasicData = { Name: "Keep Alive Frame", ChangeBatteryRequired: needBatteryReplacement, IsSlotEmpty:  isSlotEmpty};
    decoded.FrameCounter = bytes[1];
    decoded.Timestamp = bytes[2] + ":" + bytes[3];
    decoded.Temperature = bytes[4];
    decoded.X = (bytes[5] * 256) + bytes[6];
    decoded.Y = (bytes[7] * 256) + bytes[8];
    decoded.Z = (bytes[9] * 256) + bytes[10];    
  }else if(frameType === 2){
    decoded.BasicData = { Name: "Daily Frame", ChangeBatteryRequired: needBatteryReplacement, IsSlotEmpty:  isSlotEmpty};
    decoded.FrameCounter = bytes[1];
    decoded.Reset = bytes[8];
    decoded.ConfigId = bytes[9];
  }else if(frameType === 4){
    var battey = bytes[10] + (bytes[9] << 8);
    decoded.BasicData = { Name: "Start Frame", ChangeBatteryRequired: needBatteryReplacement, IsSlotEmpty:  isSlotEmpty};
    decoded.FrameCounter = bytes[1];
    decoded.Temperature = bytes[2];
    decoded.X = (bytes[3] * 256) + bytes[4];
    decoded.Y = (bytes[5] * 256) + bytes[6];
    decoded.Z = (bytes[7] * 256) + bytes[8];
    decoded.Battery = (battey / 1000) + " Volts";
  }else if(frameType === 5){
    decoded.BasicData = { Name: "Start Frame 2", ChangeBatteryRequired: needBatteryReplacement, IsSlotEmpty:  isSlotEmpty};
    decoded.FrameCounter = bytes[1];
    decoded.FIRMWARE_VERSION = bytes[2];
    decoded.NM_START = bytes[3];
    decoded.NM_PERIOD = bytes[4];
    decoded.NM_SLEEP_TIME = bytes[5];
    decoded.NM_KEEP_ALIVE = bytes[6];
    decoded.RADIO_MODE = bytes[7];
    decoded.SLEEP_TIME = bytes[8];
    decoded.KEEP_ALIVE = bytes[9];
    decoded.THRESHOLD = bytes[10];
  }else if(frameType === 6){
    decoded.BasicData = { Name: "Service Frame", ChangeBatteryRequired: needBatteryReplacement, IsSlotEmpty:  isSlotEmpty};
    decoded.FrameCounter = bytes[1];
    decoded.Timestamp = bytes[2] + ":" + bytes[3];
    decoded.Temperature = bytes[4];
    decoded.X = (bytes[5] * 256) + bytes[6];
    decoded.Y = (bytes[7] * 256) + bytes[8];
    decoded.Z = (bytes[9] * 256) + bytes[10];    
  }else if(frameType === 7){
    decoded.BasicData = { Name: "Downlink Frame", ChangeBatteryRequired: needBatteryReplacement, IsSlotEmpty:  isSlotEmpty};
    decoded.FrameCounter = bytes[1];
    decoded.Timestamp = bytes[2] + ":" + bytes[3];
    decoded.Temperature = bytes[4];
    decoded.X = (bytes[5] * 256) + bytes[6];
    decoded.Y = (bytes[7] * 256) + bytes[8];
    decoded.Z = (bytes[9] * 256) + bytes[10];    
  }else if(frameType === 8){
    decoded.BasicData = { Name: "RSSI Frame", ChangeBatteryRequired: needBatteryReplacement, IsSlotEmpty:  isSlotEmpty};
    decoded.FrameCounter = bytes[1];
    decoded.Timestamp = bytes[2] + ":" + bytes[3];
    decoded.Temperature = bytes[4];
    decoded.X = (bytes[5] * 256) + bytes[6];
    decoded.Y = (bytes[7] * 256) + bytes[8];
    decoded.Z = (bytes[9] * 256) + bytes[10];    
  }else{
    decoded.BasicData = bytes[0].toString(2);
  }

  return decoded;
}

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: Payload Decode for TTN - Smart Parking

Post by libelium-dev » Wed Sep 18, 2019 12:50 pm

Hi,

We are sorry but we don't have experience decoding data from the TTN decoder.

Regards

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests