Now let us interface a 16×2 LCD Display with NodeMCU ESP8266 board. Then we will connect the ESP8266 to the wifi network and fetch the date and time. Hence, a simple internet clock using ESP8266 can be made.

The connection between 16X2 LCD and ESP8266 is fairly simple. These are the connections between NodeMCU & LCD Display.
- RS pin of LCD — D6 pin of NodeMCU
- EN pin of LCD — D5 pin of NodeMCU
- D4 pin of LCD — D1 pin of NodeMCU
- D5 pin of LCD — D2 pin of NodeMCU
- D6 pin of LCD — D3 pin of NodeMCU
- D7 pin of LCD — D4 pin of NodeMCU
Similarly connect pin numbers 1, 5, and 16 of LCD to GND & Pin numbers 2, and 15 to 5V VCC. A 10K Potentiometer should be used at pin number 3 of LCD to adjust the contrast. 5V DC is enough to operate this device.
Libraries Requirement
To make an Internet Clock Using NodeMCU ESP8266 and 16×2 LCD without RTC Module, we need few libraries:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h>
#include <LiquidCrystal.h> First, we need the NTPClient Library. This library connects the ESP8266 WiFi to a time server, the server sends time information to the module. However, for sending and receiving UDP messages WiFiUdp.h library is used.
Then we have a Time library that converts a Unix timestamp (Unix epoch) into seconds, minutes, hours, day of the week, day, month and year.
The LCD Library is used to interface 16×2 LCD with ESP8266 Board.
Source Code/Program for ESP8266 Internet Clock
Here is a source code for getting NTP Time from Server. Before uploading the code to the NodeMCU ESP8266 Board, you need to make changes in the line below to match your time zone.
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 20700, 60000);Currently, I am staying in Nepal. Nepal is 5 hours and 45 minutes ahead of Coordinated Universal Time (UTC Time). So I converted +5 hr 45 Mins to Seconds.
+5 hr 45 Mins = 5x60x60 + 45×60 = 20700
So change this Timing according to your time Zone and Country in order to get correct time.
Also in the below code, make changes to the Wifi SSID & Password.
char* ssid = "Alsan Air WiFi 4"; //wifi ssid
char* password = "11122235122@kap1"; //wifi passwordFinal Code/sketch
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(D6, D5, D1, D2, D3, D4);
char* ssid = "Alsan Air WiFi 4"; //wifi ssid
char* password = "11122235122@kap1"; //wifi password
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 20700, 60000);
char Time[ ] = "TIME:00:00:00";
char Date[ ] = "DATE:00/00/2000";
byte last_second, second_, minute_, hour_, day_, month_;
int year_;
void setup() {
Serial.begin(115200);
lcd.begin(16, 2); // Initialize 16x2 LCD Display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(Time);
lcd.setCursor(0, 1);
lcd.print(Date);
WiFi.begin(ssid, password);
Serial.print("Connecting.");
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
Serial.println("connected");
timeClient.begin();
}
void loop() {
timeClient.update();
unsigned long unix_epoch = timeClient.getEpochTime(); // Get Unix epoch time from the NTP server
second_ = second(unix_epoch);
if (last_second != second_) {
minute_ = minute(unix_epoch);
hour_ = hour(unix_epoch);
day_ = day(unix_epoch);
month_ = month(unix_epoch);
year_ = year(unix_epoch);
Time[12] = second_ % 10 + 48;
Time[11] = second_ / 10 + 48;
Time[9] = minute_ % 10 + 48;
Time[8] = minute_ / 10 + 48;
Time[6] = hour_ % 10 + 48;
Time[5] = hour_ / 10 + 48;
Date[5] = day_ / 10 + 48;
Date[6] = day_ % 10 + 48;
Date[8] = month_ / 10 + 48;
Date[9] = month_ % 10 + 48;
Date[13] = (year_ / 10) % 10 + 48;
Date[14] = year_ % 10 % 10 + 48;
Serial.println(Time);
Serial.println(Date);
lcd.setCursor(0, 0);
lcd.print(Time);
lcd.setCursor(0, 1);
lcd.print(Date);
last_second = second_;
}
delay(500);
}
شنطة اسعافات
Trả lờiXóa