ESP32 Tutorial 53/55 - Build an LCD Internet Clock | SunFounder's ESP32 IoT Learnig kit
Related or required files and link to this lesson
TZ.h file (required )In this video we learn how to Network Time Protocol to build LCD clock which gets updated via the Wifi. Full code and wiring is explaiend.
Topics in this lesson
Use Chapters from timeline or click on the time
- 00:00 Introduction
- 2:10 Introduction
- 5:15 Wiring explained
- 7:32 Arduino code explained
- 18:43 Selecting ESP32 board and COM port in Arduino IDE
- 20:27 Internet Clock Demonstration
/* Internet Clock for ESP32 Watch video instruction for this code on YouTube https://youtu.be/0KnuNqfiVug Resource page for this lesson and code: http://robojax.com/course3 Tutoril by https://youTube.com/@robojax Written by Ahamd Shasmhiri on Dec 31, 2023 at 19:29 www.Robojax.com */ #include#include //SDA->21,SCL->22 LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display const bool showSeconds = true;//set to "true" to show seconds and "false" to hide const bool showShortMonth = false;//December = false, Dec=true const bool show24Hours = true;//true to have 18:30 or false to have 6:30 #include #include "time.h" #include "sntp.h" #include "TZ.h" //TZ.h is taken from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h const char* ssid = "dars"; const char* password = "llllllllllllll"; const char* ntpServer1 = "pool.ntp.org"; const char* ntpServer2 = "time.nist.gov"; const long gmtOffset_sec = -5 * 3600; const int daylightOffset_sec = 3600; const String months[][2] ={ "January","Jan", "February","Feb", "March","Mar", "April","Apr", "May","May", "June","Jun", "July","Jul", "August","Aug", "September","Sep", "October","Oct", "November","Nov", "December","Dec" }; void printLocalTime() { struct tm timeinfo; if(!getLocalTime(&timeinfo)){ Serial.println("No time available (yet)"); return; } //Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S 12Hours: %I:%M:%S"); // get time format HH:MM char timeHour[5]; strftime(timeHour,5, "%H", &timeinfo); String hourString = String(timeHour);//convert timeHour to string char timeMinute[3]; strftime(timeMinute,3, "%M", &timeinfo); String minuteString = String(timeMinute); //Serial.println(hourString); if(!show24Hours) { if(hourString.toInt() >= 12) { minuteString = minuteString + "PM"; }else{ minuteString = minuteString + "AM"; } } char timeSeconds[3]; strftime(timeSeconds,3, "%S", &timeinfo); char timeDayofWeek[10]; strftime(timeDayofWeek, 10 , "%A", &timeinfo); char timeMonth[10]; strftime(timeMonth, 10 , "%B", &timeinfo); String Month = String(timeMonth); if(showShortMonth) { for(int m=0; m <12; m++) { if(Month == months[m][0] ) { Month = months[m][1]; //Serial.println(months[m][1]); } } } char timeDayofMonth[3]; strftime(timeDayofMonth,3 , "%d", &timeinfo); char timeYear[5]; strftime(timeYear, 5 , "%Y", &timeinfo); String time = String(timeHour) + ":" + minuteString ; if(showSeconds) time = time +":" + String(timeSeconds);//add seconds if showSeconds=true lcd.clear(); lcd.setCursor(0, 0); lcd.print(time); lcd.print(" "); lcd.print(String(timeDayofWeek)); // lcd.setCursor(0, 1); lcd.print(timeDayofMonth); lcd.print(" "); lcd.print(Month); lcd.print(" "); lcd.print(timeYear); } // Callback function (get's called when time adjusts via NTP) void timeavailable(struct timeval *t) { Serial.println("Got time adjustment from NTP!"); printLocalTime(); } void setup() { Serial.begin(115200); lcd.init();// initialize the lcd lcd.backlight(); // Turns on the LCD backlight. lcd.print("Internet Clock"); delay(3000);//waif 3 seconds // set notification call-back function sntp_set_time_sync_notification_cb( timeavailable ); /** * NTP server address could be aquired via DHCP, * * NOTE: This call should be made BEFORE esp32 aquires IP address via DHCP, * otherwise SNTP option 42 would be rejected by default. * NOTE: configTime() function call if made AFTER DHCP-client run * will OVERRIDE aquired NTP server address */ sntp_servermode_dhcp(1); // (optional) /** * This will set configured ntp servers and constant TimeZone/daylightOffset * should be OK if your time zone does not need to adjust daylightOffset twice a year, * in such a case time adjustment won't be handled automagicaly. */ // configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2); /** * A more convenient approach to handle TimeZones with daylightOffset * would be to specify a environmnet variable with TimeZone definition including daylight adjustmnet rules. * A list of rules for your zone could be obtained from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h */ //configTzTime(TZ_America_Toronto, ntpServer1, ntpServer2); configTzTime(TZ_America_Toronto, ntpServer1, ntpServer2); //connect to WiFi Serial.printf("Connecting to %s ", ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(" CONNECTED"); } void loop() { delay(5000); printLocalTime(); // it will take some time to sync time :) }