#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 lcd(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const char *ssid = "ZTE_2.4G_fCeGuw"; /*Replace with your network SSID*/
const char *password = "w3mnw4eu"; /*Replace with network password*/
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "time.nist.gov", 18000, 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);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
lcd.setCursor(0, 0); /*Set cursor*/
lcd.print("Time"); /*print time on LCD*/
lcd.setCursor(0, 1); /*Set LCD cursor*/
lcd.print(Date); /*Print date*/
WiFi.begin(ssid, password); /*begin WiFi*/
Serial.print("Connecting.");
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
Serial.println("connected");
timeClient.begin();
delay(1000);
lcd.clear(); /*clear LCD display*/
}
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); /*Prints time on serial monitor*/
Serial.println(Date); /*Print date on serial monitor*/
lcd.setCursor(0, 0); /*Set LCD cursor*/
lcd.print(Time); /*display time on LCD*/
lcd.setCursor(0, 1); /*Set LCD cursor*/
lcd.print(Date); /*Display date on LCD*/
last_second = second_;
}
delay(200);
}