In the last ESP8266 project, I built a simple real time clock using a real time clock chip (DS3231 and DS1307), I used a 16×2 LCD to display time and date and 2 push buttons to set them.
Now, in this topic I’m going to make an internet clock based on the ESP8266 WiFi module (ESP-01), this clock gets time information from the internet and therefore there is no need for a real time clock chip such as DS3231, DS1307, DS1302 …
In this project time and date will be sent to Arduino IDE serial monitor and will be displayed on 16×2 LCD screen.
Before I start, if you want to see how to use the ESP-01 module for the first time, how to use it with Arduino IDE and how to program (upload sketches) it with Arduino, visit the following topic:
ESP8266 WiFi module programming with Arduino UNO board
And the page below shows how to interface the ESP-01 module with I2C LCD:
Interfacing ESP8266 ESP-01 module with I2C LCD
Also, the page below shows how to build a real time clock with ESP8266 module, DS3231 (or DS1307) and I2C LCD:
ESP8266 ESP-01 Real time clock with DS3231/DS1307
Components List:
- ESP8266 ESP-01 module
- 16×2 LCD screen
- PCF8574 I/O expander (or PCF8574A) — PCF8574 datasheet
- AMS1117 3V3 voltage regulator
- 10uF capacitor
- 0.1uF (100nF) ceramic capacitor
- 5 x 10k ohm resistor
- 330 ohm resistor
- 10k ohm variable resistor or potentiometer
- 5V source
- Breadboard
- Jumper wires
The Circuit:
Circuit schematic diagram is shown below.
(All grounded terminals are connected together)
The SDA and SCL lines of the I2C bus come from GPIO0 and GPIO2 of the ESP-01 (respectively), they are connected to PCF8574 SDA pin (#15) and SCL pin (#14).
The Code:
There are 3 libraries we’ve to add to Arduino IDE before we compile the code, the first one is for the I2C LCD which can be downloaded from the links below:
LiquidCrystal_I2C Library — direct link
The 2nd library is NTPClient, this library connects the ESP8266 WiFi to a time server, this server sends time information to the module. NTP: Network Time Protocol.
NTPClient Library — direct link
The last one is Arduino Time library, this library converts Unix timestamp (Unix epoch) into: seconds, minutes, hours, day of the week, day, month and year. The Unix epoch is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT).
Basically the time server sends time in Unix epoch format which needs to be converted, this library does all the job. Download links are below:
Arduino Time Library — direct link
The NTPClient library is configured to get time information (Unix epoch) from the server time.nist.gov (GMT time) and an offset of 1 hour ( ==> GMT + 1 time zone) which is equal to 3600 seconds, configuration line is below:
NTPClient timeClient(ntpUDP, “time.nist.gov”, 3600, 60000);
The full code is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | // ESP8266 ESP-01 Internet real time clock #include <ESP8266WiFi.h> #include <WiFiUdp.h> #include <NTPClient.h> // Include NTPClient library #include <TimeLib.h> // Include Arduino time library #include <LiquidCrystal_I2C.h> // Include LiquidCrystal_I2C library LiquidCrystal_I2C lcd(0x27, 16, 2); // Configure LiquidCrystal_I2C library with 0x27 address, 16 columns and 2 rows const char *ssid = "YOUR SSID"; const char *password = "YOUR WIFI PASSWORD"; WiFiUDP ntpUDP; // 'time.nist.gov' is used (default server) with +1 hour offset (3600 seconds) 60 seconds (60000 milliseconds) update interval NTPClient timeClient(ntpUDP, "time.nist.gov", 3600, 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(0, 2); // Initialize I2C LCD module (SDA = GPIO0, SCL = GPIO2) lcd.backlight(); // Turn backlight ON 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; // Send time and date to serial monitor Serial.println(Time); Serial.println(Date); // Display time and date on the 16x2 LCD lcd.setCursor(0, 0); lcd.print(Time); lcd.setCursor(0, 1); lcd.print(Date); last_second = second_; } delay(200); } |
Small Video:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
Such a simple device, only tells the time, no speaker no pictures no bloatware. Why is there no such thing readily available in the market?
how do you convert it to 12hr format? and add am and pm
hour%12 will give you 12 hour clock. You can use PM flag for pm. For example if (hour>12) pmFlag=true;
hi, i have a problem with the hours, if i change the Time[6] with the %12+48 the second digit change like a 12 hours format, the only problem is the firt digit Time[5] not match with the 12 hours format, do you need to change someting else in the Time[5] variable?
‘POSITIVE’ was not declared in this scope