ESP 8266 NTP LCD clock (internet time via ESP8266 NodeMcu and I2C LCD module)

Here is the simple example how to use NTP to display time on lcd using NodeMcu developement board.







Code:

// Core code is used from https://lastminuteengineers.com/esp8266-ntp-server-date-time-tutorial/
// and modified to be aplicable with I2C Lcd display
//

#include <NTPClient.h>     // https://github.com/arduino-libraries/NTPClient

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

// Default I2C pins on NodeMcu is    SDA - D2 , SCL - D1

const char *ssid     = "YOUR SSID";
const char *password = "YOUR PASSWORD";
/*
You need to adjust the UTC offset for your timezone in milliseconds. Refer the list of UTC time offsets.  Here are some examples for different timezones:
For UTC -5.00 : -5 * 60 * 60 : -18000
For UTC +1.00 : 1 * 60 * 60 : 3600
For UTC +0.00 : 0 * 60 * 60 : 0
const long utcOffsetInSeconds = 3600;
*/
const long utcOffsetInSeconds = 7200;  // set offset
char daysOfTheWeek[7][12] = {"Nedelja", "Ponedeljak", "Utorak", "Sreda", "Cetvrtak", "Petak", "Subota"};  // Serbian
//char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};  // English
// Define NTP Client to get time
/*
Area_____________________________________________________HostName
Worldwide_______________________________________________ pool.ntp.org
Asia____________________________________________________ asia.pool.ntp.org
Europe__________________________________________________ europe.pool.ntp.org
North America___________________________________________ north-america.pool.ntp.org
Oceania_________________________________________________ oceania.pool.ntp.org
South America___________________________________________ south-america.pool.ntp.org

*/

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
LiquidCrystal_I2C lcd(0x27, 16, 2);  // I2c address, columns lcd module, rows lcd module
void setup(){
  Serial.begin(115200);
// initialize the LCD,
   lcd.begin();
  // Turn on the backlight and print a message.
   lcd.backlight();
   lcd.clear();
   lcd.setCursor (0,0); //   column, row
   lcd.print("NTP TIME");
   delay(2000);

  WiFi.begin(ssid, password);


  while ( WiFi.status() != WL_CONNECTED ) {

    delay ( 500 );
    Serial.print ( "." );
  }

  timeClient.begin();

}

void loop() {

  timeClient.update();
  lcd.clear();
  lcd.setCursor (0,0); //   column, row
  lcd.print(daysOfTheWeek[timeClient.getDay()]);
  lcd.setCursor (0,1); //   column, row
  lcd.print(timeClient.getFormattedTime());
  delay(1000);
}


Download code and Library


Comments