[SOLVED] How to decode TTN payload for Unibots GPS


#1

How to combine sensor latitude and longitude inside a context so Ubidots dashboard can recognise the GPS coordinates?
A few people have asked this question but I cant find an example of the code to put in the TTN decoder.


#2

Hello, please find attached an example of a decoder to send latitude and longitude in a JSON format that can be interpreted at Ubidots side:

function Decoder(bytes, port) {
  // Decode an uplink message from a buffer
  // (array) of bytes to an object of fields.
  var value = bytes[0] << 16 | bytes[1] << 8 | bytes[2];
  if (bytes[0] & 0x80) {
    value |= 0xFFFFFF000000;
  }
  var latitude = value / 10000; // gps latitude,units: °
  value = bytes[3] << 16 | bytes[4] << 8 | bytes[5];
  if (bytes[3] & 0x80) {
    value |= 0xFFFFFF000000;
  }
  var longitude = value / 10000; // gps longitude,units: °
  var alarm = (bytes[6] & 0x40) ? 1 : 0; // Alarm status
  value = ((bytes[6] & 0x3f) << 8) | bytes[7];
  var batV = value / 1000; // Battery
  value = bytes[8] << 8 | bytes[9];
  if (bytes[8] & 0x80) {
    value |= 0xFFFF0000;
  }
  var roll = value / 100; // roll, units: °
  value = bytes[10] << 8 | bytes[11];
  if (bytes[10] & 0x80) {
    value |= 0xFFFF0000;
  }
  var pitch = value / 100; // pitch, units: °
  return {
    position: {"value": 0, context:{"lat": latitude, "lng": longitude}},
    Roll: roll,
    Pitch: pitch,
    BatV: batV,
    ALARM_status: alarm,
  };
}
Contraer

I hope it may serve you as reference.

All the best


#3

thank you.
At first it only returned the latitude value in Ubidots, but after changing “value”: 0 to “value”:1 both latitude and longitude showing in Ubidots and the map widget displaying correctly.

ie
from
position: {“value”: 0, context:{“lat”: latitude, “lng”: longitude}},

to
position: {“value”: 1, context:{“lat”: latitude, “lng”: longitude}},