Ubidots Community

[SOLVED] GPS data has 0 zero

I am getting my GPS latitude / longitude data thru a webhook on TTN. It all works well except once and a while the latitude and longitude comes through as 0 (zero) which show my location at the equator off Africa. Very nice I’m sure but that’s not where I am. I think the device occasionally sends a bad fix. How would I eliminate the data lines where lat / long may equal zero?

@RockiesIOT

Here’s an update to your decoder that evaluates if:

  1. The latitude OR longitudes values are 0, then it only send the altitude value in the “position” variable, otherwise, it sends all these 2 values on said variable,
  2. if hdop is null or undefined it simply deletes this key from the payload.
function Decoder(bytes, port) {
    // Decode an uplink message from a buffer
    // (array) of bytes to an object of fields.

    var latitude;//gps latitude,units: °
    latitude = (bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]) / 1000000; // gps latitude,units: °

    var longitude;
    longitude = (bytes[4] << 24 | bytes[5] << 16 | bytes[6] << 8 | bytes[7]) / 1000000; // gps longitude,units: °

    var alarm = (bytes[8] & 0x40) ? 1 : 0; // Alarm status
    var batV = (((bytes[8] & 0x3f) << 8) | bytes[9]) / 1000; // Battery,units:V
    if (bytes[10] & 0xC0 == 0x40) {
        var motion_mode = "Move";
    }
    else if (bytes[10] & 0xC0 == 0x80) {
        motion_mode = "Collide";
    }
    else if (bytes[10] & 0xC0 == 0xC0) {
        motion_mode = "User";
    }
    else {
        motion_mode = "Disable";
    } //mode of motion

    var led_updown = (bytes[10] & 0x20) ? 1 : 0; // LED status for position,uplink and downlink
    var Firmware = 160 + (bytes[10] & 0x1f); // Firmware version; 5 bits
    var roll = (bytes[11] << 8 | bytes[12]) / 100; // roll,units: °
    var pitch = (bytes[13] << 8 | bytes[14]) / 100; // pitch,units: °
    var hdop = 0;

    if (bytes[15] > 0) {
        hdop = bytes[15] / 100; // hdop,units: °
    }
    else {
        hdop = bytes[15];
    }

    var altitude = (bytes[16] << 8 | bytes[17]) / 100; // Altitude,units: °

    var payload = {
        position: { "value": altitude, "context": { "lat": latitude, "lng": longitude } },
        Roll: roll,
        Pitch: pitch,
        BatV: batV,
        ALARM_status: alarm,
        MD: { "value": 1, "context": { "md": motion_mode } },
        LON: led_updown,
        FW: Firmware,
        HDOP: hdop,
    }

    if (latitude == 0 || longitude == 0) {
        payload["position"] = altitude
    }

    if (hdop === undefined || hdop == null ) {
        delete payload["HDOP"]
    }

    return payload
}
1 Like

My apology for double posting, I thought it was 2 different issues. All seems to work now with 2 small changes.
The decoder in TTS didn’t like == 0 or == null so I changed them to ===0 and === null.

Thanks again and my apologies for troubling you. I am trying to build a demo app and I appreciate your excellent software and advice.