Skip to content

Commit 68e1363

Browse files
committed
getUnixEpoch() method added
1 parent b3aab36 commit 68e1363

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
#include <ezTime.h> //https://github.com/ropg/ezTime
31+
#include <WiFi.h>
32+
33+
long lastTime = 0; //Simple local timer. Limits amount if I2C traffic to u-blox module.
34+
35+
uint32_t us; //microseconds returned by getUnixEpoch()
36+
37+
void setup()
38+
{
39+
Serial.begin(115200);
40+
while (!Serial)
41+
; //Wait for user to open terminal
42+
Serial.println("SparkFun u-blox Example");
43+
44+
Wire.begin();
45+
46+
if (myGNSS.begin() == false) //Connect to the u-blox module using Wire port
47+
{
48+
Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing."));
49+
while (1)
50+
;
51+
}
52+
53+
myGNSS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise)
54+
//myGNSS.saveConfiguration(); //Optional: Save the current settings to flash and BBR
55+
56+
Serial.println("Compare Unix Epoch given with reference one from https://www.epochconverter.com/");
57+
58+
}
59+
60+
void loop()
61+
{
62+
//Query module only every second. Doing it more often will just cause I2C traffic.
63+
//The module only responds when a new position is available
64+
if (millis() - lastTime > 1000)
65+
{
66+
lastTime = millis(); //Update the timer
67+
68+
byte SIV = myGNSS.getSIV();
69+
Serial.print(F(" SIV: "));
70+
Serial.print(SIV);
71+
72+
Serial.print(" ");
73+
Serial.print(myGNSS.getYear());
74+
Serial.print("-");
75+
Serial.print(myGNSS.getMonth());
76+
Serial.print("-");
77+
Serial.print(myGNSS.getDay());
78+
Serial.print(" ");
79+
Serial.print(myGNSS.getHour());
80+
Serial.print(":");
81+
Serial.print(myGNSS.getMinute());
82+
Serial.print(":");
83+
Serial.print(myGNSS.getSecond());
84+
Serial.print(" makeTime(tm): ");
85+
Serial.print(makeTime(myGNSS.getHour(), myGNSS.getMinute(), myGNSS.getSecond(), myGNSS.getDay(), myGNSS.getMonth(), myGNSS.getYear()));
86+
Serial.print(" micros: ");
87+
Serial.print((int32_t)(myGNSS.getNanosecond() / 1000));
88+
Serial.print(" getUnixEpoch(micros): ");
89+
Serial.print(myGNSS.getUnixEpoch(us));
90+
Serial.print(" micros: ");
91+
Serial.print(us, DEC);
92+
93+
Serial.print(" Time is ");
94+
if (myGNSS.getTimeValid() == false)
95+
{
96+
Serial.print("not ");
97+
}
98+
Serial.print("valid ");
99+
if (myGNSS.getConfirmedTime() == false)
100+
{
101+
Serial.print("but not ");
102+
} else {
103+
Serial.print("and ");
104+
}
105+
Serial.print("confirmed");
106+
107+
Serial.println();
108+
}
109+
}

keywords.txt

+3
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ getMinute KEYWORD2
391391
getSecond KEYWORD2
392392
getMillisecond KEYWORD2
393393
getNanosecond KEYWORD2
394+
getUnixEpoch KEYWORD2
394395
getDateValid KEYWORD2
395396
getTimeValid KEYWORD2
396397
getConfirmedDate KEYWORD2
@@ -604,3 +605,5 @@ SFE_UBLOX_GNSS_ID_BEIDOU LITERAL1
604605
SFE_UBLOX_GNSS_ID_IMES LITERAL1
605606
SFE_UBLOX_GNSS_ID_QZSS LITERAL1
606607
SFE_UBLOX_GNSS_ID_GLONASS LITERAL1
608+
609+
DAYS_SINCE_MONTH LITERAL1

src/SparkFun_u-blox_GNSS_Arduino_Library.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -8867,6 +8867,45 @@ int32_t SFE_UBLOX_GNSS::getNanosecond(uint16_t maxWait)
88678867
return (packetUBXNAVPVT->data.nano);
88688868
}
88698869

8870+
//Get the current Unix epoch - includes microseconds
8871+
uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint32_t& microsecond, uint16_t maxWait)
8872+
{
8873+
if (packetUBXNAVPVT == NULL) initPacketUBXNAVPVT(); //Check that RAM has been allocated for the PVT data
8874+
if (packetUBXNAVPVT == NULL) //Bail if the RAM allocation failed
8875+
return 0;
8876+
8877+
if (packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.confirmedTime == false)
8878+
getPVT(maxWait);
8879+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.confirmedTime = false;
8880+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.year = false;
8881+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.month = false;
8882+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.day = false;
8883+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.hour = false;
8884+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.min = false;
8885+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec = false;
8886+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.nano = false;
8887+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.all = false;
8888+
uint32_t t = 0;
8889+
if((bool)packetUBXNAVPVT->data.flags2.bits.confirmedTime)
8890+
{
8891+
// assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza
8892+
t = (uint32_t)(((((((packetUBXNAVPVT->data.year - 1970) * 365) + (((packetUBXNAVPVT->data.year - 1970) + 3) / 4)) +
8893+
DAYS_SINCE_MONTH[(packetUBXNAVPVT->data.year - 1970) & 3][packetUBXNAVPVT->data.month] +
8894+
(packetUBXNAVPVT->data.day - 1)) * 24 +
8895+
packetUBXNAVPVT->data.hour) * 60 +
8896+
packetUBXNAVPVT->data.min) * 60 +
8897+
packetUBXNAVPVT->data.sec);
8898+
int32_t us = packetUBXNAVPVT->data.nano / 1000;
8899+
microsecond = (uint32_t)us;
8900+
// ajust t if nano is negative
8901+
if(us < 0) {
8902+
microsecond = (uint32_t)(us + 1000000);
8903+
t--;
8904+
}
8905+
}
8906+
return t;
8907+
}
8908+
88708909
//Get the current date validity
88718910
bool SFE_UBLOX_GNSS::getDateValid(uint16_t maxWait)
88728911
{

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:
@@ -924,6 +932,7 @@ class SFE_UBLOX_GNSS
924932
uint8_t getSecond(uint16_t maxWait = defaultMaxWait);
925933
uint16_t getMillisecond(uint16_t maxWait = defaultMaxWait);
926934
int32_t getNanosecond(uint16_t maxWait = defaultMaxWait);
935+
uint32_t getUnixEpoch(uint32_t& microsecond, uint16_t maxWait = defaultMaxWait);
927936

928937
bool getDateValid(uint16_t maxWait = defaultMaxWait);
929938
bool getTimeValid(uint16_t maxWait = defaultMaxWait);

0 commit comments

Comments
 (0)