|
| 1 | +/* |
| 2 | + Getting time and date using Ublox commands |
| 3 | + By: davidallenmann |
| 4 | + SparkFun Electronics |
| 5 | + Date: April 16th, 2019 |
| 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 query a Ublox module for the current time and date. We also |
| 10 | + turn off the NMEA output on the I2C port. This decreases the amount of I2C traffic |
| 11 | + dramatically. |
| 12 | +
|
| 13 | + Leave NMEA parsing behind. Now you can simply ask the module for the datums you want! |
| 14 | +
|
| 15 | + Feel like supporting open source hardware? |
| 16 | + Buy a board from SparkFun! |
| 17 | + ZED-F9P RTK2: https://www.sparkfun.com/products/15136 |
| 18 | + NEO-M8P RTK: https://www.sparkfun.com/products/15005 |
| 19 | + SAM-M8Q: https://www.sparkfun.com/products/15106 |
| 20 | +
|
| 21 | + Hardware Connections: |
| 22 | + Plug a Qwiic cable into the GPS and a BlackBoard |
| 23 | + If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425) |
| 24 | + Open the serial monitor at 115200 baud to see the output |
| 25 | +*/ |
| 26 | + |
| 27 | +#include <Wire.h> //Needed for I2C to GPS |
| 28 | + |
| 29 | +#include "SparkFun_Ublox_Arduino_Library.h" //http://librarymanager/All#SparkFun_Ublox_GPS |
| 30 | +SFE_UBLOX_GPS myGPS; |
| 31 | + |
| 32 | +long lastTime = 0; //Simple local timer. Limits amount if I2C traffic to Ublox module. |
| 33 | + |
| 34 | +void setup() |
| 35 | +{ |
| 36 | + Serial.begin(500000); //Increase serial speed to maximize |
| 37 | + while (!Serial) |
| 38 | + ; //Wait for user to open terminal |
| 39 | + Serial.println("SparkFun Ublox Example"); |
| 40 | + |
| 41 | + Wire.begin(); |
| 42 | + Wire.setClock(400000); |
| 43 | + |
| 44 | + if (myGPS.begin() == false) //Connect to the Ublox module using Wire port |
| 45 | + { |
| 46 | + Serial.println(F("Ublox GPS not detected at default I2C address. Please check wiring. Freezing.")); |
| 47 | + while (1) |
| 48 | + ; |
| 49 | + } |
| 50 | + |
| 51 | + myGPS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise) |
| 52 | + myGPS.saveConfiguration(); //Save the current settings to flash and BBR |
| 53 | + |
| 54 | + //myGPS.enableDebugging(); //Enable debug messages over Serial (default) |
| 55 | + |
| 56 | + myGPS.setNavigationFrequency(10); //Set output to 10 times a second |
| 57 | + byte rate = myGPS.getNavigationFrequency(); //Get the update rate of this module |
| 58 | + Serial.print("Current update rate:"); |
| 59 | + Serial.println(rate); |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +void loop() |
| 64 | +{ |
| 65 | + //Query module only every second. Doing it more often will just cause I2C traffic. |
| 66 | + //The module only responds when a new position is available |
| 67 | + if (millis() - lastTime > 10) |
| 68 | + { |
| 69 | + lastTime = millis(); //Update the timer |
| 70 | + |
| 71 | + long latitude = myGPS.getLatitude(); |
| 72 | + Serial.print(F("Lat: ")); |
| 73 | + Serial.print(latitude); |
| 74 | + |
| 75 | + long longitude = myGPS.getLongitude(); |
| 76 | + Serial.print(F(" Long: ")); |
| 77 | + Serial.print(longitude); |
| 78 | + Serial.print(F(" (degrees * 10^-7)")); |
| 79 | + |
| 80 | + long altitude = myGPS.getAltitude(); |
| 81 | + Serial.print(F(" Alt: ")); |
| 82 | + Serial.print(altitude); |
| 83 | + Serial.print(F(" (mm)")); |
| 84 | + |
| 85 | + byte SIV = myGPS.getSIV(); |
| 86 | + Serial.print(F(" SIV: ")); |
| 87 | + Serial.print(SIV); |
| 88 | + |
| 89 | + Serial.print(myGPS.getYear()); |
| 90 | + Serial.print("-"); |
| 91 | + Serial.print(myGPS.getMonth()); |
| 92 | + Serial.print("-"); |
| 93 | + Serial.print(myGPS.getDay()); |
| 94 | + Serial.print(" "); |
| 95 | + Serial.print(myGPS.getHour()); |
| 96 | + Serial.print(":"); |
| 97 | + Serial.print(myGPS.getMinute()); |
| 98 | + Serial.print(":"); |
| 99 | + Serial.print(myGPS.getSecond()); |
| 100 | + Serial.print("."); |
| 101 | + Serial.print(myGPS.getNanosecond()); |
| 102 | + |
| 103 | + Serial.println(); |
| 104 | + } |
| 105 | +} |
0 commit comments