|
| 1 | +/* |
| 2 | + Configuring the GNSS to automatically send NAV PVT reports over I2C and log them to file on SD card |
| 3 | + By: Paul Clark |
| 4 | + SparkFun Electronics |
| 5 | + Date: October 18th, 2021 |
| 6 | + License: MIT. See license file for more information. |
| 7 | +
|
| 8 | + This example shows how to configure the u-blox GNSS to send NAV PVT reports automatically |
| 9 | + and log the data to SD card in UBX format. |
| 10 | +
|
| 11 | + This code is intended to be run on the ESP32 Thing Plus USB-C |
| 12 | + but can be adapted by changing the chip select pin and SPI definitions: |
| 13 | + https://www.sparkfun.com/products/20168 |
| 14 | + |
| 15 | + Hardware Connections: |
| 16 | + Please see: https://learn.sparkfun.com/tutorials/esp32-thing-plus-usb-c-hookup-guide |
| 17 | + Connect your GNSS breakout to the Thing Plus C using a Qwiic cable. |
| 18 | + Insert a formatted micro-SD card into the socket on the Thing Plus. |
| 19 | + Connect the Thing Plus to your computer using a USB-C cable. |
| 20 | + This code has been tested using version 2.0.5 of the Espressif Systems ESP32 board package on Arduino IDE 1.8.19. |
| 21 | + Select "SparkFun ESP32 Thing Plus C" as the board type. |
| 22 | + Press upload to upload the code onto the ESP32. |
| 23 | + Open the Serial Monitor at 115200 baud to see the output. |
| 24 | +
|
| 25 | + To minimise I2C bus errors, it is a good idea to open the I2C pull-up split pad links on |
| 26 | + the u-blox module breakout. |
| 27 | +
|
| 28 | + Data is logged in u-blox UBX format. Please see the u-blox protocol specification for more details. |
| 29 | + You can replay and analyze the data using u-center: |
| 30 | + https://www.u-blox.com/en/product/u-center |
| 31 | +
|
| 32 | + Feel like supporting open source hardware? |
| 33 | + Buy a board from SparkFun! |
| 34 | + SparkFun GPS-RTK2 - ZED-F9P (GPS-15136) https://www.sparkfun.com/products/15136 |
| 35 | + SparkFun GPS-RTK-SMA - ZED-F9P (GPS-16481) https://www.sparkfun.com/products/16481 |
| 36 | + SparkFun MAX-M10S Breakout (GPS-18037) https://www.sparkfun.com/products/18037 |
| 37 | + SparkFun ZED-F9K Breakout (GPS-18719) https://www.sparkfun.com/products/18719 |
| 38 | + SparkFun ZED-F9R Breakout (GPS-16344) https://www.sparkfun.com/products/16344 |
| 39 | +
|
| 40 | +*/ |
| 41 | + |
| 42 | +#include "FS.h" |
| 43 | +#include <SPI.h> |
| 44 | +#include <SD.h> |
| 45 | +#include <Wire.h> //Needed for I2C to GNSS |
| 46 | + |
| 47 | +#include <SparkFun_u-blox_GNSS_v3.h> //Click here to get the library: http://librarymanager/All#SparkFun_u-blox_GNSS_v3 |
| 48 | +SFE_UBLOX_GNSS myGNSS; |
| 49 | + |
| 50 | +File myFile; //File that all GNSS data is written to |
| 51 | + |
| 52 | +//Define the microSD (SPI) Chip Select pin. Adjust for your processor if necessary. |
| 53 | +const int sd_cs = 5; //Thing Plus C |
| 54 | + |
| 55 | +#define packetLength 100 // NAV PVT is 92 + 8 bytes in length (including the sync chars, class, id, length and checksum bytes) |
| 56 | +uint8_t *myBuffer; // Use myBuffer to hold the data while we write it to SD card |
| 57 | + |
| 58 | +// Callback: printPVTdata will be called when new NAV PVT data arrives |
| 59 | +// See u-blox_structs.h for the full definition of UBX_NAV_PVT_data_t |
| 60 | +// _____ You can use any name you like for the callback. Use the same name when you call setAutoPVTcallback |
| 61 | +// / _____ This _must_ be UBX_NAV_PVT_data_t |
| 62 | +// | / _____ You can use any name you like for the struct |
| 63 | +// | | / |
| 64 | +// | | | |
| 65 | +void printPVTdata(UBX_NAV_PVT_data_t *ubxDataStruct) |
| 66 | +{ |
| 67 | + Serial.println(); |
| 68 | + |
| 69 | + Serial.print(F("Time: ")); // Print the time |
| 70 | + uint8_t hms = ubxDataStruct->hour; // Print the hours |
| 71 | + if (hms < 10) Serial.print(F("0")); // Print a leading zero if required |
| 72 | + Serial.print(hms); |
| 73 | + Serial.print(F(":")); |
| 74 | + hms = ubxDataStruct->min; // Print the minutes |
| 75 | + if (hms < 10) Serial.print(F("0")); // Print a leading zero if required |
| 76 | + Serial.print(hms); |
| 77 | + Serial.print(F(":")); |
| 78 | + hms = ubxDataStruct->sec; // Print the seconds |
| 79 | + if (hms < 10) Serial.print(F("0")); // Print a leading zero if required |
| 80 | + Serial.print(hms); |
| 81 | + Serial.print(F(".")); |
| 82 | + unsigned long millisecs = ubxDataStruct->iTOW % 1000; // Print the milliseconds |
| 83 | + if (millisecs < 100) Serial.print(F("0")); // Print the trailing zeros correctly |
| 84 | + if (millisecs < 10) Serial.print(F("0")); |
| 85 | + Serial.print(millisecs); |
| 86 | + |
| 87 | + long latitude = ubxDataStruct->lat; // Print the latitude |
| 88 | + Serial.print(F(" Lat: ")); |
| 89 | + Serial.print(latitude); |
| 90 | + |
| 91 | + long longitude = ubxDataStruct->lon; // Print the longitude |
| 92 | + Serial.print(F(" Long: ")); |
| 93 | + Serial.print(longitude); |
| 94 | + Serial.print(F(" (degrees * 10^-7)")); |
| 95 | + |
| 96 | + long altitude = ubxDataStruct->hMSL; // Print the height above mean sea level |
| 97 | + Serial.print(F(" Height above MSL: ")); |
| 98 | + Serial.print(altitude); |
| 99 | + Serial.println(F(" (mm)")); |
| 100 | +} |
| 101 | + |
| 102 | +void setup() |
| 103 | +{ |
| 104 | + delay(1000); |
| 105 | + |
| 106 | + Serial.begin(115200); |
| 107 | + Serial.println("SparkFun u-blox Example"); |
| 108 | + |
| 109 | + Wire.begin(); // Start I2C communication with the GNSS |
| 110 | + |
| 111 | + while (Serial.available()) // Make sure the Serial buffer is empty |
| 112 | + { |
| 113 | + Serial.read(); |
| 114 | + } |
| 115 | + |
| 116 | + Serial.println(F("Press any key to start logging.")); |
| 117 | + |
| 118 | + while (!Serial.available()) // Wait for the user to press a key |
| 119 | + { |
| 120 | + ; // Do nothing |
| 121 | + } |
| 122 | + |
| 123 | + delay(100); // Wait, just in case multiple characters were sent |
| 124 | + |
| 125 | + while (Serial.available()) // Empty the Serial buffer |
| 126 | + { |
| 127 | + Serial.read(); |
| 128 | + } |
| 129 | + |
| 130 | + Serial.println("Initializing SD card..."); |
| 131 | + |
| 132 | + // See if the card is present and can be initialized: |
| 133 | + if (!SD.begin(sd_cs)) |
| 134 | + { |
| 135 | + Serial.println("Card failed, or not present. Freezing..."); |
| 136 | + // don't do anything more: |
| 137 | + while (1); |
| 138 | + } |
| 139 | + Serial.println("SD card initialized."); |
| 140 | + |
| 141 | + // Create or open a file called "NAV_PVT.ubx" on the SD card. |
| 142 | + // If the file already exists, the new data is appended to the end of the file. |
| 143 | + myFile = SD.open("/NAV_PVT.ubx", FILE_WRITE); |
| 144 | + if(!myFile) |
| 145 | + { |
| 146 | + Serial.println(F("Failed to create UBX data file! Freezing...")); |
| 147 | + while (1); |
| 148 | + } |
| 149 | + |
| 150 | + //myGNSS.enableDebugging(); // Uncomment this line to enable helpful GNSS debug messages on Serial |
| 151 | + |
| 152 | + // NAV PVT messages are 100 bytes long. |
| 153 | + // In this example, the data will arrive no faster than one message per second. |
| 154 | + // So, setting the file buffer size to 301 bytes should be more than adequate. |
| 155 | + // I.e. room for three messages plus an empty tail byte. |
| 156 | + myGNSS.setFileBufferSize(301); // setFileBufferSize must be called _before_ .begin |
| 157 | + |
| 158 | + if (myGNSS.begin() == false) //Connect to the u-blox module using Wire port |
| 159 | + { |
| 160 | + Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing...")); |
| 161 | + while (1); |
| 162 | + } |
| 163 | + |
| 164 | + // Uncomment the next line if you want to reset your module back to the default settings with 1Hz navigation rate |
| 165 | + // (This will also disable any "auto" messages that were enabled and saved by other examples and reduce the load on the I2C bus) |
| 166 | + //myGNSS.factoryDefault(); delay(5000); |
| 167 | + |
| 168 | + myGNSS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise) |
| 169 | + myGNSS.saveConfigSelective(VAL_CFG_SUBSEC_IOPORT); //Save (only) the communications port settings to flash and BBR |
| 170 | + |
| 171 | + myGNSS.setNavigationFrequency(1); //Produce one navigation solution per second |
| 172 | + |
| 173 | + myGNSS.setAutoPVTcallbackPtr(&printPVTdata); // Enable automatic NAV PVT messages with callback to printPVTdata |
| 174 | + |
| 175 | + myGNSS.logNAVPVT(); // Enable NAV PVT data logging |
| 176 | + |
| 177 | + myBuffer = new uint8_t[packetLength]; // Create our own buffer to hold the data while we write it to SD card |
| 178 | + |
| 179 | + Serial.println(F("Press any key to stop logging.")); |
| 180 | +} |
| 181 | + |
| 182 | +void loop() |
| 183 | +{ |
| 184 | + myGNSS.checkUblox(); // Check for the arrival of new data and process it. |
| 185 | + myGNSS.checkCallbacks(); // Check if any callbacks are waiting to be processed. |
| 186 | + |
| 187 | + if (myGNSS.fileBufferAvailable() >= packetLength) // Check to see if a new packetLength-byte NAV PVT message has been stored |
| 188 | + { |
| 189 | + myGNSS.extractFileBufferData(myBuffer, packetLength); // Extract exactly packetLength bytes from the UBX file buffer and put them into myBuffer |
| 190 | + |
| 191 | + myFile.write(myBuffer, packetLength); // Write exactly packetLength bytes from myBuffer to the ubxDataFile on the SD card |
| 192 | + |
| 193 | + //printBuffer(myBuffer); // Uncomment this line to print the data as Hexadecimal bytes |
| 194 | + } |
| 195 | + |
| 196 | + if (Serial.available()) // Check if the user wants to stop logging |
| 197 | + { |
| 198 | + myFile.close(); // Close the data file |
| 199 | + Serial.println(F("\r\nLogging stopped. Freezing...")); |
| 200 | + while(1); // Do nothing more |
| 201 | + } |
| 202 | + |
| 203 | + Serial.print("."); |
| 204 | + delay(50); |
| 205 | +} |
| 206 | + |
| 207 | +// Print the buffer contents as Hexadecimal bytes |
| 208 | +// You should see: |
| 209 | +// SYNC CHAR 1: 0xB5 |
| 210 | +// SYNC CHAR 2: 0x62 |
| 211 | +// CLASS: 0x01 for NAV |
| 212 | +// ID: 0x07 for PVT |
| 213 | +// LENGTH: 2-bytes Little Endian (0x5C00 = 92 bytes for NAV PVT) |
| 214 | +// PAYLOAD: LENGTH bytes |
| 215 | +// CHECKSUM_A |
| 216 | +// CHECKSUM_B |
| 217 | +// Please see the u-blox protocol specification for more details |
| 218 | +void printBuffer(uint8_t *ptr) |
| 219 | +{ |
| 220 | + for (int i = 0; i < packetLength; i++) |
| 221 | + { |
| 222 | + if (ptr[i] < 16) Serial.print("0"); // Print a leading zero if required |
| 223 | + Serial.print(ptr[i], HEX); // Print the byte as Hexadecimal |
| 224 | + Serial.print(" "); |
| 225 | + } |
| 226 | + Serial.println(); |
| 227 | +} |
0 commit comments