ESP32 ntp client code


// esp32 dev module, disabled,disabled,Huge App (3mb No OTA/1MB SPIFFS), 
// 240Mhz(wfif/bt),OIO,80Mhz, 4Mb(32Mb),921600,core1,core1,none,disabled,
// disabled on com13
#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h> // Required for I2C communication
#include <WiFi.h>
#include <time.h>
#include "esp_sntp.h"

const char* ssid     = "@home";
const char* password = "xxxxxxxx";


const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 25200;  // 7 hours
const int   daylightOffset_sec = 0;
bool ntp_time=false;
int tm_hour;



// #define DEBUG_MODE

// Use the correct constructor for your display
// Example for an SH1107 I2C display (adjust as needed)
U8G2_SH1107_SEEED_128X128_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

// this script works also for  DOIT esp32 DEVKIT V1

//--------------------------------------
void printLocalTime(){
  struct tm timeinfo; // = { 0 };  //initializes all its members to zero
 
  if(!getLocalTime(&timeinfo)){
#ifdef DEBUG_MODE
   Serial.println("getLocalTime failed (return)");
#endif     
    return;
  }
  char timebuf[9], datebuf[15]; // 22:15:39  ,  Sun 28/12/2025 

  tm_hour = timeinfo.tm_hour;
  
  strftime(datebuf, sizeof(datebuf), "%a %d/%m/%Y", &timeinfo);
  strftime(timebuf, sizeof(timebuf), "%H:%M:%S", &timeinfo);
  
  u8g2.clearBuffer();
  u8g2.setFontDirection(0);   // horizontal 
  u8g2.setFont(u8g2_font_timB12_tr);
  u8g2.drawStr(10,20,datebuf);  
  u8g2.setFont(u8g2_font_timB24_tn);
  u8g2.drawStr(10,80,timebuf);
  u8g2.setFont(u8g2_font_5x7_tf);
  if (ntp_time)      u8g2.drawStr(110,  110, "NTP");
  if (tm_hour == 23) u8g2.drawStr(110,  110, "   ");
  u8g2.sendBuffer();
}
//---------------------------
void wifi_connect() {
  if (WiFi.status() == WL_CONNECTED) return;
  WiFi.mode(WIFI_STA);  //crucial to get the correct connection
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {      
#ifdef DEBUG_MODE
   Serial.println("Wifi not connected");
#endif 
    delay(500);
  }  
 
#ifdef DEBUG_MODE
   Serial.println("Wifi connected");
#endif  
}
//----------------------------
void wifi_disconnect() {
  if (WiFi.status() != WL_CONNECTED) return;
  WiFi.disconnect();
  while (WiFi.status() == WL_CONNECTED) {      
#ifdef DEBUG_MODE
   Serial.println("Wifi not disconnected");
#endif 
    delay(500);
  }
  WiFi.mode(WIFI_OFF);  
  
}

void cbSyncTime(struct timeval *tv)  { // callback function to show when NTP was synchronized
#ifdef DEBUG_MODE  
  Serial.println("NTP time synched");
#endif
  ntp_time=true;
}

//-------------------------------------
void setup(void) {
#ifdef DEBUG_MODE
   Serial.begin(115200);
   delay(500);
   Serial.println("Serial monitor started");
#endif 
  
  wifi_connect();
  u8g2.setBusClock(400000); //Set the clock frequency of I2C to 400Khz.
  u8g2.begin();
 // u8g2.setContrast(255);
  sntp_set_sync_interval(55 * 60 * 1000UL); // 55 min interval
  sntp_set_time_sync_notification_cb(cbSyncTime);  // Callback function for time sync notification
  delay(100);

    // Init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();
  wifi_disconnect(); 
  ntp_time=false;
}

//-----------------------------
void loop(void) {
  
  if (tm_hour == 0) {  // open WiFi at midnight
        if (!ntp_time) wifi_connect();  // and close wifi when ntp is updated    
        else  wifi_disconnect();  
        printLocalTime();
  } else {
      printLocalTime();
      ntp_time=false;
  }
} // loop