// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <TM1637Display.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>               // Include NTPClient library
#include <TimeLib.h>                 // Include Arduino time library
#include <Wire.h>
#include "RTClib.h"

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "time.nist.gov", 28800, 60000);

RTC_DS1307 rtc;

#define CLK  5 // The ESP32 pin GIOP22 connected to CLK
#define DIO  18 // The ESP32 pin GIOP23 connected to DIO

TM1637Display display = TM1637Display(CLK, DIO);

char daysOfTheWeek[7][12] = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

int minute_, hour_, day_, month_, year_;

void setup () {
  Serial.begin(9600);

  // if (! rtc.begin()) {
  //   Serial.println("Couldn't find RTC");
  //   while (1);
  // }
  // if (! rtc.isrunning()) {
  //   Serial.println("RTC is NOT running!");
  //   // following line sets the RTC to the date & time this sketch was compiled
  //   // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  //   // This line sets the RTC with an explicit date & time, for example to set
  //   // January 21, 2014 at 3am you would call:
  //   rtc.adjust(DateTime(2016, 11, 19, 19, 45, 0));   // <----------------------SET TIME AND DATE: YYYY,MM,DD,HH,MM,SS
  // }

  display.clear();
  display.setBrightness(7); // set the brightness to 7 (0:dimmest, 7:brightest)

  timeClient.begin();

  timeClient.update();
  unsigned long unix_epoch = timeClient.getEpochTime();    // Get Unix epoch time from the NTP server

  minute_ = minute(unix_epoch);
  hour_   = hour(unix_epoch);
  day_    = day(unix_epoch);
  month_  = month(unix_epoch);
  year_   = year(unix_epoch);

  delay(2000);
  delay(2000);

  rtc.begin();

  rtc.adjust(DateTime(year_, month_, day_, hour_, minute_));

  delay(100);
}

void loop () {
    DateTime now = rtc.now();
    // Serial.print(now.day(), DEC);
    // Serial.print('/');
    // Serial.print(now.month(), DEC);
    // Serial.print('/');
    // Serial.print(now.year(), DEC);
    // Serial.print(" (");
    // Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    // Serial.print(") ");
    // Serial.print(now.hour(), DEC);
    // Serial.print(':');
    // Serial.print(now.minute(), DEC);
    // Serial.print(':');
    // Serial.print(now.second(), DEC);
    // Serial.println();
    
    // delay(3000); //Print date and time every 3 sec

  // int wkt = (hour_*100) + minute_;
  int wkt = (now.hour() * 100) + (now.minute());
  // displayed letters: dOnE

  display.showNumberDecEx(wkt,0b11100000, false, 4, 0);
  delay(2000);
  display.clear();
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
GND5VSDASCLSQWRTCDS1307+
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW
4-Digit Display
sevseg1:CLK
sevseg1:DIO
sevseg1:VCC
sevseg1:GND