GpsInterface.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "GpsInterface.h"
  2. #ifdef HAS_GPS
  3. char nmeaBuffer[100];
  4. MicroNMEA nmea(nmeaBuffer, sizeof(nmeaBuffer));
  5. HardwareSerial Serial2(GPS_SERIAL_INDEX);
  6. void GpsInterface::begin() {
  7. Serial2.begin(9600, SERIAL_8N1, GPS_TX, GPS_RX);
  8. MicroNMEA::sendSentence(Serial2, "$PSTMSETPAR,1201,0x00000042");
  9. MicroNMEA::sendSentence(Serial2, "$PSTMSAVEPAR");
  10. MicroNMEA::sendSentence(Serial2, "$PSTMSRR");
  11. delay(4000);
  12. if (Serial2.available()) {
  13. Serial.println("GPS Attached Successfully");
  14. this->gps_enabled = true;
  15. while (Serial2.available())
  16. Serial2.read();
  17. }
  18. }
  19. // Thanks JosephHewitt
  20. String GpsInterface::dt_string_from_gps(){
  21. //Return a datetime String using GPS data only.
  22. String datetime = "";
  23. if (nmea.isValid() && nmea.getYear() > 0){
  24. datetime += nmea.getYear();
  25. datetime += "-";
  26. datetime += nmea.getMonth();
  27. datetime += "-";
  28. datetime += nmea.getDay();
  29. datetime += " ";
  30. datetime += nmea.getHour();
  31. datetime += ":";
  32. datetime += nmea.getMinute();
  33. datetime += ":";
  34. datetime += nmea.getSecond();
  35. }
  36. return datetime;
  37. }
  38. void GpsInterface::setGPSInfo() {
  39. this->good_fix = nmea.isValid();
  40. this->num_sats = nmea.getNumSatellites();
  41. this->datetime = this->dt_string_from_gps();
  42. this->lat = String((float)nmea.getLatitude()/1000000, 7);
  43. this->lon = String((float)nmea.getLongitude()/1000000, 7);
  44. long alt = 0;
  45. if (!nmea.getAltitude(alt)){
  46. alt = 0;
  47. }
  48. this->altf = (float)alt / 1000;
  49. this->accuracy = 2.5 * ((float)nmea.getHDOP()/10);
  50. //nmea.clear();
  51. }
  52. float GpsInterface::getAccuracy() {
  53. return this->accuracy;
  54. }
  55. String GpsInterface::getLat() {
  56. return this->lat;
  57. }
  58. String GpsInterface::getLon() {
  59. return this->lon;
  60. }
  61. float GpsInterface::getAlt() {
  62. return this->altf;
  63. }
  64. String GpsInterface::getDatetime() {
  65. return this->datetime;
  66. }
  67. String GpsInterface::getNumSatsString() {
  68. return (String)num_sats;
  69. }
  70. bool GpsInterface::getFixStatus() {
  71. return this->good_fix;
  72. }
  73. String GpsInterface::getFixStatusAsString() {
  74. if (this->getFixStatus())
  75. return "Yes";
  76. else
  77. return "No";
  78. }
  79. bool GpsInterface::getGpsModuleStatus() {
  80. return this->gps_enabled;
  81. }
  82. void GpsInterface::main() {
  83. while (Serial2.available()) {
  84. //Fetch the character one by one
  85. char c = Serial2.read();
  86. //Serial.print(c);
  87. //Pass the character to the library
  88. nmea.process(c);
  89. }
  90. uint8_t num_sat = nmea.getNumSatellites();
  91. if ((nmea.isValid()) && (num_sat > 0))
  92. this->setGPSInfo();
  93. else if ((!nmea.isValid()) && (num_sat <= 0)) {
  94. this->setGPSInfo();
  95. }
  96. }
  97. #endif