Advertisement
sibee86

GPS

Aug 22nd, 2016
3,348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SoftwareSerial.h>
  2. #include <TinyGPS.h>
  3.  
  4. TinyGPS gps;
  5. SoftwareSerial ss(4, 3);
  6. SoftwareSerial SIM800L(8, 7); // RX | TX
  7.  
  8. static void smartdelay(unsigned long ms);
  9. static void print_float(float val, float invalid, int len, int prec);
  10. static void print_int(unsigned long val, unsigned long invalid, int len);
  11. static void print_date(TinyGPS &gps);
  12. static void print_str(const char *str, int len);
  13.  
  14. void setup()
  15. {
  16.  
  17.   Serial.begin(115200);
  18.  
  19.   ss.begin(9600);
  20.    
  21. }
  22.  
  23. void loop()
  24. {
  25.   float flat, flon;
  26.   unsigned long age, date, time, chars = 0;
  27.   unsigned short sentences = 0, failed = 0;
  28.   static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  29.    
  30.   gps.f_get_position(&flat, &flon, &age);
  31.   print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
  32.   print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);
  33.   print_date(gps);
  34.  
  35.   Serial.println();
  36.  
  37.   smartdelay(1000);
  38. }
  39.  
  40. static void smartdelay(unsigned long ms)
  41. {
  42.   unsigned long start = millis();
  43.   do
  44.   {
  45.     while (ss.available())
  46.       gps.encode(ss.read());
  47.   } while (millis() - start < ms);
  48. }
  49.  
  50. static void print_float(float val, float invalid, int len, int prec)
  51. {
  52.  if (val == invalid)
  53.  {
  54.    while (len-- > 1)
  55.       Serial.print('*');
  56.     Serial.print(' ');
  57.   }
  58.   else
  59.   {
  60.     Serial.print(val, prec);
  61.     int vi = abs((int)val);
  62.     int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  63.    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  64.     for (int i=flen; i<len; ++i)
  65.      Serial.print(' ');
  66.  }
  67.  smartdelay(0);
  68. }
  69.  
  70. static void print_int(unsigned long val, unsigned long invalid, int len)
  71. {
  72.  char sz[32];
  73.  if (val == invalid)
  74.    strcpy(sz, "*******");
  75.  else
  76.    sprintf(sz, "%ld", val);
  77.  sz[len] = 0;
  78.  for (int i=strlen(sz); i<len; ++i)
  79.    sz[i] = ' ';
  80.  if (len > 0)
  81.     sz[len-1] = ' ';
  82.   Serial.print(sz);
  83.   smartdelay(0);
  84. }
  85.  
  86. static void print_date(TinyGPS &gps)
  87. {
  88.  int year;
  89.   byte month, day, hour, minute, second, hundredths;
  90.   unsigned long age;
  91.   gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  92.   if (age == TinyGPS::GPS_INVALID_AGE)
  93.     Serial.print("********** ******** ");
  94.   else
  95.   {
  96.     char sz[32];
  97.     sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
  98.         month, day, year, hour+7, minute, second);
  99.     Serial.print(sz);
  100.   }
  101.   print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  102.   smartdelay(0);
  103. }
  104.  
  105. static void print_str(const char *str, int len)
  106. {
  107.   int slen = strlen(str);
  108.   for (int i=0; i<len; ++i)
  109.    Serial.print(i<slen ? str[i] : ' ');
  110.  smartdelay(0);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement