How to get the Day and Time through the internet with the Nodemcu ESP8266

How to get Day and Time through the internet using Nodemcu ESP8266

Hello, welcome back. In this tutorial, we will learn how to get in day and time through the internet. It mainly uses the Nodemcu ESP8266 board and the Network Time Protocol (NTP) protocol. Also, no other components are required for this project. Therefore, you can try this project easily and on a low budget. In this tutorial, the day and time are displayed on the Serial Monitor, LCD, and Blynk app. Also, if you want to control these display sources, you can use the button in the Blynk app interface. If you do not have an LCD. You can ignore it.

What is the NTP?

The NTP is simply called Network Time Protocol. Also, this protocol synchronizes all networked devices to Coordinated Universal Time (UTC). The UTC is the primary time standard that determines clocks and times. Also, this NTP protocol transmits time information through the UTC system. Therefore, through this NTP protocol, the exact time relevant to the time zone of our country can be obtained.

The process of this project

When power up this Nodemcu board, It connects to the NTP server and the Blynk cloud. Also, the Nodemcu board uses the user diagram protocol (UDP) to connect to the NTP server. Then, the Nodemcu board then sends a request packet to the NTP server. After, the NTP server then sends a time stamps packet to the Nodemcu board. It contains various information and the Nodemcu board only gets the date and time. After, these are displaying on the serial monitor, LCD, and Blynk app. Also, we can enable or disable the day and time using the button in the Blynk app.

OK, let’s do this project step by step. The required components are given below.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect these components. For that, use the circuit diagram below.

How to get Day and Time through the internet using Nodemcu ESP8266

Step 3

Thirdly, download and install the Blynk app on your smartphone. You can download it using the play store or app store. Then, follow the steps below.

  • First, sign up for this app using your Gmail and click the “New Project” button.
How to get Day and Time through the internet using Nodemcu ESP8266
  • After, enter the project name as you like and select the board. Then, click the “Confirm” button.
How to get Day and Time through the internet using Nodemcu ESP8266
  • Now, you can see the project interface. Ok, let’s include the widgets. For that, click the “+” icon in the corner.
How to get Day and Time through the internet using Nodemcu ESP8266
  • First, include a button widget and click it. After, select the PIN as V1 and change it to the switch mode.
  • Second, include a LCD widget and click it. Then, change this LCD to the advance mode and select the PIN as V0.
  • Finally, customize these widgets as you like.
How to get Day and Time through the internet using Nodemcu ESP8266

Step 4

Ok, now let’s create the program for this project. It is as follows.

/*Internet clock with Nodemcu ESP8266
   https://srituhobby.com
*/

#include <Blynk.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

const char *ssid = "**********";
const char *password = "**********";
const char *auth = "*********************";

const long offsetTime = 19800;
String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
bool Button = 1;
WiFiUDP NTP;
NTPClient srituhobbyTime(NTP, "pool.ntp.org", offsetTime);
LiquidCrystal_I2C lcd(0x27, 16, 2);

WidgetLCD LCD(V0);

BLYNK_WRITE(V1) {
  Button = param.asInt();
}

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, password);
  srituhobbyTime.begin();
  lcd.init();
  lcd.backlight();

  Serial.print("Time is ready..");
  lcd.setCursor(0, 0);
  lcd.print("Time is ready..");
  LCD.print(0, 0, "Time is ready..");
  delay(3000);

  lcd.clear();
  LCD.clear();

}

void loop() {
  Blynk.run();
  Time();
}

void Time() {
  if (Button == 1) {
    srituhobbyTime.update();
    Serial.print("Today:");
    lcd.setCursor(0, 0);
    lcd.print("Today:");
    LCD.print(0, 0, "Today:");
    LCD.print(6, 0, days[srituhobbyTime.getDay()]);
    lcd.print(days[srituhobbyTime.getDay()]);
    Serial.println(days[srituhobbyTime.getDay()]);

    Serial.print("Time:");
    lcd.setCursor(0, 1);
    lcd.print("Time:");
    LCD.print(0, 1, "Time:");
    lcd.print(srituhobbyTime.getFormattedTime());
    LCD.print(5, 1, srituhobbyTime.getFormattedTime());
    Serial.println(srituhobbyTime.getFormattedTime());
    delay(800);

  } else if (Button == 0) {
    lcd.clear();
    LCD.clear();
  }
}

Code explanation

Firstly, library files are included.

#include <Blynk.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

Secondly, include your WIFI connection details and Blynk token.

const char *ssid = "**********";
const char *password = "**********";
const char *auth = "*********************";

Thirdly, enter your offset time in second. For that, get the time zone at your location. My time zone is (GMT+5:30). After, calculate it as follows.

  • offset time = time zone * 60*60
  • offset time = 5.5 * 60*60
  • offset time = 19800
const long offsetTime = 19800;

After, the array is created for the days.

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

Next, objects are created for these libraries.

WiFiUDP NTP;
NTPClient srituhobbyTime(NTP, "pool.ntp.org", offsetTime);
LiquidCrystal_I2C lcd(0x27, 16, 2);

These codes send and receive the data from the Blynk app.

WidgetLCD LCD(V0);

BLYNK_WRITE(V1) {
  Button = param.asInt();
}

In the setup function,

void setup() {
//The serial monitor begins
  Serial.begin(115200);
//The Blink Library begins
  Blynk.begin(auth, ssid, password);
//The NTP server begins
  srituhobbyTime.begin();
//The LCD initialized
  lcd.init();
  lcd.backlight();

  Serial.print("Time is ready..");
  lcd.setCursor(0, 0);
  lcd.print("Time is ready..");
  LCD.print(0, 0, "Time is ready..");
  delay(3000);
//LCD clear
  lcd.clear();
  LCD.clear();

}

In the loop function, the main functions are called.

void loop() {
  Blynk.run();
  Time();
}

This is the main function of this project.

void Time() {
//Button value is checked using the IF condition
  if (Button == 1) {
//Update the time
    srituhobbyTime.update();
//Prints the day on the serial monitor, LCD and Blynk app
    Serial.print("Today:");
    lcd.setCursor(0, 0);
    lcd.print("Today:");
    LCD.print(0, 0, "Today:");
    LCD.print(6, 0, days[srituhobbyTime.getDay()]);
    lcd.print(days[srituhobbyTime.getDay()]);
    Serial.println(days[srituhobbyTime.getDay()]);
//Prints the time on the serial monitor, LCD and Blynk app
    Serial.print("Time:");
    lcd.setCursor(0, 1);
    lcd.print("Time:");
    LCD.print(0, 1, "Time:");
    lcd.print(srituhobbyTime.getFormattedTime());
    LCD.print(5, 1, srituhobbyTime.getFormattedTime());
    Serial.println(srituhobbyTime.getFormattedTime());
    delay(800);

  } else if (Button == 0) {
    lcd.clear();
    LCD.clear();
  }
}

Step 5

Now, select the board and the port. After, upload this code to the Nodemcu board.

Now, you can see the day and time on the serial monitor and the LCD. Next, run the Blynk app interface. Now, we can see the day and time in it and enable or disable the time using the button.

Ok, enjoy this project. The full video guide has given below. So, we will meet in the next tutorial.

How to get the Day and Time through the internet with the Nodemcu ESP8266

Leave a Reply

Your email address will not be published. Required fields are marked *