Please watch this VDO First
Hook up I2C LCD
I2C LCD
Connect
LCD >>> Arduino
GND >>> GND
VCC >>> 5Vcc
SDA >>> A4
SCL >>> A5
Code
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include<SoftwareSerial.h>
SoftwareSerial GPSModule(11,10); // RX,TX)
int updates;
int failedUpdates;
int pos;
int stringplace = 0;
String timeUp;
String nmea[15];
String labels[12] {"Time: ", "Status: ", "Latitude: ", "Hemisphere: ", "Longitude: ", "Hemisphere: ", "Speed: ", "Track Angle: ", "Date: "};
void setup() {
Serial.begin(57600);
GPSModule.begin(9600);
//LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("A-arduino GPS");
lcd.setCursor(0,1);
lcd.print(nmea[8]);
lcd.setCursor(9,1);
lcd.print(nmea[0]);
delay(3000);
}
void loop() {
Serial.flush();
GPSModule.flush();
while (GPSModule.available() > 0)
{
GPSModule.read();
}
if (GPSModule.find("$GPRMC,")) {
String tempMsg = GPSModule.readStringUntil('\n');
for (int i = 0; i < tempMsg.length(); i++) {
if (tempMsg.substring(i, i + 1) == ",") {
nmea[pos] = tempMsg.substring(stringplace, i);
stringplace = i + 1;
pos++;
}
if (i == tempMsg.length() - 1) {
nmea[pos] = tempMsg.substring(stringplace, i);
}
}
updates++;
nmea[2] = ConvertLat();
nmea[4] = ConvertLng();
for (int i = 0; i < 9; i++) {
Serial.print(labels[i]);
Serial.print(nmea[i]);
Serial.println("");
lcd.setCursor(0,0);
lcd.print("lat :");
lcd.setCursor(5,0);
lcd.print(nmea[2]);
lcd.setCursor(0,1);
lcd.print("long:");
lcd.setCursor(5,1);
lcd.print(nmea[4]);
delay(1000);
}
}
else {
failedUpdates++;
}
stringplace = 0;
pos = 0;
}
String ConvertLat() {
String posneg = "";
if (nmea[3] == "S") {
posneg = "-";
}
String latfirst;
float latsecond;
for (int i = 0; i < nmea[2].length(); i++) {
if (nmea[2].substring(i, i + 1) == ".") {
latfirst = nmea[2].substring(0, i - 2);
latsecond = nmea[2].substring(i - 2).toFloat();
}
}
latsecond = latsecond / 60;
String CalcLat = "";
char charVal[9];
dtostrf(latsecond, 4, 6, charVal);
for (int i = 0; i < sizeof(charVal); i++)
{
CalcLat += charVal[i];
}
latfirst += CalcLat.substring(1);
latfirst = posneg += latfirst;
return latfirst;
}
String ConvertLng() {
String posneg = "";
if (nmea[5] == "W") {
posneg = "-";
}
String lngfirst;
float lngsecond;
for (int i = 0; i < nmea[4].length(); i++) {
if (nmea[4].substring(i, i + 1) == ".") {
lngfirst = nmea[4].substring(0, i - 2);
//Serial.println(lngfirst);
lngsecond = nmea[4].substring(i - 2).toFloat();
//Serial.println(lngsecond);
}
}
lngsecond = lngsecond / 60;
String CalcLng = "";
char charVal[9];
dtostrf(lngsecond, 4, 6, charVal);
for (int i = 0; i < sizeof(charVal); i++)
{
CalcLng += charVal[i];
}
lngfirst += CalcLng.substring(1);
lngfirst = posneg += lngfirst;
return lngfirst;
}
Hello
ตอบลบdoesn't it require any GPS Library ?