#include <WiFi.h>
#include <WifiUDP.h>
#include <String.h>
#include <Wire.h>
#include <NTPClient.h>
#include <Time.h>
#include <TimeLib.h>
#include <Timezone.h>
#include <U8g2lib.h>
#include <Wire.h>
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0);
// Define NTP properties
#define NTP_OFFSET 60 * 60 // In seconds
#define NTP_INTERVAL 60 * 1000 // In miliseconds
#define NTP_ADDRESS "ca.pool.ntp.org" // change this to whatever pool is closest (see ntp.org)
// Set up the NTP UDP client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
const char* ssid = "Wokwi-GUEST"; // insert your own ssid
const char* password = ""; // and password
String date;
String t;
const char * days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} ;
const char * months[] = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"} ;
const char * ampm[] = {"AM", "PM"} ;
void setup ()
{
Serial.begin(115200); // most ESP-01's use 115200 but this could vary
timeClient.begin(); // Start the NTP UDP client
u8g2.begin();
// Connect to wifi
Serial.println("");
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi at ");
Serial.print(WiFi.localIP());
Serial.println("");
delay(1000);
}
void loop()
{
if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
{
date = ""; // clear the variables
t = "";
// update the NTP client and get the UNIX UTC timestamp
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
// convert received time stamp to time_t object
time_t local, utc;
utc = epochTime;
// Then convert the UTC UNIX timestamp to local time
TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, -300}; //UTC - 5 hours - change this as needed
TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, -360}; //UTC - 6 hours - change this as needed
Timezone usEastern(usEDT, usEST);
local = usEastern.toLocal(utc);
// now format the Time variables into strings with proper names for month, day etc
date += days[weekday(local)-1];
date += ", ";
date += months[month(local)-1];
date += " ";
date += day(local)+1;
date += ", ";
date += year(local);
// format the time to 12-hour format with AM/PM and no seconds
t += hourFormat12(local-1)-1;
t += ":";
if(minute(local) < 10) // add a zero if minute is under 10
t += "0";
t += minute(local);
t += " ";
if (ampm[isPM(local)] == "PM"){
t += "AM";
}
if (ampm[isPM(local)] == "AM"){
t += "PM";
}
// Display the date and time
Serial.println(t);
// print the date and time on the OLED
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_logisoso24_tr); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.setCursor(10, 29);
u8g2.print(t);
u8g2.sendBuffer();
}
else // attempt to connect to wifi again if disconnected
{
}
delay(10000); //Send a request to update every 10 sec (= 10,000 ms)
}