|
| 1 | +/* |
| 2 | + Get the NMEA sentence using getLatestNMEAGPxxx (GGA, VTG, RMC, ZDA) |
| 3 | + By: Paul Clark |
| 4 | + SparkFun Electronics |
| 5 | + Date: March 2nd, 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 use the new getLatestNMEAGPxxx function to retrieve the latest GPGGA message. |
| 11 | + getLatestNMEAGPxxx returns immediately - it is not blocking. |
| 12 | + It returns: |
| 13 | + 0 if no data is available |
| 14 | + 1 if the data is valid but is stale (you have read it before) |
| 15 | + 2 if the data is valid and fresh |
| 16 | +
|
| 17 | + If the module is using multiple GNSS constellations, the GGA message will be prefixed with Talker ID "GN" instead of "GP". |
| 18 | + The library includes getLatestNMEAGNxxx functions too. |
| 19 | + This example shows how to use both functions - and how to change the Talker ID so the GNGGA messages become GPGGA etc.. |
| 20 | +
|
| 21 | + Feel like supporting open source hardware? |
| 22 | + Buy a board from SparkFun! |
| 23 | + ZED-F9P RTK2: https://www.sparkfun.com/products/15136 |
| 24 | + NEO-M8P RTK: https://www.sparkfun.com/products/15005 |
| 25 | + SAM-M8Q: https://www.sparkfun.com/products/15106 |
| 26 | +
|
| 27 | + Hardware Connections: |
| 28 | + Plug a Qwiic cable into the GNSS and a RedBoard |
| 29 | + If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) |
| 30 | + Open the serial monitor at 115200 baud to see the output |
| 31 | +*/ |
| 32 | + |
| 33 | +#include <Wire.h> //Needed for I2C to GNSS |
| 34 | + |
| 35 | +#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_u-blox_GNSS |
| 36 | +SFE_UBLOX_GNSS myGNSS; |
| 37 | + |
| 38 | +void setup() |
| 39 | +{ |
| 40 | + |
| 41 | + Serial.begin(115200); |
| 42 | + Serial.println(F("SparkFun u-blox GNSS Example")); |
| 43 | + |
| 44 | + Wire.begin(); |
| 45 | + |
| 46 | + //myGNSS.enableDebugging(); // Uncomment this line to enable debug messages on Serial |
| 47 | + |
| 48 | + if (myGNSS.begin() == false) |
| 49 | + { |
| 50 | + Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing.")); |
| 51 | + while (1) |
| 52 | + ; |
| 53 | + } |
| 54 | + |
| 55 | + //Disable or enable various NMEA sentences over the I2C interface |
| 56 | + myGNSS.setI2COutput(COM_TYPE_NMEA | COM_TYPE_UBX); // Turn on both UBX and NMEA sentences on I2C. (Turn off RTCM and SPARTN) |
| 57 | + myGNSS.disableNMEAMessage(UBX_NMEA_GLL, COM_PORT_I2C); |
| 58 | + myGNSS.disableNMEAMessage(UBX_NMEA_GSA, COM_PORT_I2C); |
| 59 | + myGNSS.disableNMEAMessage(UBX_NMEA_GSV, COM_PORT_I2C); |
| 60 | + myGNSS.enableNMEAMessage(UBX_NMEA_RMC, COM_PORT_I2C); |
| 61 | + myGNSS.enableNMEAMessage(UBX_NMEA_VTG, COM_PORT_I2C); |
| 62 | + myGNSS.enableNMEAMessage(UBX_NMEA_GGA, COM_PORT_I2C); |
| 63 | + myGNSS.enableNMEAMessage(UBX_NMEA_ZDA, COM_PORT_I2C); |
| 64 | + |
| 65 | + // Set the Main Talker ID to "GP". The NMEA GGA messages will be GPGGA instead of GNGGA |
| 66 | + myGNSS.setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_GP); |
| 67 | + //myGNSS.setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_DEFAULT); // Uncomment this line to restore the default main talker ID |
| 68 | + |
| 69 | + myGNSS.setHighPrecisionMode(true); // Enable High Precision Mode - include extra decimal places in the GGA messages |
| 70 | + |
| 71 | + //myGNSS.saveConfiguration(VAL_CFG_SUBSEC_IOPORT | VAL_CFG_SUBSEC_MSGCONF); //Optional: Save only the ioPort and message settings to NVM |
| 72 | + |
| 73 | + Serial.println(F("Messages configured")); |
| 74 | + |
| 75 | + //myGNSS.setNMEAOutputPort(Serial); // Uncomment this line to echo all NMEA data to Serial for debugging |
| 76 | +} |
| 77 | + |
| 78 | +void loop() |
| 79 | +{ |
| 80 | + // getLatestNMEAGPGGA calls checkUblox for us. We don't need to do it here |
| 81 | + |
| 82 | + NMEA_GGA_data_t dataGGA; // Storage for the GPGGA data |
| 83 | + uint8_t result = myGNSS.getLatestNMEAGPGGA(&dataGGA); // Get the latest GPGGA data (if any) |
| 84 | + |
| 85 | + if (result == 2) |
| 86 | + { |
| 87 | + // Data contains .length and .nmea |
| 88 | + Serial.print(F("Latest GPGGA: Length: ")); |
| 89 | + Serial.print(dataGGA.length); |
| 90 | + Serial.print(F("\tData: ")); |
| 91 | + Serial.print((const char *)dataGGA.nmea); // .nmea is printable (NULL-terminated) |
| 92 | + } |
| 93 | + |
| 94 | + result = myGNSS.getLatestNMEAGNGGA(&dataGGA); // Get the latest GNGGA data (if any) |
| 95 | + |
| 96 | + if (result == 2) |
| 97 | + { |
| 98 | + // Data contains .length and .nmea |
| 99 | + Serial.print(F("Latest GNGGA: Length: ")); |
| 100 | + Serial.print(dataGGA.length); |
| 101 | + Serial.print(F("\tData: ")); |
| 102 | + Serial.print((const char *)dataGGA.nmea); // .nmea is printable (NULL-terminated) |
| 103 | + } |
| 104 | + |
| 105 | + // getLatestNMEAGPVTG calls checkUblox for us. We don't need to do it here |
| 106 | + |
| 107 | + NMEA_VTG_data_t dataVTG; // Storage for the GPVTG data |
| 108 | + result = myGNSS.getLatestNMEAGPVTG(&dataVTG); // Get the latest GPVTG data (if any) |
| 109 | + |
| 110 | + if (result == 2) |
| 111 | + { |
| 112 | + // Data contains .length and .nmea |
| 113 | + Serial.print(F("Latest GPVTG: Length: ")); |
| 114 | + Serial.print(dataVTG.length); |
| 115 | + Serial.print(F("\tData: ")); |
| 116 | + Serial.print((const char *)dataVTG.nmea); // .nmea is printable (NULL-terminated) |
| 117 | + } |
| 118 | + |
| 119 | + result = myGNSS.getLatestNMEAGNVTG(&dataVTG); // Get the latest GNVTG data (if any) |
| 120 | + |
| 121 | + if (result == 2) |
| 122 | + { |
| 123 | + // Data contains .length and .nmea |
| 124 | + Serial.print(F("Latest GNVTG: Length: ")); |
| 125 | + Serial.print(dataVTG.length); |
| 126 | + Serial.print(F("\tData: ")); |
| 127 | + Serial.print((const char *)dataVTG.nmea); // .nmea is printable (NULL-terminated) |
| 128 | + } |
| 129 | + |
| 130 | + // getLatestNMEAGPRMC calls checkUblox for us. We don't need to do it here |
| 131 | + |
| 132 | + NMEA_RMC_data_t dataRMC; // Storage for the GPRMC data |
| 133 | + result = myGNSS.getLatestNMEAGPRMC(&dataRMC); // Get the latest GPRMC data (if any) |
| 134 | + |
| 135 | + if (result == 2) |
| 136 | + { |
| 137 | + // Data contains .length and .nmea |
| 138 | + Serial.print(F("Latest GPRMC: Length: ")); |
| 139 | + Serial.print(dataRMC.length); |
| 140 | + Serial.print(F("\tData: ")); |
| 141 | + Serial.print((const char *)dataRMC.nmea); // .nmea is printable (NULL-terminated) |
| 142 | + } |
| 143 | + |
| 144 | + result = myGNSS.getLatestNMEAGNRMC(&dataRMC); // Get the latest GNRMC data (if any) |
| 145 | + |
| 146 | + if (result == 2) |
| 147 | + { |
| 148 | + // Data contains .length and .nmea |
| 149 | + Serial.print(F("Latest GNRMC: Length: ")); |
| 150 | + Serial.print(dataRMC.length); |
| 151 | + Serial.print(F("\tData: ")); |
| 152 | + Serial.print((const char *)dataRMC.nmea); // .nmea is printable (NULL-terminated) |
| 153 | + } |
| 154 | + |
| 155 | + // getLatestNMEAGPZDA calls checkUblox for us. We don't need to do it here |
| 156 | + |
| 157 | + NMEA_ZDA_data_t dataZDA; // Storage for the GPZDA data |
| 158 | + result = myGNSS.getLatestNMEAGPZDA(&dataZDA); // Get the latest GPZDA data (if any) |
| 159 | + |
| 160 | + if (result == 2) |
| 161 | + { |
| 162 | + // Data contains .length and .nmea |
| 163 | + Serial.print(F("Latest GPZDA: Length: ")); |
| 164 | + Serial.print(dataZDA.length); |
| 165 | + Serial.print(F("\tData: ")); |
| 166 | + Serial.print((const char *)dataZDA.nmea); // .nmea is printable (NULL-terminated) |
| 167 | + } |
| 168 | + |
| 169 | + result = myGNSS.getLatestNMEAGNZDA(&dataZDA); // Get the latest GNZDA data (if any) |
| 170 | + |
| 171 | + if (result == 2) |
| 172 | + { |
| 173 | + // Data contains .length and .nmea |
| 174 | + Serial.print(F("Latest GNZDA: Length: ")); |
| 175 | + Serial.print(dataZDA.length); |
| 176 | + Serial.print(F("\tData: ")); |
| 177 | + Serial.print((const char *)dataZDA.nmea); // .nmea is printable (NULL-terminated) |
| 178 | + } |
| 179 | + |
| 180 | + delay(250); |
| 181 | +} |
0 commit comments