simple_test.ino 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <SoftwareSerial.h>
  2. #include <TinyGPS.h>
  3. /* This sample code demonstrates the normal use of a TinyGPS object.
  4. It requires the use of SoftwareSerial, and assumes that you have a
  5. 4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
  6. */
  7. TinyGPS gps;
  8. SoftwareSerial ss(4, 3);
  9. void setup()
  10. {
  11. Serial.begin(115200);
  12. ss.begin(4800);
  13. Serial.print("Simple TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  14. Serial.println("by Mikal Hart");
  15. Serial.println();
  16. }
  17. void loop()
  18. {
  19. bool newData = false;
  20. unsigned long chars;
  21. unsigned short sentences, failed;
  22. // For one second we parse GPS data and report some key values
  23. for (unsigned long start = millis(); millis() - start < 1000;)
  24. {
  25. while (ss.available())
  26. {
  27. char c = ss.read();
  28. // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
  29. if (gps.encode(c)) // Did a new valid sentence come in?
  30. newData = true;
  31. }
  32. }
  33. if (newData)
  34. {
  35. float flat, flon;
  36. unsigned long age;
  37. gps.f_get_position(&flat, &flon, &age);
  38. Serial.print("LAT=");
  39. Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
  40. Serial.print(" LON=");
  41. Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
  42. Serial.print(" SAT=");
  43. Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
  44. Serial.print(" PREC=");
  45. Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
  46. }
  47. gps.stats(&chars, &sentences, &failed);
  48. Serial.print(" CHARS=");
  49. Serial.print(chars);
  50. Serial.print(" SENTENCES=");
  51. Serial.print(sentences);
  52. Serial.print(" CSUM ERR=");
  53. Serial.println(failed);
  54. if (chars == 0)
  55. Serial.println("** No characters received from GPS: check wiring **");
  56. }