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