Skip to main content

GPSUARTDriver

Latest Version: 1.2.0

This library is a driver class for GPS modules that can be interfaced over UART. It has been tested on UBlox NEO-M8N and UBlox LEA-6S modules.

Note The class methods hasFix(), getLatitude() and getLongitude(), and the constructor’s parseData option are dependent on the GPSParser library. If GPSParser is not detected, the class methods will return an error string, and the parseData option will default to false.

You can view the library’s source code on GitHub. Click here to see information on the available versions of this library.

To use this library in your project, add #require "GPSUARTDriver.device.lib.nut:1.2.0" to the top of your device code.

GPSUARTDriver Usage

Constructor: GPSUARTDriver(uart[, options])

Parameters

Parameter Type Required Description
uart String Yes An imp UART bus to which the GPS module is connected
options String No A table of settings to override default behaviors (see below)

Any table passed into options may contain any of the following keys:

Key Type Default Description
baudRate Integer 9600 The baud rate used to configure the UART
wordSize Integer 8 The word size in bits (7 or 8) used to configure the UART
parity Integer PARITY_NONE Parity (PARITY_NONE, PARITY_EVEN or PARITY_ODD) used to configure the UART
stopBits Integer 1 Stop bits (1 or 2) used to configure the UART
gpsDataReady Function null Callback that is called when a new GPS sentence is received. The callback has two parameters of its own, both required: a boolean, hasActiveLocation, indicating whether the GPS sentence has active location data, and gpsData, which will be either the GPS sentence or a table with parsed GPS data
parseData Boolean false If false, the unparsed GPS sentence will be passed to the gpsDataReady callback’s gpsData parameter. If true, and GPSParser is detected, the gpsData parameter will contain the table returned by GPSParser.getGPSDataTable()
rxFifoSize Integer The OS default (currently 80) Sets the size (in bytes) of the input FIFO stack of the UART serial bus

GPSUARTDriver Methods

hasFix()

If GPSParser is detected, this method indicates whether the GPS module has sufficient data to get a fix on the device’s location.

Return Value

Boolean — true if the module has a fix, otherwise false (or an error string if GPSParser is not loaded).

getLatitude()

If GPSParser is detected, this method returns a string with the last known latitude in decimal degrees.

Return Value

String — the latitude or null, or an error string if GPSParser is not loaded.

getLongitude()

If GPSParser is detected, this method returns a string with the last known longitude in decimal degrees.

Return Value

String — the longitude or null, or an error string if GPSParser is not loaded.

getGPSSentence()

This method provides the most recently received GPS sentence.

Return Value

String — the last GPS sentence, or null if no sentences have been received.

Full GPSUARTDriver Example

#require "GPSParser.device.lib.nut:1.0.0"
#require "GPSUARTDriver.device.lib.nut:1.2.0"

// Create GPS variable
local gps = null;

// GPS callback
function gpsHandler(hasLocation, data) {
    // Log location or GPS sentence
    if (hasLocation) {
        server.log(format("Latitude: %s, Longitude: %s", gps.getLatitude(), gps.getLongitude()));
    } else {
        server.log(gps.getGPSSentence());
    }

    // If we don't have a fix log number of satellites in view
    if (!gps.hasFix() && "numSatellites" in data) {
        server.log(format("Number of satellites: %s", data.numSatellites));
    }
}

// GPS options
local gpsOpts = {"gpsDataReady" : gpsHandler, "parseData" : true};

// Initialize GPS UART driver
gps = GPSUARTDriver(hardware.uart1, gpsOpts);

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release
1.1.0 GitHub Add the option to set the RX FIF buffer size
1.2.0 GitHub Fix options table parameter typo

License

This library is licensed under the MIT License.