Hardware Connection
- OLED_SCL → 14
- OLED_SDA → 02
- GP-02-Kit_RX → 04
- GP-02-Kit_TX → 05
Code Explanation
#include <TinyGPSPlus.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
// I2C Connection Instructions
// NodeMCU Development Board — 0.96" OLED Pin Mapping
// GND → GND
// 3V3 → VCC
// SCL → D1 (GPIO 5)
// SDA → D2 (GPIO 4)
#define SCREEN_WIDTH 128 // OLED display width
#define SCREEN_HEIGHT 64 // OLED display height
#define OLED_SDA 02 // SDA pin, GPIO2 (D4)
#define OLED_SCL 14 // SCL pin, GPIO14 (D5)
#define OLED_RESET 13 // Reset pin
#define SCREEN_ADDRESS 0x3C // OLED display I2C address
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// GPS module pin definitions
#define RXPin 4 // GPIO12 (NodeMCU D6)
#define TXPin 5 // GPIO14 (NodeMCU D5)
SoftwareSerial ss(RXPin, TXPin);
TinyGPSPlus gps;
int Year, Month, Date, Hour, Minute, Second, Yea, Mon, Dat, Hou;
double Lat, Lng;
String sMonth, sDate, sHour, sMinute, sSecond;
void setup() {
Wire.begin(OLED_SDA, OLED_SCL);
Serial.begin(9600);
WiFi.mode(WIFI_OFF); // Disable WiFi to save power
WiFi.forceSleepBegin();
ss.begin(9600); // Initialize virtual serial port for GPS
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextColor(WHITE);
display.display();
}
void loop() {
boolean newData = false;
for (unsigned long start = millis(); millis() - start < 300;) {
while (ss.available()) {
if (gps.encode(ss.read())) {
newData = true;
}
}
}
Yea = gps.date.year();
Mon = gps.date.month();
Dat = gps.date.day();
Hou = gps.time.hour();
Minute = gps.time.minute();
Second = gps.time.second();
Lng = gps.location.lng();
Lat = gps.location.lat();
// Convert UTC time to Beijing time and correct errors
Hour = Hou + 8;
if (Hour >= 24) {
Hour -= 24;
}
if (Hou + 8 >= 24) {
Date = Dat + 1;
if ((Mon == 1 || Mon == 3 || Mon == 5 || Mon == 7 || Mon == 8 || Mon == 10 || Mon == 12) && (Date > 31)) {
Date -= 30;
Month = Mon + 1;
} else if ((Mon == 4 || Mon == 6 || Mon == 9 || Mon == 11) && (Date > 30)) {
Date -= 29;
Month = Mon + 1;
} else if ((Yea % 4 == 0) && (Date > 29)) {
Date -= 28;
Month = Mon + 1;
} else if ((Yea % 4 != 0) && (Date > 28)) {
Date -= 27;
Month = Mon + 1;
} else {
Month = Mon;
Year = Yea;
}
if (Month > 12) {
Month -= 12;
Year = Yea + 1;
}
} else {
Date = Dat;
Month = Mon;
Year = Yea;
}
// Display the results
display.setTextColor(SSD1306_WHITE);
display.setCursor(38, 0);
display.setTextSize(1);
display.print(Year);
display.setCursor(63, 0);
display.print("-");
display.setCursor(71, 0);
sMonth = formatNumber(Month, 2);
display.print(sMonth);
display.setCursor(83, 0);
display.print("-");
display.setCursor(91, 0);
sDate = formatNumber(Date, 2);
display.print(sDate);
display.setTextSize(2);
display.setCursor(26, 13);
sHour = formatNumber(Hour, 2);
display.print(sHour);
display.setCursor(46, 13);
display.print(":");
display.setCursor(56, 13);
sMinute...
Read more »