Example\SimpleTime.ino help

good day to all in this forum,
I am running the example\SimpleTime.ino on my ESP32 and I would like to keep my local time automatically updated by correcting for the GMT offset and for the daylight saving time.
I did comment the following lines:
// const long gmtOffset_sec = 3600;
// const int daylightOffset_sec = 3600;
//configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);
I un_commented the folowing line:
configTzTime(time_zone, ntpServer1, ntpServer2);
I have the following questions:

did I do it correctly ?
esp_sntp_servermode_dhcp(1); // (optional) >> Is this line to be un_commented ?
How often the update from the server take place ? How to set the update interval please ?
Thanks in advance for the assistance.
regards,

Please, format your code.

No!
Starting with Arduino: My Recommended Approach

It seems you might not be fully ready to start the project just yet, but that’s perfectly okay! We don't know your skill set or what resources you have at your disposal, so I recommend starting with some online tutorials. While the quality varies, many tutorials are excellent and will help you build a solid foundation.

Here’s How to Get Started:

  1. Learn the Basics: Begin with the fundamentals, such as controlling outputs (like LEDs) and interpreting inputs (such as reading a switch or receiving a message). LEDs are a great starting point since they’re inexpensive and most Arduino boards come with a built-in LED.
  2. Explore Basic Electronics Tutorials: Look for beginner-friendly tutorials on electronics concepts. These will help you understand key principles that you’ll need as you progress.
  3. Recommended Resources:
  • Arduino Cookbook: Consider going through the Arduino Cookbook, which is a comprehensive guide filled with practical projects and step-by-step instructions.
  • Online Tutorials: Check out YouTube channels like CodeBeauty for easy-to-understand lessons presented in small, digestible parts.
  • Embedded: Arduino Interrupts Tutorial & Examples
  1. Learning the IDE: During this process, you will also learn what an IDE (Integrated Development Environment) is and how to use it to write, generate, and upload your code to the Arduino.
  2. Practice and Learn at Your Own Pace: Everyone learns at different speeds, and that’s completely normal. It may take a few months to grasp the basics, but with consistent practice, you’ll see progress.

Additional Help:

Please keep us updated on your journey, and don’t hesitate to ask questions as you learn. We’re here to help and guide you along the way!

#include <WiFi.h>
#include "time.h"
#include "esp_sntp.h"

const char *ssid = "YOUR_SSID";
const char *password = "YOUR_PASS";

const char *ntpServer1 = "pool.ntp.org";
const char *ntpServer2 = "time.nist.gov";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;

const char *time_zone = "CET-1CEST,M3.5.0,M10.5.0/3";  // TimeZone rule for Europe/Rome including daylight adjustment rules (optional)

void printLocalTime() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("No time available (yet)");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}

// Callback function (gets called when time adjusts via NTP)
void timeavailable(struct timeval *t) {
  Serial.println("Got time adjustment from NTP!");
  printLocalTime();
}

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

  // First step is to configure WiFi STA and connect in order to get the current time and date.
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);

  /**
   * NTP server address could be acquired via DHCP,
   *
   * NOTE: This call should be made BEFORE esp32 acquires IP address via DHCP,
   * otherwise SNTP option 42 would be rejected by default.
   * NOTE: configTime() function call if made AFTER DHCP-client run
   * will OVERRIDE acquired NTP server address
   */
  esp_sntp_servermode_dhcp(1);  // (optional)

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

  // set notification call-back function
  sntp_set_time_sync_notification_cb(timeavailable);

  /**
   * This will set configured ntp servers and constant TimeZone/daylightOffset
   * should be OK if your time zone does not need to adjust daylightOffset twice a year,
   * in such a case time adjustment won't be handled automagically.
   */
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);

  /**
   * A more convenient approach to handle TimeZones with daylightOffset
   * would be to specify a environment variable with TimeZone definition including daylight adjustmnet rules.
   * A list of rules for your zone could be obtained from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
   */
  //configTzTime(time_zone, ntpServer1, ntpServer2);
}

void loop() {
  delay(5000);
  printLocalTime();  // it will take some time to sync time :)
}

I am attaching the code ...just in case someone will give an help !
Since I want to sychronize my local time automatically I would remove lines 10 and 11 , I would change line 13 to meet my tzone with automatic daylight correction. I do not know if line 45 will be commented, I am not sure which lines ( 61 or 68 ) I have to activate. In addidion, I do not understand how often the sNTP sychronizes the ESP ... or which command should I send to to synchronize ( if possible ).
Thank again for any assistance

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.