Code for GPS

The following is the code for the node-red function to parse the $GPGGA sentence. There is a similar function for the $GPVTG sentence, but it is a little simpler. This code is still in beta, so some other improvements are likely.

if ( isValid(msg.payload) ) {
    
    var sentence = msg.payload.split(",");

    if ((sentence.length > 10) && (sentence[0] == "$GPGGA" )) {

        if (sentence[1] !== "" ) {
            time = sentence[1];
        }
    
        if (sentence[2] !== "" && sentence[3] !== "" && !isNaN(parseFloat(sentence[2]))) {
            if (sentence[3] == "N") {
                degrees = Math.floor(parseFloat(sentence[2]) / 100);
                latitude = degrees + ((parseFloat(sentence[2]) / 100 - degrees) / 60 * 100);
            }
            if (sentence[3] == "S") {
                degrees = Math.floor(parseFloat(sentence[2]) / 100);
                latitude = - degrees - ((parseFloat(sentence[2]) / 100 - degrees) / 60 * 100);
            }
        }

        if (sentence[4] !== "" && sentence[5] !== "" && !isNaN(parseFloat(sentence[4]))) {
            if (sentence[5] == "E") {
                degrees = Math.floor(parseFloat(sentence[4]) / 100);
                longitude = degrees + ((parseFloat(sentence[4]) / 100 - degrees) / 60 * 100);
            }
            if (sentence[5] == "W") {
                degrees = Math.floor(parseFloat(sentence[4]) / 100);
                longitude = - degrees - ((parseFloat(sentence[4]) / 100 - degrees) / 60 * 100);
            }
        }
    
        if (sentence[6] !== "" && !isNaN(parseInt(sentence[6]))) {
            quality = parseInt(sentence[6]);
        }
    
        if (sentence[7] !== "" && !isNaN(parseInt(sentence[7]))) {
            satellites = parseInt(sentence[7]);
        }
    
        if (sentence[8] !== "" && !isNaN(parseFloat(sentence[8]))) {
            hdop = parseFloat(sentence[8]);
        }
    
        if (sentence[9] !== "" && sentence[10] == "M" && !isNaN(parseFloat(sentence[9]))) {
            altitude = parseFloat(sentence[9]);
        }
    
        msg.payload = [{
            fixtime: time,
            latitude: latitude,
            longitude: longitude,
            quality: quality,
            satellites: satellites,
            hdop: hdop,
            altitude: altitude
        },
        {
            source: "$GPGGA",
            sensor: "IBR900"
        }];

        return msg;
    }

}

function checksum(_command) {
    var command = _command[0] === '$' ? _command.slice(1) : _command;

    var checksum = 0;
    for(var i = 0; i < command.length; i++) {
        checksum = checksum ^ command.charCodeAt(i);
    }

    var hex = Number(checksum).toString(16).toUpperCase();
    if (hex.length < 2) {
        hex = ("00" + hex).slice(-2);
    }

    return hex;
}

function wrap(_command) {
    var command = _command[0] === '$' ? _command.slice(1) : _command;

    return '$' + command + '*' + checksum(command);
}

function strip(_command) {
    //    return _command.slice(1).slice(0, -3);
    return _command.slice(1).slice(0, -5);
}

function isValid(_command) {
    //    var _checksum = _command.slice(-2);
    var _checksum = _command.slice(-4,-2);

    var command = strip(_command);

    return _checksum === checksum(command);
}

Many thanks to the following open source project (https://github.com/vesteraas/nmea-checksum)

Back to GPS Logging