|
| 1 | +/* |
| 2 | + Reading lat and long via UBX binary commands using UART @38400 baud - free from I2C |
| 3 | + By: Nathan Seidle, Adapted from Example3_GetPosition by Thorsten von Eicken |
| 4 | + SparkFun Electronics |
| 5 | + Date: January 28rd, 2019 |
| 6 | + License: MIT. See license file for more information but you can |
| 7 | + basically do whatever you want with this code. |
| 8 | +
|
| 9 | + This example shows how to configure the library and U-Blox for serial port use as well as |
| 10 | + switching the module from the default 9600 baud to 38400. |
| 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 | + Connect the U-Blox serial port to Serial1 |
| 26 | + Open the serial monitor at 115200 baud to see the output |
| 27 | +*/ |
| 28 | + |
| 29 | +#include "SparkFun_Ublox_Arduino_Library.h" //http://librarymanager/All#SparkFun_Ublox_GPS |
| 30 | +SFE_UBLOX_GPS myGPS; |
| 31 | + |
| 32 | +long lastTime = 0; //Simple local timer. Limits amount if I2C traffic to Ublox module. |
| 33 | + |
| 34 | +void setup() |
| 35 | +{ |
| 36 | + Serial.begin(115200); |
| 37 | + while (!Serial); //Wait for user to open terminal |
| 38 | + Serial.println("SparkFun Ublox Example"); |
| 39 | + |
| 40 | + //Assume that the U-Blox GPS is running at 9600 baud (the default) or at 38400 baud. |
| 41 | + //Loop until we're in sync and then ensure it's at 38400 baud. |
| 42 | + do { |
| 43 | + Serial.println("GPS: trying 38400 baud"); |
| 44 | + Serial1.begin(38400); |
| 45 | + if (myGPS.begin(Serial1)) break; |
| 46 | + |
| 47 | + delay(100); |
| 48 | + Serial.println("GPS: trying 9600 baud"); |
| 49 | + Serial1.begin(9600); |
| 50 | + if (myGPS.begin(Serial1)) { |
| 51 | + Serial.println("GPS: connected at 9600 baud, switching to 38400"); |
| 52 | + myGPS.setSerialRate(38400); |
| 53 | + delay(100); |
| 54 | + } else { |
| 55 | + //gps.factoryReset(); |
| 56 | + delay(2000); //Wait a bit before trying again to limit the Serial output |
| 57 | + } |
| 58 | + } while(1); |
| 59 | + Serial.println("GPS serial connected"); |
| 60 | + |
| 61 | + myGPS.setUART1Output(COM_TYPE_UBX); //Set the UART port to output UBX only |
| 62 | + myGPS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise) |
| 63 | + myGPS.saveConfiguration(); //Save the current settings to flash and BBR |
| 64 | +} |
| 65 | + |
| 66 | +void loop() |
| 67 | +{ |
| 68 | + //Query module only every second. Doing it more often will just cause I2C traffic. |
| 69 | + //The module only responds when a new position is available |
| 70 | + if (millis() - lastTime > 1000) |
| 71 | + { |
| 72 | + lastTime = millis(); //Update the timer |
| 73 | + |
| 74 | + long latitude = myGPS.getLatitude(); |
| 75 | + Serial.print(F("Lat: ")); |
| 76 | + Serial.print(latitude); |
| 77 | + |
| 78 | + long longitude = myGPS.getLongitude(); |
| 79 | + Serial.print(F(" Long: ")); |
| 80 | + Serial.print(longitude); |
| 81 | + Serial.print(F(" (degrees * 10^-7)")); |
| 82 | + |
| 83 | + long altitude = myGPS.getAltitude(); |
| 84 | + Serial.print(F(" Alt: ")); |
| 85 | + Serial.print(altitude); |
| 86 | + Serial.print(F(" (mm)")); |
| 87 | + |
| 88 | + byte SIV = myGPS.getSIV(); |
| 89 | + Serial.print(F(" SIV: ")); |
| 90 | + Serial.print(SIV); |
| 91 | + |
| 92 | + Serial.println(); |
| 93 | + } |
| 94 | +} |
0 commit comments