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
+ }
0 commit comments