#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define rxGPS 12
#define txGPS 13
#define GPS_UPDATE_INTERVAL 2000
long lat, lon;
SoftwareSerial gpsSerial(rxGPS, txGPS);
TinyGPSPlus gps;
LiquidCrystal_I2C lcd(0x3F, 20, 4); // I2C 주소 0x27, 16x2 LCD 설정
unsigned long lastUpdate = 0;
void setup()
{
Serial.begin(9600); // connect serial
gpsSerial.begin(9600); // connect gps sensor
lcd.begin(); // LCD 초기화
lcd.backlight(); // 백라이트 켜기
lcd.print("GPS Initializing...");
Serial.println("GPS Initializing...");
delay(5000); // GPS 모듈 초기화 시간 연장 (5초)
}
void loop()
{
while (gpsSerial.available())
{
char c = gpsSerial.read();
//Serial.write(c); // GPS 모듈에서 읽은 원본 데이터 출력
gps.encode(c); // encode gps data
}
unsigned long currentMillis = millis();
if (currentMillis - lastUpdate >= GPS_UPDATE_INTERVAL)
{
lastUpdate = currentMillis;
Serial.print("SATS: ");
Serial.println(gps.satellites.value());
Serial.print("LAT: ");
Serial.println(gps.location.lat(), 6);
Serial.print("LONG: ");
Serial.println(gps.location.lng(), 6);
Serial.print("ALT: ");
Serial.println(gps.altitude.meters());
Serial.print("SPEED: ");
Serial.println(gps.speed.mps());
// LCD에 위치 정보 출력
lcd.clear();
lcd.setCursor(0, 0); // 첫 번째 줄
lcd.print("LAT: ");
lcd.print(gps.location.lat(), 6); // 소수점 이하 제거
lcd.setCursor(0, 1); // 첫 번째 줄
lcd.print("LNG: ");
lcd.print(gps.location.lng(), 6); // 소수점 이하 제거
lcd.setCursor(0, 2);
lcd.print("SPEED: ");
float speedKmh = gps.speed.mps() * 3.6;
lcd.print(speedKmh, 2);
lcd.print("Km");
lcd.setCursor(0, 3);
lcd.print("D:");
lcd.print(gps.date.year());
lcd.print("/");
lcd.print(gps.date.month());
lcd.print("/");
lcd.print(gps.date.day());
lcd.setCursor(12, 3);
lcd.print("T:");
lcd.print(gps.time.hour()+9);
lcd.print(":");
lcd.print(gps.time.minute());
if (gps.date.isValid()) {
Serial.print("Date: ");
Serial.print(gps.date.day());
Serial.print("/");
Serial.print(gps.date.month());
Serial.print("/");
Serial.println(gps.date.year());
} else {
Serial.println("Date: ** Invalid **");
}
if (gps.time.isValid()) {
Serial.print("Hour: ");
Serial.print(gps.time.hour()+9);
Serial.print(":");
Serial.print(gps.time.minute());
Serial.print(":");
Serial.print(gps.time.second());
} else {
Serial.println("Time: ** Invalid **");
}
Serial.println("---------------------------");
}
}