1
+ /*
2
+ Reading lat and long via UBX binary commands over SPI (ESP32 specific example)
3
+ By: Andrew Berridge
4
+ Date: 27th June 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 its lat/long/altitude over SPI. We also
9
+ turn off the NMEA output on the SPI port. This decreases the amount of SPI traffic
10
+ dramatically.
11
+
12
+ Note: Long/lat are large numbers because they are * 10^7. To convert lat/long
13
+ to something google maps understands simply divide the numbers by 10,000,000. We
14
+ do this so that we don't have to use floating point numbers.
15
+
16
+ Leave NMEA parsing behind. Now you can simply ask the module for the datums you want!
17
+
18
+ Feel like supporting open source hardware?
19
+ Buy a board from SparkFun!
20
+ ZED-F9P RTK2: https://www.sparkfun.com/products/15136
21
+ NEO-M8P RTK: https://www.sparkfun.com/products/15005
22
+ SAM-M8Q: https://www.sparkfun.com/products/15106
23
+
24
+ Hardware Connections:
25
+ You need to connect the SPI pins from your microcontroller to the specific pins on your SparkFun product.
26
+ Connections will vary based on your microcontroller, but for reference please refer to this tutorial on SPI:
27
+ https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi/all
28
+
29
+ Most new boards now use the terms:
30
+ CS - Chip Select
31
+ COP - Controller Out, Peripheral In
32
+ CIPO - Controller in, Peripheral Out
33
+ SCK - Serial Clock
34
+
35
+ You can choose any pin for Chip Select, but the others are likely either defined by the library you are using
36
+ (see here for the standard Arduino one: https://www.arduino.cc/en/reference/SPI) or the microcontroller. The
37
+ ESP32 has two standard, selectable SPI ports, for example.
38
+
39
+ IMPORTANT: there have been reports that some ublox devices do not respond to the UBX protocol over SPI
40
+ with the factory settings. You may find you need to connect to the device via USB and u-center and set
41
+ the incoming protocol to UBX only. Make sure you disable all other protocols as inputs if you can't
42
+ get things to work! Hopefully this is just a bug in the ublox firmware that will be fixed soon ;-)
43
+
44
+ */
45
+
46
+ #include < SPI.h> // Needed for SPI to GNSS
47
+
48
+ #include < SparkFun_u-blox_GNSS_Arduino_Library.h> // http://librarymanager/All#SparkFun_u-blox_GNSS
49
+ SFE_UBLOX_GNSS myGNSS;
50
+
51
+ // Instantiate an instance of the SPI class.
52
+ // This is the default ESP32 SPI interface. Your configuration may be different, depending on the microcontroller you are using!
53
+ SPIClass spiPort (HSPI);
54
+ const uint8_t csPin = 15 ; // Change this to suit your own connection for the chip select pin
55
+
56
+ long lastTime = 0 ; // Simple local timer. Limits amount of SPI traffic to u-blox module.
57
+
58
+ void setup ()
59
+ {
60
+ Serial.begin (115200 );
61
+ while (!Serial); // Wait for user to open terminal
62
+ Serial.println (" SparkFun u-blox Example" );
63
+
64
+ spiPort.begin ();
65
+
66
+ myGNSS.setFileBufferSize (100 ); // set the file buffer size
67
+
68
+ // Connect to the u-blox module using SPI port, csPin and speed setting
69
+ // ublox devices generally work up to 5Mhz. We'll use 4Mhz for this example:
70
+ if (myGNSS.begin (spiPort, csPin, 4000000 ) == false )
71
+ {
72
+ Serial.println (F (" u-blox GNSS not detected on SPI bus. Please check wiring. Freezing." ));
73
+ while (1 );
74
+ }
75
+ myGNSS.disableMessage (UBX_CLASS_HNR, UBX_HNR_PVT, 0x04 );
76
+ myGNSS.disableMessage (UBX_CLASS_HNR, UBX_HNR_ATT, 0x04 );
77
+ myGNSS.disableMessage (UBX_CLASS_HNR, UBX_HNR_INS, 0x04 );
78
+ myGNSS.setAutoPVT (false ); // Tell the GNSS to "send" each solution
79
+ myGNSS.setPortInput (COM_PORT_SPI, COM_TYPE_UBX); // Set the SPI port to output UBX only (turn off NMEA noise)
80
+ myGNSS.saveConfigSelective (VAL_CFG_SUBSEC_IOPORT); // Save (only) the communications port settings to flash and BBR
81
+ }
82
+
83
+ void loop ()
84
+ {
85
+ // Query module only every second. Doing it more often will just cause SPI traffic.
86
+ // The module only responds when a new position is available
87
+ if (millis () - lastTime > 1000 )
88
+ {
89
+ lastTime = millis (); // Update the timer
90
+
91
+ long latitude = myGNSS.getLatitude ();
92
+ Serial.print (F (" Lat: " ));
93
+ Serial.print (latitude);
94
+
95
+ long longitude = myGNSS.getLongitude ();
96
+ Serial.print (F (" Long: " ));
97
+ Serial.print (longitude);
98
+ Serial.print (F (" (degrees * 10^-7)" ));
99
+
100
+ long altitude = myGNSS.getAltitude ();
101
+ Serial.print (F (" Alt: " ));
102
+ Serial.print (altitude);
103
+ Serial.print (F (" (mm)" ));
104
+
105
+ byte SIV = myGNSS.getSIV ();
106
+ Serial.print (F (" SIV: " ));
107
+ Serial.print (SIV);
108
+
109
+ Serial.println ();
110
+ }
111
+ }
0 commit comments