Skip to content

Instantly share code, notes, and snippets.

@mcspx
Forked from nazt/gps.hpp
Created January 5, 2018 17:45
Show Gist options
  • Select an option

  • Save mcspx/fbe9946b2147ea25b4098b45e8222f27 to your computer and use it in GitHub Desktop.

Select an option

Save mcspx/fbe9946b2147ea25b4098b45e8222f27 to your computer and use it in GitHub Desktop.
gps.hpp
#include <Arduino.h>
#include "gnss.h"
GNSS gps;
float GPS_SEARCH_TIMEOUT_S = 40;
extern uint8_t LED;
// volatile char GNSS_data[58] = "";
char GNSS_data[58] = "";
String gps_data = "";
String gps_lat = "";
String gps_lon = "";
String gps_alt = "";
uint32_t gpsCounter = 0;
bool gotGPSLocation = true;
void convertGPSRawDataToLatLng() {
if (gps_data.substring(0, 6) == "$GPGGA") {
gps_data.toCharArray(GNSS_data, 58);
String gps_cal_s = gps_data.substring(18, 27);
float gps_cal_f = gps_cal_s.toFloat();
gps_cal_f /= 60.0f;
uint32_t gps_cal_l = gps_cal_f * 10000000;
gps_lat += GNSS_data[16] ;
gps_lat += GNSS_data[17] ;
gps_lat += ".";
gps_lat += gps_cal_l;
gps_lat += GNSS_data[28] ;
gps_cal_s = gps_data.substring(33, 42);
gps_cal_f = gps_cal_s.toFloat();
gps_cal_f /= 60.0f;
gps_cal_l = gps_cal_f * 10000000;
gps_lon += GNSS_data[30] ;
gps_lon += GNSS_data[31] ;
gps_lon += GNSS_data[32] ;
gps_lon += ".";
gps_lon += gps_cal_l;
gps_lon += GNSS_data[43] ;
gps_alt += GNSS_data[54];
gps_alt += GNSS_data[55];
gps_alt += GNSS_data[56];
// gps_alt += GNSS_data[57];
// gps_alt += GNSS_data[58];
Serial.println(F("Stop GPS"));
gps.Stop();
gps.DisableNMEA();
delay(1000);
}
}
void startGPSService() {
gps.Start();
gps.EnableNMEA();
gps_data = gps.GetNMEA("GGA");
gpsCounter = 0;
// assume got gps unless searching timeout.
uint32_t gpsTimeoutNextTick = millis() + GPS_SEARCH_TIMEOUT_S*1000L;
Serial.print("GPS TIMEOUT NEXTICK = ");
Serial.println(gpsTimeoutNextTick);
while ((gps_data.substring(0, 8) == "$GPGGA,," ||
gps_data.substring(0, 8) == "Please W")) {
gps_data = gps.GetNMEA("GGA");
gpsCounter += 1;
if (++gpsCounter%10 == 0) {
Serial.print(millis());
Serial.println(" Waiting GPS...");
}
// gps timeout
if (millis() > gpsTimeoutNextTick) {
Serial.println("GPS Timeout..");
gps_lat = "0.0";
gps_lon = "0.0";
gps_alt = "0.0";
gotGPSLocation = false;
break;
}
}
// action after finish GPS searching...
if (gotGPSLocation) {
Serial.println("FOUND GPS....");
Serial.println("COVERTING to Coordinate....");
convertGPSRawDataToLatLng();
/*
// cache GPS Information
// Serial.println("update GPS cache...");
// Serial.print(gpsValue.lat);
// Serial.print(F(" "));
// Serial.print(gpsValue.lng);
// Serial.print(F(" "));
// Serial.println(gps_alt);
*/
}
else {
Serial.println("GPS TIMEOUT..");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment