Skip to content

Instantly share code, notes, and snippets.

@KornWtp
Created November 8, 2018 07:45
Show Gist options
  • Save KornWtp/928007fabb48a7beca036c848d68ade6 to your computer and use it in GitHub Desktop.
Save KornWtp/928007fabb48a7beca036c848d68ade6 to your computer and use it in GitHub Desktop.
esp32+128x64 lcd+gps_v2.io
#include <Arduino.h>
#include <U8g2lib.h>
#include <TinyGPS++.h>
#include <HardwareSerial.h>
#include <TimeLib.h>
#define TXPin (16)
#define RXPin (17)
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
const int offset = 7; //Time Zone
void getPosition();
float latitude , longitude;
int Year;
byte Hour , Minute , Second , Month , Day ;
U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=*/ 18, /* MOSI=*/ 23, /* MISO=*/ 19);
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;
// The serial connection to the GPS device
HardwareSerial ss(2);
void setup(void) {
Serial.begin(115200);
u8g2.begin();
ss.begin(GPSBaud);
}
void loop(void) {
getPosition();
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(40, 10, "Position");
u8g2.drawStr(0, 20, "Latitude : ");
u8g2.setCursor(60, 20);
u8g2.print(latitude, 6);
u8g2.drawStr(0, 30, "Longitude : ");
u8g2.setCursor(70, 30);
u8g2.print(longitude, 6);
u8g2.drawStr(0, 40, "Date : ");
u8g2.setCursor(40, 40);
u8g2.print(Day);
u8g2.print("/");
u8g2.print(Month);
u8g2.print("/");
u8g2.print(Year);
u8g2.drawStr(0, 50, "Time :");
u8g2.setCursor(45, 50);
u8g2.print(Hour);
u8g2.print(":");
u8g2.print(Minute);
u8g2.print(":");
u8g2.print(Second);
u8g2.sendBuffer(); // transfer internal memory to the display
//delay(1000);
}
void getPosition() {
while (ss.available() > 0) {
if (gps.encode(ss.read())) {
Serial.print(F("Location: "));
if (gps.location.isValid()) {
latitude = gps.location.lat(), 6;
longitude = gps.location.lng(), 6;
Serial.print(gps.location.lat(), 6);
Serial.print(F(","));
Serial.print(gps.location.lng(), 6);
}
else {
Serial.print(F("INVALID"));
}
Serial.print(F(" Date/Time: "));
if (gps.date.isValid()) {
Year = gps.date.year();
Month = gps.date.month();
Day = gps.date.day();
Serial.print(gps.date.year());
Serial.print(F("/"));
Serial.print(gps.date.month());
Serial.print(F("/"));
Serial.print(gps.date.day());
}
else {
Serial.print(F("INVALID"));
}
Serial.print(F(" "));
if (gps.time.isValid()) {
Hour = gps.time.hour();
Minute = gps.time.minute();
Second = gps.time.second();
setTime(Hour, Minute, Second, Day, Month, Year);
// Calc current Time Zone time by offset value
adjustTime(7 * SECS_PER_HOUR);
if (hour() < 10) Serial.print(F("0"));
Hour = hour();
Serial.print(hour());
Serial.print(F(":"));
if (minute() < 10) Serial.print(F("0"));
Minute = minute();
Serial.print(minute());
Serial.print(F(":"));
if (second() < 10) Serial.print(F("0"));
Second = second();
Serial.print(second());
}
else {
Serial.print(F("INVALID"));
}
Serial.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment