Clock without RTC Module using ESP32 & NTP Server

Modern Gaming Cover YouTube Channel Art (3)
This content provide you Experience

By utilizing the ESP32’s built-in Wi-Fi module to link to an NTP server on the internet to obtain the current time, a clock without an RTC (Real-Time Clock) module can be constructed using ESP32 and an NTP (Network Time Protocol) server.

To create a time without an RTC module using an ESP32 and an NTP server, follow these steps:

1.Setup the Hardware

Components Required –

  1. Esp32
  2. OLED display

Setup Environment for ESP32 in Arduino IDE

Click on this link – https://elconics.com/getting-started-withesp32/

Install some libraries-

Goto- tools>> manage libraries

Search for – Adafruit GFX and install it .

Another library called – Adafruit_SSD1306

Code

// This code is for Wifi based Time having Wifi configuration fetures on phone
#include <WiFi.h>
#include "time.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 19800; // 19800 represent time differance of 5 hour 30 min in sec 19800
const int   daylightOffset_sec = 3600;
struct tm timeinfo;
void printLocalTime()
{
  
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}
#include <WiFiManager.h>
void setup() {
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("OLED display initialization failed");
    while (true);
  }
    WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
    // it is a good practice to make sure your code sets wifi mode how you want it.
 
    // put your setup code here, to run once:
    Serial.begin(115200);
    
    //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wm;
 
    // reset settings - wipe stored credentials for testing
    // these are stored by the esp library
    //wm.resetSettings();
 
    // Automatically connect using saved credentials,
    // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
    // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
    // then goes into a blocking loop awaiting configuration and will return success result
 
    bool res;
    // res = wm.autoConnect(); // auto generated AP name from chipid
    res = wm.autoConnect("Suraj"); // anonymous ap
  //  res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
 
    if(!res) {
        Serial.println("Failed to connect");
        // ESP.restart();
    } 
    else {
        //if you get here you have connected to the WiFi    
        Serial.println("connected...yeey :)");
    }
    //connect to WiFi
 // Serial.printf("Connecting to %s ", ssid);
  WiFi.begin();
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" CONNECTED");
  
  //init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}
 
void loop() {
    // put your main code here, to run repeatedly:   

      delay(1000);
  printLocalTime();
  

  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
 // display.println(&timeinfo,%A);
 display.println(&timeinfo, "%A");
 display.setTextSize(1);
 display.setCursor(0,20);
 display.println(&timeinfo, " %B %d %Y");
  display.setTextSize(2);
 display.setCursor(0,40);
 display.println(&timeinfo, "%H:%M:%S");
  display.display();
}




  
 





Here is the tutorial video –

Leave a Comment

Your email address will not be published. Required fields are marked *

Related post