// Install from the Library Manager: // "LiquidCrystal I2C" // Board: "ESP32 Dev Module" #include #include #include #include // Your Wi-Fi network's name and password const char* WIFI_NAME = "Network_Name"; const char* WIFI_PASSWORD = "Password"; // Turkey time: UTC+3, no summer time const char* TIME_ZONE = "<+03>-3"; // The server that gives the time const char* NTP_SERVER = "pool.ntp.org"; // The date on screen: day/month/year const char* DATE_FORMAT = "%d/%m/%Y"; // The days, starting on Sunday const char* DAYS[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // The ESP32's I2C pins const int SDA_PIN = 21; const int SCL_PIN = 22; // Ask about the password after this (ms) const unsigned long WIFI_HINT_AFTER = 15000; // I2C address: 0x27 on most, 0x3F on some LiquidCrystal_I2C lcd(0x27, 16, 2); // The second last written on screen int shownSecond = -1; void setup() { Serial.begin(115200); Wire.begin(SDA_PIN, SCL_PIN); lcd.init(); lcd.backlight(); lcd.print("Waiting for WiFi"); Serial.print("Connecting to "); Serial.println(WIFI_NAME); WiFi.begin(WIFI_NAME, WIFI_PASSWORD); unsigned long start = millis(); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); if (millis() - start > WIFI_HINT_AFTER) { // No luck: check name, password lcd.setCursor(0, 1); lcd.print("Name/password?"); } } Serial.println(); Serial.print("Connected, IP: "); Serial.println(WiFi.localIP()); // The time comes from the net once, // then the board keeps it itself configTzTime(TIME_ZONE, NTP_SERVER); lcd.clear(); lcd.print("Getting time..."); } void loop() { struct tm now; // No time yet: waits 1 s for it if (!getLocalTime(&now, 1000)) { Serial.println("No time yet"); return; } // Same second: nothing new to write if (now.tm_sec == shownSecond) { delay(50); return; } bool first = shownSecond < 0; shownSecond = now.tm_sec; char dateText[11]; strftime(dateText, sizeof dateText, DATE_FORMAT, &now); char timeText[9]; strftime(timeText, sizeof timeText, "%H:%M:%S", &now); // Line 1: the date and the day lcd.setCursor(0, 0); lcd.print(dateText); lcd.print(" "); lcd.print(DAYS[now.tm_wday]); // Line 2: the time, in the middle lcd.setCursor(4, 1); lcd.print(timeText); // To the Serial Monitor: the first // time, then once a minute if (first || now.tm_sec == 0) { Serial.print(dateText); Serial.print(" "); Serial.print(DAYS[now.tm_wday]); Serial.print(" "); Serial.println(timeText); } }