The GPS Parser Library is a C library for parsing GPS data from NMEA 0183 (GPGGA) payloads. It provides a simple and easy-to-use interface for extracting GPS data fields such as latitude, longitude, altitude, time, and the number of satellites from NMEA 0183 (GPGGA) strings.
The code for the GPS data parser follows these steps to extract the necessary information and monitor missing or incorrect data:
-
The GPS data parser code consists of a function called
Parse_gps_data
to fulfill the requirements of parsing data. This function takes a GPGGA string, performs operations to extract useful information, and returns the individual fields data in a structuregps_data
. -
At the start of the function, the code initializes all the fields with default values.
-
Then, it checks whether the string starts with talker ID '$GPGGA' using the built-in string function
strcmp()
. If it does, the code proceeds with the parsing. Otherwise, the function returns, and the code execution is terminated. -
Before parsing, it checks the validity of the checksum. It retrieves the exact checksum value given in the GPGGA sentence using a for loop. Next, it calculates the checksum of the given data by adding the XOR of all the characters between '$' and '*'. The XORed sum is then converted to hexadecimal to obtain the actual hexadecimal checksum.
-
The calculated checksum is compared with the extracted checksum from the given string to validate the integrity of the data.
-
Once the checksum is validated, the code starts parsing
the time_stamp
(the first field) using the built-in functionstrsep()
. This function divides a string into two halves based on the provided delimiter returning two strings, one holding the string before the delimiter (,)pre_delimiter
while the other string contains the data after the delimiter,post_delimiter
. Ifstrsep()
returns NULL, it means there is no data between two consecutive commas. -
If the time field is NULL, the code stores the
missing field
value into thetime_stamp
field. -
strsep()
is used iteratively for 14 times until the last available field. Ifstrsep()
returns an empty string, a messageNot Found!
is stored in that field. -
After it's done parsing the data is returned.
-
The data can be displayed on terminal using
ESP_LOGX
orprintf()
function.
I've executed 2 unit tests, one with a valid string containing all the fields data but one.
I implemented the second test with a NULL string. Both strings passed the test sucessfully.
To include the GPS Parser component in your IDF project, follow these steps:
- Download or clone the repository to your local machine.
- Include the
gps_data_parser.h
header file in your C source code. - Link against the GPS Parser Library during compilation.
To make this library work perfectly, the following tools are needed:
- ESP-IDF v5.0 Stable release
- ESP-IDF toolchain
gps_data Parse_gps_data(char *msg)
The Parse_gps_data()
function parses a NMEA 0183 (GPGGA) sentence and extracts the GPS data fields into a struct gps_data
.
Parameters:
msg
- A pointer to a null-terminated string representing a NMEA 0183 (GPGGA) sentence. Return:gps_data
- Structure containing the individual GPS data fields.
void print_data(gps_data)
The print_data()
function prints the extracted GPS data fields stored in a struct gps_data
to the console.
Parameters:
gps_data
- Structurestruct gps_data
containing the parsed GPS data.