【Arduino】GPS

IMG_20150912_152353

 

GPS: GMS-G9  ランニングエレクトロニクス

1:GND

9:TXD

12:VCC

 

Arduino Pro mini

9: RX(software serial)

#include <avr/sleep.h>
#include <avr/interrupt.h> 
#include <SoftwareSerial.h>
#include <TinyGPS.h>

#define __DEBUG__

TinyGPS gps;
SoftwareSerial ss(9, 8);

void setup()
{
  Serial.begin(38400);
  ss.begin(9600);  // GMS-G9 
  
  Serial.print("Hello!!!");
}

void loop()
{
  bool newData = false;
  unsigned long chars;
  unsigned short sentences, failed;

  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (ss.available())
    {
      char c = ss.read();
//      Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
    }
  }

  if (newData)
  {
    float flat, flon;
    unsigned long age;
    gps.f_get_position(&flat, &flon, &age);
    
    Serial.print("LAT=");
    Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
    Serial.print(" LON=");
    Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
    Serial.print(" SAT=");
    Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
    Serial.print(" PREC=");
    Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
  }
  
#ifdef __DEBUG__  
  gps.stats(&chars, &sentences, &failed);
  Serial.print(" CHARS=");
  Serial.print(chars);
  Serial.print(" SENTENCES=");
  Serial.print(sentences);
  Serial.print(" CSUM ERR=");
  Serial.println(failed);
#endif  
  if (chars == 0)
    Serial.println("** No characters received from GPS: check wiring **");
}

 

 

Leave a Comment.