Accurate Timekeeping on ESP32 using an NTP Server




Below is the code used in the video to have ESP32 request time from an NTP server and update the internal RTC clock.

// The Tinker Foundry --- https://youtu.be/c6r9kECS6RQ
// ESP32 Code for testing RTC - real-time clock updated from an NTP (network time protocol) server over the Internet
// Use NTP to get Coordinated Universal Time (UTC) and adjust to local time/date
// ESP32 will connect to an NTP server and send a request packet
// In response, the NTP server will send a time stamp packet
#include <WiFi.h> // The usual for ESP32...
#include "time.h" // ESP32 native time library to handle NTP server connectivity and synchronization
 
const char* ssid     = "********";   // input your wifi name
const char* password = "********";   // input your wifi passwords
 
// set up the NTP server name
const char* NTPServer = "time.google.com"; // for our example, let's use Google's public NTP - see developers.google.time for more information
 
// set GMT offset - this defines the offset between GMT and local time in seconds
// in order words, we apply an adjustment to suit our timezone
const long gmtOffset_sec = -18000; // For my timezone (Eastern), we are 5 hours behind GMT (so GMT - 5 hours * 3600 seconds in an hour = -18000 seconds)
// Look up time zone definitions at greenwichmeantime.com
 
// set Daylight offset - in places where daylight savings is applied, this defines offset in seconds, usually one hour
const int daylightOffset_sec = 3600; // 3600 seconds in an hour
 
// Setup function
void setup() {
  // --------------------------------------------------------------------------------------
  // Below section is the normal ESP32 stuff we do to set up the serial connection and WiFi
  // --------------------------------------------------------------------------------------
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  Serial.println();
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  Serial.print("Msg: Ready! Use 'http://");
  Serial.print(WiFi.localIP());
  Serial.println("' to connect");
  // --------------------------------------------------------------------------------------
 
  // Instruction message output over serial interface
  Serial.print("Configured NTP Server: ");
  Serial.println(NTPServer);
  Serial.println("Enter 'Update' to retrieve NTP Server time. Enter 'Time' to retrieve local clock time.");
 
}
 
// Main loop
void loop() {
 
  // Wait for serial input to return time
  while (Serial.available() == 0){}
  String testStr = Serial.readString();
  testStr.trim();
  if (testStr == "Time") // if serial input is word "Time", then return local clock through serial output
  {
    struct tm clockTime; // declare time information variable with structure tm:  
 
    if (!getLocalTime(&clockTime))
    {
      Serial.println("Failed to get time");
    } else
    {
      Serial.println(&clockTime, "%A, %B %d %Y %H:%M:%S"); // day of the week, month, day of month, year, hour:minute:second
    }
 
  } else if (testStr == "Update") // if serial input is word "Update", retrieve time from NTP server and print the time
  {
    // Configure the time with the constants we defined...
    configTime(gmtOffset_sec, daylightOffset_sec, NTPServer); // This will retrieve UTC time from NTP server and adjust to local
    Serial.println("Retrieved time from NTP server");
 
    struct tm clockTime; // declare time information variable with structure tm:  
 
    if (!getLocalTime(&clockTime))
    {
      Serial.println("Failed to get time");
    } else
    {
      Serial.println(&clockTime, "%A, %B %d %Y %H:%M:%S"); // day of the week, month, day of month, year, hour:minute:second
    }
 
  } else
  {
    Serial.println("Invalid input - try again");
  }
 
}


Comments

Popular posts from this blog

ESP32 Based Pulse-Oximeter using MAX30102

ESP32 Web Sockets - Transferring Image Raw Data

Heart Rate Measurement with ESP32 and Pulse Oximeter Module using FFT