Gps tracker payload decoding for gps latitude and longitude

Hello Everyone ,basically want help in decoding payload to get gps information like latitude and longitude.

payload

{"applicationID":"1","applicationName":"cloud","data":"YgAlYSgAHbo2AGR8QFocrwASACYD4gAC","devEUI":"f06a5fffff0f549e","deviceName":"lw004pb-1","fCnt":18,"fPort":222,"rxInfo":[{"altitude":0,"latitude":0,"loRaSNR":11.5,"longitude":0,"mac":"24e124fffef0a78e","name":"24e124fffef0a78e","rssi":-63,"time":"2021-01-22T10:28:43.328275Z"}],"time":"2021-01-22T10:28:43.328275Z","txInfo":{"adr":false,"codeRate":"4/5","dataRate":{"bandwidth":125,"modulation":"LORA","spreadFactor":12},"frequency":865985000}}



I have used buffer parser acc to manual to extract latitude and longitude check if its correct.Now acc to it is in little endian format means least significant byte is stored in lower memory address.after this need some guidance like simply converting hexadecimal to decimal would work.

    char hex[17];
    int decimal, place;
    int i = 0, val, len;

    decimal = 0;
    place = 1;


    /* Find the length of total number of hex digit */
    len = strlen(hex);
    len--;

    for(i=0; hex[i]!='\0'; i++)
    {
 
        /* Find the decimal representation of hex[i] */
        if(hex[i]>='0' && hex[i]<='9')
        {
            val = hex[i] - 48;
        }
        else if(hex[i]>='a' && hex[i]<='f')
        {
            val = hex[i] - 97 + 10;
        }
        else if(hex[i]>='A' && hex[i]<='F')
        {
            val = hex[i] - 65 + 10;
        }

        decimal += val * pow(16, len);
        len--;
    }

console.log(decimal);

I believe that node-red-contrib-buffer-parser is designed for exactly these problems.

Pretty sure I've been here before?

Anyhoo...

Using the sample data from the manual...

Function...

msg.payload.lat = msg.payload.lat_dec * 90 / 8388607
msg.payload.lon = msg.payload.lon_dec * 180 / 8388607
return msg;

Result...

Demo flow...

[{"id":"67cdb95c.be7d88","type":"inject","z":"a2d1264d.2189b8","name":"example data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2F0179512B0077665100D9194D750B3300D0006C03A2000E","payloadType":"str","x":1220,"y":780,"wires":[["db29b1f3.c93e6"]]},{"id":"db29b1f3.c93e6","type":"buffer-parser","z":"a2d1264d.2189b8","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint8","name":"battery","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint8","name":"alarmStatus","offset":1,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"lat_dec","offset":2,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"lon_dec","offset":6,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"hex","name":"nearestBeaconMAC","offset":10,"length":6,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1400,"y":780,"wires":[["1a005c2a.a874f4"]]},{"id":"c2db851a.fd8fd8","type":"debug","z":"a2d1264d.2189b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1430,"y":840,"wires":[]},{"id":"1a005c2a.a874f4","type":"function","z":"a2d1264d.2189b8","name":"calc coords","func":"msg.payload.lat = msg.payload.lat_dec * 90 / 8388607\nmsg.payload.lon = msg.payload.lon_dec * 180 / 8388607\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1250,"y":840,"wires":[["c2db851a.fd8fd8"]]}]
2 Likes

Thanks a lot steve , :innocent: :heart_eyes: without you it will be difficult for me to decode.Good javascripting

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.