Internet Clock: Display Date and Time With an OLED Using ESP8266 NodeMCU With NTP Protocol

Introduction: Internet Clock: Display Date and Time With an OLED Using ESP8266 NodeMCU With NTP Protocol

Hi guys in this instructables we will build a internet clock which will get time from internet so this project will not need any RTC to run , it will only need a working internet connection

And for this project you need a esp8266 which will have a wifi to get internet access and a display to display the time on it and esp8266 will fetch time from internet using NTP protocol, ntp stands for network time protocol, so basically there are ntp servers on the web which are used to synchronize computer clocks and we will using bthose servers to get time in our project.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Things You Need

for this project you will need following things :

Esp8266/nodemcu

Oled ssd1306 0.96"

Jumper wires

Breadboard

Usb cable

Step 2: Connections

This 4-pin OLED display communicates with ESP8266 module using I2C protocol, below are the circuit diagram and connections table to connect OLED I2C pins with NodeMCU to display Internet time.

Step 3: Download Libraries

Make sure you downloaded SD1306 libraries in Your Arduino ide as shown in image and make sure you Adafruit GFX library as well, if not then install these two libraries.

In your Arduino IDE go to Libraries manager and search for NTP and just download the NTP client library as i downloaded, refer image for further help.

Step 4: What Is NTP

NTP is one of the oldest networking Internet Protocol (IP) for synchronizing clocks between computer networks. It was designed by David L. Mills of the University of Delaware in 1981. This protocol can be used to synchronize many networks to Coordinated Universal Time (UTC) within few milliseconds. UTC is the primary time standard by which world regulates clock and time. UTC does not changes and vary for different geographical locations. NTP uses UTC as the time reference and provides accurate and synchronized time across the Internet.

NTP works on a hierarchical client-server model. Top model has reference clocks known as “stratum0” like atomic clocks, radio waves, GPS, GSM which receives time from the satellite. The servers which receive time from stratum0 are called as “stratum1” and servers which receive time from stratum1 are called “stratum2” and so on. This goes on and the accuracy of time goes on decreasing after each stage. NTP automatically selects the best of several available time sources to synchronize which makes it fault-tolerant able protocol. So here in this project, we are getting time from NTP server using ESP8266 NodeMCU and showing it on OLED display. This same kind of Internet clock is built by using ESP32 in previous tutorial.

Step 5: Coding Part

To request date and time, initialize time client with address of NTP servers. For better accuracy choose the address of NTP servers which are close to your geographical area. Here we use “pool.ntp.org” which gives servers from worldwide. If you wish to choose servers from Asia you can use “asia.pool.ntp.org”. timeClient also takes UTC time offset in milliseconds of your timezone. For instance, UTC offset for India is +5:30 so we convert this offset in milliseconds which is equal to 5*60*60+30*60 = 19800.

Area. UTC time offset(hours and minutes). UTC time offset(seconds)

INDIA +5:30 19800

LONDON 0:00. 0

NEW YORK -5:00 -18000

Please copy the following code & enter your wifi & password in the code & Enter the time offset in the code then upload it to your esp8266 boards. :

#include "NTPClient.h"
#include "ESP8266WiFi.h" // provides ESP8266 specific Wi-Fi routines we are calling to connect to network #include "WiFiUdp.h" //handles sending and receiving of UDP packages

#include "SPI.h" // SPI for interfacing OLED with NodeMCu

#include "Adafruit_GFX.h"

#include "Adafruit_SSD1306.h"

#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET -1

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

constchar *ssid = "yourwifissid";

const char *password = "yourwifipass";

WiFiUDP ntpUDP;

NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800,60000);

String arr_days[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

String date_time; // You can specify the time server pool and the offset (in seconds, can be // changed later with setTimeOffset() ). Additionaly you can specify the // update interval (in milliseconds, can be changed using setUpdateInterval() ).

void setup()

{

Serial.begin(115200);

WiFi.begin(ssid, password);

while ( WiFi.status() != WL_CONNECTED )

{

delay ( 500 );

Serial.print ( "." );

}

if(!display.begin(SSD1306_SWITCHCAPVCC,0x3C))

{

Serial.println(F("SSD1306 allocation failed"));

for(;;); // Don't proceed, loop forever

}

display.clearDisplay();

display.setTextSize(2); // Draw 2X-scale text

display.setTextColor(WHITE);

display.setCursor(5, 2);

display.println("WELCOME TO");

display.println(" instructables");

display.println(" Project");

display.display();

delay(3000);

timeClient.begin();

}

void loop()

{

timeClient.update();

display.clearDisplay();

Serial.println(timeClient.getFormattedTime());

display.setTextSize(2); // Draw 2X-scale text

display.setTextColor(BLUE);

display.setCursor(0, 2);

int hh = timeClient.getHours();

int mm = timeClient.getMinutes();

int ss = timeClient.getSeconds();

if(hh>12)

{

hh=hh-12;

display.print(hh);

display.print(":");

display.print(mm);

display.print(":");

display.print(ss);

display.println(" PM");

}

else

{

display.print(hh);

display.print(":");

display.print(mm);

display.print(":");

display.print(ss);

display.println(" AM");

}

int day = timeClient.getDay();

display.println("'"+arr_days[day]+"'");

date_time = timeClient.getFormattedDate();

int index_date = date_time.indexOf("T");

String date = date_time.substring(0, index_date);

Serial.println(date);

display.println(date);

display.display();

// Show initial text }

Step 6: Getting Date & Time

if you connected everything properly and Uploaded the code properly as well then you'll able to see your ntp clock running on oled display as mine in running in the oled display. Please refer the image for output.

Be the First to Share

    Recommendations

    • Magnets Challenge

      Magnets Challenge
    • Raspberry Pi Contest 2020

      Raspberry Pi Contest 2020
    • Wearables Contest

      Wearables Contest

    Discussions