gps_logger_to_serial_csv.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <SoftwareSerial.h>
  2. #include <TinyGPS.h>
  3. TinyGPS gps;
  4. SoftwareSerial ss(3, 2);
  5. unsigned long pattern = 0x00000000; //0000 speed, 0000 error
  6. static void smartdelay(unsigned long ms);
  7. static void print_float(float val, float invalid, int len, int prec);
  8. static void print_int(unsigned long val, unsigned long invalid, int len);
  9. static void print_date(TinyGPS &gps);
  10. static void print_str(const char *str, int len);
  11. long readVcc();
  12. void setup()
  13. {
  14. Serial.begin(115200);
  15. pinMode(13, OUTPUT);
  16. ss.begin(9600);
  17. }
  18. float flat, flon;
  19. unsigned long age, date, time, chars = 0, chars2 = 0;
  20. unsigned short sentences = 0, sentences2 = 0, failed = 0;
  21. void loop()
  22. {
  23. gps.stats(&chars, &sentences, &failed);
  24. if(gps.satellites() != TinyGPS::GPS_INVALID_SATELLITES && sentences != sentences2) {
  25. gps.f_get_position(&flat, &flon, &age);
  26. Serial.print(",");
  27. print_date(gps);
  28. Serial.print(",");
  29. Serial.print(flat,6);
  30. Serial.print(",");
  31. Serial.print(flon,6);
  32. Serial.print(",");
  33. Serial.print(gps.f_altitude(),1);
  34. Serial.print(",");
  35. Serial.println(gps.hdop());
  36. }
  37. if(chars == chars2) pattern = 0xffff;
  38. else if(sentences == sentences2) pattern = chars % 2;
  39. else if(readVcc()<4000) pattern = 0;
  40. else pattern = (gps.speed() << 16) + gps.satellites()-4;
  41. chars2 = chars;
  42. sentences2 = sentences;
  43. smartdelay(1000);
  44. }
  45. long readVcc() {
  46. long result; // Read 1.1V reference against AVcc
  47. ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  48. delay(2); // Wait for Vref to settle
  49. ADCSRA |= _BV(ADSC); // Convert
  50. while (bit_is_set(ADCSRA,ADSC));
  51. result = ADCL;
  52. result |= ADCH<<8;
  53. result = 1126400L / result; // Back-calculate AVcc in mV
  54. return result;
  55. }
  56. static void smartdelay(unsigned long ms)
  57. {
  58. unsigned long start = millis();
  59. do
  60. {
  61. digitalWrite(13, (pattern >> ((millis() % 1000) * 32 / 1000)) & 0x1 );
  62. while (ss.available())
  63. gps.encode(ss.read());
  64. } while (millis() - start < ms);
  65. }
  66. static void print_float(float val, float invalid, int len, int prec)
  67. {
  68. if (val == invalid)
  69. {
  70. while (len-- > 1)
  71. Serial.print('*');
  72. Serial.print(' ');
  73. }
  74. else
  75. {
  76. Serial.print(val, prec);
  77. int vi = abs((int)val);
  78. int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  79. flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  80. for (int i=flen; i<len; ++i)
  81. Serial.print(' ');
  82. }
  83. smartdelay(0);
  84. }
  85. static void print_int(unsigned long val, unsigned long invalid, int len)
  86. {
  87. char sz[32];
  88. if (val == invalid)
  89. strcpy(sz, "*******");
  90. else
  91. sprintf(sz, "%ld", val);
  92. sz[len] = 0;
  93. for (int i=strlen(sz); i<len; ++i)
  94. sz[i] = ' ';
  95. if (len > 0)
  96. sz[len-1] = ' ';
  97. Serial.print(sz);
  98. smartdelay(0);
  99. }
  100. static void print_date(TinyGPS &gps)
  101. {
  102. int year;
  103. byte month, day, hour, minute, second, hundredths;
  104. unsigned long age;
  105. gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  106. if (age == TinyGPS::GPS_INVALID_AGE)
  107. Serial.print("********** ******** ");
  108. else
  109. {
  110. char sz[32];
  111. sprintf(sz, "%02d-%02d-%02d %02d:%02d:%02d",
  112. year, month, day, hour, minute, second);
  113. Serial.print(sz);
  114. }
  115. //print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  116. smartdelay(0);
  117. }
  118. static void print_str(const char *str, int len)
  119. {
  120. int slen = strlen(str);
  121. for (int i=0; i<len; ++i)
  122. Serial.print(i<slen ? str[i] : ' ');
  123. smartdelay(0);
  124. }