#include <WiFi.h>
#include <FastLED.h>
#include <time.h>
#include "clock_display.h"

const long gmtOffsetInSeconds = -18000;
const char *NTP_POOL = "pool.ntp.org";
// local timezone (go to https://remotemonitoringsystems.ca/time-zone-abbreviations.php to figure out your one)
const char *LOCAL_TZ = "CST6CDT";

// UNIX timestamp representing current time
time_t currentTime = 0;
// structure representing date and time
tm timeinfo;

bool loopflag = false;
int ctr = 0;

void setup() {
  Serial.begin(115200);
  initDisplay();
  WiFi.begin("Wokwi-GUEST", "", 6);

  Serial.print("Connecting to ");
  Serial.print("Wokwi-GUEST");
  uint8_t counter = 0;
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(200);

    // give up after 20s
    if (++counter == 100)
      ESP.restart();
  }
  Serial.println("\nConnected!");

  counter = 0;
  setenv("TZ", LOCAL_TZ, 1);
  configTime(gmtOffsetInSeconds, 0, NTP_POOL);
  Serial.print("Synchronising time with ");
  Serial.print(NTP_POOL);
  do {
    time(&currentTime);
    localtime_r(&currentTime, &timeinfo);
    Serial.print(".");
    delay(50);

    // give up after 5s
    if (++counter == 100)
      ESP.restart();

    // if year is anything else than 1970 the time should be synchronised
  } while (timeinfo.tm_year < 10);
}

void loop() {
  if(!loopflag){
    Serial.println(" ");
    Serial.println("starting loop function.");
    loopflag = true;
  }
  // update current time
  time(&currentTime);
  // convert UTC UNIX timestamp to timeinfo in local timezone
  localtime_r(&currentTime, &timeinfo);
  ctr++;
  if(ctr > 100){
    Serial.print("h: ");
    Serial.print(timeinfo.tm_hour);
    Serial.print(" | m: ");
    Serial.print(timeinfo.tm_min);
    Serial.print(" | s: ");
    Serial.println(timeinfo.tm_sec);
    ctr = 0;
  }
  displayTime(timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
}
Loading
esp32-devkit-v1