|
| 1 | +/* |
| 2 | + Get the latest GPGGA / GNGGA NMEA sentence using callbacks |
| 3 | + By: Paul Clark |
| 4 | + SparkFun Electronics |
| 5 | + Date: January 12th, 2021 |
| 6 | + License: MIT. See license file for more information but you can |
| 7 | + basically do whatever you want with this code. |
| 8 | +
|
| 9 | + This example shows how to turn on/off the NMEA sentences being output over I2C. |
| 10 | + It then demonstrates how to get the latest GPGGA or GNGGA message autonomously using callbacks. |
| 11 | +
|
| 12 | + If the module is using multiple GNSS constellations, the GGA message will be prefixed with Talker ID "GN" instead of "GP". |
| 13 | + This example shows how to change the Talker ID so the GNGGA messages become GPGGA. |
| 14 | + It also shows how to enable "high precision mode" to include extra decimal places in the GGA messages. |
| 15 | +
|
| 16 | + This example turns off all sentences except for GGA. |
| 17 | +
|
| 18 | + Feel like supporting open source hardware? |
| 19 | + Buy a board from SparkFun! |
| 20 | + ZED-F9P RTK2: https://www.sparkfun.com/products/15136 |
| 21 | + NEO-M8P RTK: https://www.sparkfun.com/products/15005 |
| 22 | + SAM-M8Q: https://www.sparkfun.com/products/15106 |
| 23 | +
|
| 24 | + Hardware Connections: |
| 25 | + Plug a Qwiic cable into the GNSS and a RedBoard |
| 26 | + If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) |
| 27 | + Open the serial monitor at 115200 baud to see the output |
| 28 | +*/ |
| 29 | + |
| 30 | +#include <Wire.h> //Needed for I2C to GNSS |
| 31 | + |
| 32 | +#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_u-blox_GNSS |
| 33 | +SFE_UBLOX_GNSS myGNSS; |
| 34 | + |
| 35 | +// Callback: printGPGGA will be called when new GPGGA NMEA data arrives |
| 36 | +// See u-blox_structs.h for the full definition of NMEA_GGA_data_t |
| 37 | +// _____ You can use any name you like for the callback. Use the same name when you call setNMEAGPGGAcallback |
| 38 | +// / _____ This _must_ be NMEA_GGA_data_t |
| 39 | +// | / _____ You can use any name you like for the struct |
| 40 | +// | | / |
| 41 | +// | | | |
| 42 | +void printGPGGA(NMEA_GGA_data_t nmeaData) |
| 43 | +{ |
| 44 | + Serial.print(F("\r\nGPGGA: Length: ")); |
| 45 | + Serial.print(nmeaData.length); |
| 46 | + Serial.print(F("\tData: ")); |
| 47 | + Serial.print((const char *)nmeaData.nmea); // .nmea is printable (NULL-terminated) and already has \r\n on the end |
| 48 | +} |
| 49 | + |
| 50 | +// Callback: printGNGGA will be called if new GNGGA NMEA data arrives |
| 51 | +// See u-blox_structs.h for the full definition of NMEA_GGA_data_t |
| 52 | +// _____ You can use any name you like for the callback. Use the same name when you call setNMEAGNGGAcallback |
| 53 | +// / _____ This _must_ be NMEA_GGA_data_t |
| 54 | +// | / _____ You can use any name you like for the struct |
| 55 | +// | | / |
| 56 | +// | | | |
| 57 | +void printGNGGA(NMEA_GGA_data_t nmeaData) |
| 58 | +{ |
| 59 | + Serial.print(F("\r\nGNGGA: Length: ")); |
| 60 | + Serial.print(nmeaData.length); |
| 61 | + Serial.print(F("\tData: ")); |
| 62 | + Serial.print((const char *)nmeaData.nmea); // .nmea is printable (NULL-terminated) and already has \r\n on the end |
| 63 | +} |
| 64 | + |
| 65 | +void setup() |
| 66 | +{ |
| 67 | + |
| 68 | + Serial.begin(115200); |
| 69 | + Serial.println(F("SparkFun u-blox GNSS Example")); |
| 70 | + |
| 71 | + Wire.begin(); |
| 72 | + |
| 73 | + //myGNSS.enableDebugging(); // Uncomment this line to enable debug messages on Serial |
| 74 | + |
| 75 | + if (myGNSS.begin() == false) |
| 76 | + { |
| 77 | + Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing.")); |
| 78 | + while (1) |
| 79 | + ; |
| 80 | + } |
| 81 | + |
| 82 | + // Disable or enable various NMEA sentences over the I2C interface |
| 83 | + myGNSS.setI2COutput(COM_TYPE_NMEA | COM_TYPE_UBX); // Turn on both UBX and NMEA sentences on I2C. (Turn off RTCM and SPARTN) |
| 84 | + myGNSS.disableNMEAMessage(UBX_NMEA_GLL, COM_PORT_I2C); // Several of these are on by default on ublox board so let's disable them |
| 85 | + myGNSS.disableNMEAMessage(UBX_NMEA_GSA, COM_PORT_I2C); |
| 86 | + myGNSS.disableNMEAMessage(UBX_NMEA_GSV, COM_PORT_I2C); |
| 87 | + myGNSS.disableNMEAMessage(UBX_NMEA_RMC, COM_PORT_I2C); |
| 88 | + myGNSS.disableNMEAMessage(UBX_NMEA_VTG, COM_PORT_I2C); |
| 89 | + myGNSS.enableNMEAMessage(UBX_NMEA_GGA, COM_PORT_I2C); // Leave only GGA enabled at current navigation rate |
| 90 | + |
| 91 | + // Set the Main Talker ID to "GP". The NMEA GGA messages will be GPGGA instead of GNGGA |
| 92 | + myGNSS.setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_GP); |
| 93 | + //myGNSS.setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_DEFAULT); // Uncomment this line to restore the default main talker ID |
| 94 | + |
| 95 | + myGNSS.setHighPrecisionMode(true); // Enable High Precision Mode - include extra decimal places in the GGA messages |
| 96 | + |
| 97 | + //myGNSS.saveConfiguration(VAL_CFG_SUBSEC_IOPORT | VAL_CFG_SUBSEC_MSGCONF); //Optional: Save only the ioPort and message settings to NVM |
| 98 | + |
| 99 | + Serial.println(F("Messages configured")); |
| 100 | + |
| 101 | + //myGNSS.setNMEAOutputPort(Serial); // Uncomment this line to echo all NMEA data to Serial for debugging |
| 102 | + |
| 103 | + // Set up the callback for GPGGA |
| 104 | + myGNSS.setNMEAGPGGAcallback(&printGPGGA); |
| 105 | + |
| 106 | + // Set up the callback for GNGGA |
| 107 | + myGNSS.setNMEAGNGGAcallback(&printGNGGA); |
| 108 | +} |
| 109 | + |
| 110 | +void loop() |
| 111 | +{ |
| 112 | + myGNSS.checkUblox(); // Check for the arrival of new data and process it. |
| 113 | + myGNSS.checkCallbacks(); // Check if any callbacks are waiting to be processed. |
| 114 | + |
| 115 | + Serial.print("."); |
| 116 | + delay(50); |
| 117 | +} |
0 commit comments