#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// LCD Setup
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
// NTP Setup
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 8 // UTC+8
#define UTC_OFFSET_DST 0 // No DST
// Simulated GPS data
const char* simulatedGPSLatitude = "22.3964 N"; // Simulated Latitude
const char* simulatedGPSLongitude = "114.1095 E"; // Simulated Longitude
int simulatedGPSHour = 12; // Simulated GPS time: Hour
int simulatedGPSMinute = 34; // Simulated GPS time: Minute
int simulatedGPSSecond = 56; // Simulated GPS time: Second
// Modes
enum Mode { WIFI_MODE, GPS_MODE };
Mode currentMode = WIFI_MODE; // Start in WiFi mode
unsigned long lastSwitchTime = 0; // Time of the last mode switch
const unsigned long switchInterval = 10000; // Switch every 10 seconds
void connectToWiFi() {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Connecting WiFi...");
WiFi.begin("Wokwi-GUEST", ""); // Replace with your WiFi credentials
while (WiFi.status() != WL_CONNECTED) {
delay(500);
LCD.print(".");
}
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("WiFi Connected");
delay(1000);
}
void printWiFiTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.print("Time Error");
return;
}
// Display time
LCD.setCursor(0, 0);
LCD.printf("%02d:%02d:%02d", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
// Display mode
LCD.setCursor(0, 1);
LCD.print("WiFi Mode");
}
void printSimulatedGPSTime() {
// Display simulated GPS time
LCD.setCursor(0, 0);
LCD.printf("%02d:%02d:%02d", simulatedGPSHour, simulatedGPSMinute, simulatedGPSSecond);
// Display simulated GPS coordinates
static bool showLat = true;
LCD.setCursor(0, 1);
if (showLat) {
LCD.printf("Lat: %s", simulatedGPSLatitude);
} else {
LCD.printf("Lon: %s", simulatedGPSLongitude);
}
showLat = !showLat; // Toggle between latitude and longitude
}
void setup() {
Serial.begin(115200);
LCD.init();
LCD.backlight();
// Connect to WiFi
connectToWiFi();
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER); // Configure NTP
LCD.clear();
}
void loop() {
// Automatically switch modes every 10 seconds
if (millis() - lastSwitchTime > switchInterval) {
currentMode = (currentMode == WIFI_MODE) ? GPS_MODE : WIFI_MODE; // Toggle mode
lastSwitchTime = millis();
LCD.clear();
}
// Update display based on the current mode
if (currentMode == WIFI_MODE) {
printWiFiTime();
} else if (currentMode == GPS_MODE) {
printSimulatedGPSTime();
}
delay(1000); // Update every second
}