#include #include #include #include #include #include // Pin definitions #define LCD_RS 23 #define LCD_EN 19 #define LCD_D4 18 #define LCD_D5 5 #define LCD_D6 4 #define LCD_D7 0 // Time zone offset (example: UTC-5 for EST) #define TIME_OFFSET 0 // LCD setup (2 rows, 40 characters wide) LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7); // WiFi credentials const char* ssid = "yourSSID"; const char* password = "yourPASSWORD"; // NTP client setup WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "pool.ntp.org", TIME_OFFSET * 3600, 86400000); // Update every 24 hours // Text URL to fetch for row 2 const String textFileURL = "http://ben.lostgeek.net/fclock.txt"; // Variables for fetched text String currentText = ""; void setup() { // Start serial for debugging Serial.begin(115200); // Initialize LCD lcd.begin(40, 2); // 40 characters, 2 rows lcd.clear(); // Connect to WiFi connectToWiFi(); // Start NTP client to get the time timeClient.begin(); // Set time based on NTP sync timeClient.update(); setTime(timeClient.getEpochTime()); // Set up LCD display (row 1: date, row 2: text) displayDateAndTime(); fetchText(); displayText(); } void loop() { // Update time every second updateTime(); // Every 5 minutes, fetch the new text static unsigned long lastFetchTime = 0; if (millis() - lastFetchTime >= 300000) { // 5 minutes fetchText(); displayText(); lastFetchTime = millis(); } delay(1000); // Delay 1 second to update time } void connectToWiFi() { Serial.println("Connecting to WiFi..."); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Attempting to connect..."); } // Get IP address after successful connection String ipAddress = WiFi.localIP().toString(); // Display the connection status for 2 seconds lcd.clear(); lcd.setCursor(0, 0); lcd.print("Connected to"); lcd.setCursor(0, 1); lcd.print(ssid); lcd.setCursor(0, 2); lcd.print("IP: " + ipAddress); delay(2000); // Show the message for 2 seconds Serial.println("Connected to WiFi!"); } void updateTime() { timeClient.update(); setTime(timeClient.getEpochTime()); // Update internal time displayDateAndTime(); } void displayDateAndTime() { // Get date and time information String formattedDate = getFormattedDate(); // Pad time with leading zeros if needed String formattedTime = String(hour() < 10 ? "0" : "") + String(hour()) + ":" + String(minute() < 10 ? "0" : "") + String(minute()) + ":" + String(second() < 10 ? "0" : "") + String(second()) + " " + (hour() >= 12 ? "PM" : "AM"); // Clear row 1 and display formatted date lcd.setCursor(0, 0); lcd.print(" "); // Clear row 1 (40 chars) lcd.setCursor(0, 0); lcd.print(formattedDate); // Display time at position 38 lcd.setCursor(28, 0); lcd.print(formattedTime); // Display time starting from position 38 } String getFormattedDate() { int dayOfWeek = weekday(); String dayNames[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; String dayName = dayNames[dayOfWeek - 1]; int dayOfMonth = day(); String suffix = getDaySuffix(dayOfMonth); String monthNames[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; String monthName = monthNames[month() - 1]; return dayName + ", " + monthName + " " + String(dayOfMonth) + suffix + " "; } String getDaySuffix(int day) { if (day >= 11 && day <= 13) return "th"; switch (day % 10) { case 1: return "st"; case 2: return "nd"; case 3: return "rd"; default: return "th"; } } void fetchText() { HTTPClient http; http.begin(textFileURL); int httpCode = http.GET(); if (httpCode == HTTP_CODE_OK) { currentText = http.getString(); if (currentText.length() > 40) { currentText = currentText.substring(0, 40); // Truncate if longer than 40 characters } } else { currentText = "Error fetching text"; // Default text in case of error } http.end(); } void displayText() { // Clear row 2 and display fetched text lcd.setCursor(0, 1); lcd.print(" "); // Clear row 2 (40 chars) lcd.setCursor(0, 1); lcd.print(currentText); // Display fetched text }