Skip to content

Commit 691ea47

Browse files
authoredMar 30, 2021
Merge pull request #16 from UT2UH/getTime
getUnixEpoch() method added
2 parents 348bcb2 + 82c4020 commit 691ea47

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Getting Unix Epoch Time and micros using u-blox commands
3+
By: UT2UH
4+
Date: March 30th, 2021
5+
License: MIT. See license file for more information but you can
6+
basically do whatever you want with this code.
7+
8+
This example shows how to query a u-blox module for the current time and date as Unix Epoch uint32_t type to avoid time.h dependency.
9+
We also turn off the NMEA output on the I2C port. This decreases the amount of I2C traffic dramatically.
10+
11+
Leave NMEA parsing behind. Now you can simply ask the module for the datums you want!
12+
13+
Feel like supporting open source hardware?
14+
Buy a board from SparkFun!
15+
ZED-F9P RTK2: https://www.sparkfun.com/products/15136
16+
NEO-M8P RTK: https://www.sparkfun.com/products/15005
17+
SAM-M8Q: https://www.sparkfun.com/products/15106
18+
19+
Hardware Connections:
20+
Plug a Qwiic cable into the GNSS and a BlackBoard
21+
If you don't have a platform with a Qwiic connection use the SparkFun Qwiic Breadboard Jumper (https://www.sparkfun.com/products/14425)
22+
Open the serial monitor at 115200 baud to see the output
23+
*/
24+
25+
#include <Wire.h> //Needed for I2C to GNSS
26+
27+
#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //http://librarymanager/All#SparkFun_u-blox_GNSS
28+
SFE_UBLOX_GNSS myGNSS;
29+
30+
31+
long lastTime = 0; //Simple local timer. Limits amount if I2C traffic to u-blox module.
32+
33+
uint32_t us; //microseconds returned by getUnixEpoch()
34+
35+
void setup()
36+
{
37+
Serial.begin(115200);
38+
while (!Serial)
39+
; //Wait for user to open terminal
40+
Serial.println("SparkFun u-blox Example");
41+
42+
Wire.begin();
43+
44+
if (myGNSS.begin() == false) //Connect to the u-blox module using Wire port
45+
{
46+
Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing."));
47+
while (1)
48+
;
49+
}
50+
51+
myGNSS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise)
52+
//myGNSS.saveConfiguration(); //Optional: Save the current settings to flash and BBR
53+
54+
Serial.println("Compare Unix Epoch given with reference one from https://www.epochconverter.com/");
55+
56+
}
57+
58+
void loop()
59+
{
60+
//Query module only every second. Doing it more often will just cause I2C traffic.
61+
//The module only responds when a new position is available
62+
if (millis() - lastTime > 1000)
63+
{
64+
lastTime = millis(); //Update the timer
65+
66+
byte SIV = myGNSS.getSIV();
67+
Serial.print(F(" SIV: "));
68+
Serial.print(SIV);
69+
70+
Serial.print(" ");
71+
Serial.print(myGNSS.getYear());
72+
Serial.print("-");
73+
Serial.print(myGNSS.getMonth());
74+
Serial.print("-");
75+
Serial.print(myGNSS.getDay());
76+
Serial.print(" ");
77+
Serial.print(myGNSS.getHour());
78+
Serial.print(":");
79+
Serial.print(myGNSS.getMinute());
80+
Serial.print(":");
81+
Serial.print(myGNSS.getSecond());
82+
Serial.print(" getUnixEpoch(micros): ");
83+
Serial.print(myGNSS.getUnixEpoch(us));
84+
Serial.print(" micros: ");
85+
Serial.print(us, DEC);
86+
87+
Serial.print(" Time is ");
88+
if (myGNSS.getTimeValid() == false)
89+
{
90+
Serial.print("not ");
91+
}
92+
Serial.print("valid ");
93+
if (myGNSS.getConfirmedTime() == false)
94+
{
95+
Serial.print("but not ");
96+
} else {
97+
Serial.print("and ");
98+
}
99+
Serial.print("confirmed");
100+
101+
Serial.println();
102+
}
103+
}

‎keywords.txt

+3
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ getMinute KEYWORD2
397397
getSecond KEYWORD2
398398
getMillisecond KEYWORD2
399399
getNanosecond KEYWORD2
400+
getUnixEpoch KEYWORD2
400401
getDateValid KEYWORD2
401402
getTimeValid KEYWORD2
402403
getConfirmedDate KEYWORD2
@@ -610,3 +611,5 @@ SFE_UBLOX_GNSS_ID_BEIDOU LITERAL1
610611
SFE_UBLOX_GNSS_ID_IMES LITERAL1
611612
SFE_UBLOX_GNSS_ID_QZSS LITERAL1
612613
SFE_UBLOX_GNSS_ID_GLONASS LITERAL1
614+
615+
DAYS_SINCE_MONTH LITERAL1

‎src/SparkFun_u-blox_GNSS_Arduino_Library.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -8954,6 +8954,45 @@ int32_t SFE_UBLOX_GNSS::getNanosecond(uint16_t maxWait)
89548954
return (packetUBXNAVPVT->data.nano);
89558955
}
89568956

8957+
//Get the current Unix epoch - includes microseconds
8958+
uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint32_t& microsecond, uint16_t maxWait)
8959+
{
8960+
if (packetUBXNAVPVT == NULL) initPacketUBXNAVPVT(); //Check that RAM has been allocated for the PVT data
8961+
if (packetUBXNAVPVT == NULL) //Bail if the RAM allocation failed
8962+
return 0;
8963+
8964+
if (packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.confirmedTime == false)
8965+
getPVT(maxWait);
8966+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.confirmedTime = false;
8967+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.year = false;
8968+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.month = false;
8969+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.day = false;
8970+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.hour = false;
8971+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.min = false;
8972+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec = false;
8973+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.nano = false;
8974+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.all = false;
8975+
uint32_t t = 0;
8976+
if((bool)packetUBXNAVPVT->data.flags2.bits.confirmedTime)
8977+
{
8978+
// assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza
8979+
t = (uint32_t)(((((((packetUBXNAVPVT->data.year - 1970) * 365) + (((packetUBXNAVPVT->data.year - 1970) + 3) / 4)) +
8980+
DAYS_SINCE_MONTH[(packetUBXNAVPVT->data.year - 1970) & 3][packetUBXNAVPVT->data.month] +
8981+
(packetUBXNAVPVT->data.day - 1)) * 24 +
8982+
packetUBXNAVPVT->data.hour) * 60 +
8983+
packetUBXNAVPVT->data.min) * 60 +
8984+
packetUBXNAVPVT->data.sec);
8985+
int32_t us = packetUBXNAVPVT->data.nano / 1000;
8986+
microsecond = (uint32_t)us;
8987+
// ajust t if nano is negative
8988+
if(us < 0) {
8989+
microsecond = (uint32_t)(us + 1000000);
8990+
t--;
8991+
}
8992+
}
8993+
return t;
8994+
}
8995+
89578996
//Get the current date validity
89588997
bool SFE_UBLOX_GNSS::getDateValid(uint16_t maxWait)
89598998
{

‎src/SparkFun_u-blox_GNSS_Arduino_Library.h

+9
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,14 @@ typedef struct
454454
bool moduleQueried;
455455
} moduleSWVersion_t;
456456

457+
const uint16_t DAYS_SINCE_MONTH[4][16] =
458+
{
459+
{ 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 335, 335, 335 },
460+
{ 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 334, 334, 334 },
461+
{ 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 334, 334, 334 },
462+
{ 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 334, 334, 334 },
463+
};
464+
457465
class SFE_UBLOX_GNSS
458466
{
459467
public:
@@ -930,6 +938,7 @@ class SFE_UBLOX_GNSS
930938
uint8_t getSecond(uint16_t maxWait = defaultMaxWait);
931939
uint16_t getMillisecond(uint16_t maxWait = defaultMaxWait);
932940
int32_t getNanosecond(uint16_t maxWait = defaultMaxWait);
941+
uint32_t getUnixEpoch(uint32_t& microsecond, uint16_t maxWait = defaultMaxWait);
933942

934943
bool getDateValid(uint16_t maxWait = defaultMaxWait);
935944
bool getTimeValid(uint16_t maxWait = defaultMaxWait);

0 commit comments

Comments
 (0)
Please sign in to comment.