#include <NTPClient.h>
#include <WiFi.h> // for ESP32
#include <WiFiUdp.h>
#include "Wire.h"
#include <LCD_I2C.h>

const char *ssid     = "Wokwi-GUEST";
const char *password = "";

WiFiUDP ntpUDP;
LCD_I2C lcd(0x27, 16, 2);

// initialized to a time offset of +8 hours
NTPClient timeClient(ntpUDP,"pool.ntp.org", 3600*8, 60000);
//                           HH:MM:SS
// timeClient initializes to 10:00:00 if it does not receive an NTP packet
// before the 100ms timeout.


// blue LED on ESP-12F
const int led = 2;
const int hour = 10;
const int minute = 0;
int sec, sec_old;
byte led_state;
uint32_t t0;
  byte error, address;
  int nDevices = 0;
void setup(){
  Serial.begin(115200);
  Wire.begin();
  lcd.begin();
  lcd.backlight();
  pinMode(led, OUTPUT);
  // led is off when pin is high
  digitalWrite(led, 1);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay (500);
    Serial.print (".");
  }

  timeClient.begin();
  Serial.println();
  Serial.println("Started!");
}

void loop() {
  timeClient.update();
  if (t0 < millis()){
    t0 += millis()+1000;
    lcd.setCursor(0,0);
    lcd.println(timeClient.getFormattedTime());
    sec = timeClient.getSeconds();
  }

  
  if (sec_old != sec){
    if(timeClient.isTimeSet()) {  
      if (sec % 2 == 1)  
        digitalWrite(led, digitalRead(led)^1);     
      }
  }
  sec_old = sec;
}