Use or abuse of configTime() function in ESP8266?

Hi guys! I need some of your expertise. My code is working just fine (following an excerpt of my code):

...
static const char WEB_ACTIONS1[] PROGMEM = "<a class='w3-bar-item w3-button' href='/'><i class='fas fa-home'></i> Home</a>"
    "<a class='w3-bar-item w3-button' href='/configure'><i class='fas fa-cog'></i> Configure</a>"
    "<a class='w3-bar-item w3-button' href='/display'>";

static const char WEB_ACTION2[] PROGMEM = "</a><a class='w3-bar-item w3-button' href='#' onClick='confirm(\"Are you sure your want to reboot?\") && window.location.replace(\"/reboot\");'><i class='fas fa-power-off'></i> Reboot</a>";

static const char CHANGE_FORM[] PROGMEM = "<form class='w3-container' action='/saveconfig' method='get'><h2>Configure</h2>"
    "<p><label>TimeZone (insert code from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h)</label></p>"
    "<p><input class='w3-input w3-border w3-margin-bottom' type='text' name='TimeZoneDB' value='%TIMEDBKEY%' maxlength='40'></p>"
    "<hr>"
    "<h3>Security</h3>"
    "<p><input name='isBasicAuth' class='w3-check w3-margin-top' type='checkbox' %IS_BASICAUTH_CHECKED%> Use Security Credentials for Configuration Changes</p>"
    "<p><label>Word-Clock User ID (for this web interface)</label><input class='w3-input w3-border w3-margin-bottom' type='text' name='userid' value='%USERID%' maxlength='20'></p>"
    "<p><label>Word-Clock Password</label><input class='w3-input w3-border w3-margin-bottom' type='password' name='stationpassword' value='%STATIONPASSWORD%'></p>"
    "<hr>"
    "<p><button class='w3-button w3-block w3-green w3-section w3-padding' type='submit'>Save</button></p></form>"
    "<script>function isNumberKey(e){var h=e.which?e.which:event.keyCode;return!(h>31&&(h<48||h>57))}</script>";
//
void setup() {
  Serial.begin (115200);
  LittleFS.begin();
  
  //Initialize
  const char* MY_TZ = TIMEDBKEY.c_str();   //Converts String to const char*
  configTime(MY_TZ, MY_NTP_SERVER); // --> Here is the IMPORTANT ONE LINER needed in your sketch!

  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;

  // Uncomment for testing wifi manager
  //wifiManager.resetSettings();
  wifiManager.setAPCallback(configModeCallback);

  String hostname(HOSTNAME);
  hostname += String(ESP.getChipId(), HEX);
  if (!wifiManager.autoConnect((const char *)hostname.c_str())) {// new addition
    delay(3000);
    WiFi.disconnect(true);
    ESP.reset();
    delay(5000);
  }
  if (WEBSERVER_ENABLED) {
    
    server.on("/", handleHome);
    server.on("/locations", handleLocations);
    server.on("/configure", handleConfigure);
    server.on("/saveconfig", processConfig);
    
    server.onNotFound(redirectHome);
    serverUpdater.setup(&server, "/update", www_username, www_password);
    // Start the server
    server.begin();
    Serial.println("Server started");
    // Print the IP address
    String webAddress = "http://" + WiFi.localIP().toString() + ":" + String(WEBSERVER_PORT) + "/";
    Serial.println("Use this URL : " + webAddress);
    Serial.println(" v" + String(VERSION) + "  IP:" + WiFi.localIP().toString() + "  ");
  } else {
    Serial.println("Web Interface is Disabled");
  }

} //End Setup

//************************************************************
// Main Loop
//************************************************************
void loop() {
  
  time(&now);                       // read the current time
  localtime_r(&now, &tm);           // update the structure tm with the current time

  //MY_TZ = "CST6CDT,M3.2.0,M11.1.0";    //America_Chicago
  //MY_TZ = "<-04>4";                    //America_La_Paz
  //MY_TZ = "PST8PDT,M3.2.0,M11.1.0";    //America_Los_Angeles

  const char* MY_TZ = TIMEDBKEY.c_str();   //Converts String to const char*

  // calculate world-time
  configTime(MY_TZ, MY_NTP_SERVER);
  
  if (WEBSERVER_ENABLED) {
    server.handleClient();
  }

  //handleHome();
  delay(1000);
} //End Loop
...

So, you may ask: what is my question then?

In every code that I have seen in the web the configTime() function is only used in the setup() section, however I also needed, AFAIK, in the loop() section. Why?

This clock is supposed to be carried anywhere in the world and, hence, the owner (without any Arduino IDE knowledge) should be able to configure the TimeZone from an html input variable (TIMEDBKEY) in a WEB page.

I’m a newbie and haven’t found a better way to do this. So, my question is: does anyone knows another way to do this? Do you see a problem using configTime() function in the loop() section?

Thanks in advanced.

Try setTimezone() in loop().
Explained here.
Leo..

1 Like

Thank you very much Leo!! That is exactly what I needed!

here I have a version where the User can select from a Listbox

https://werner.rothschopf.net/microcontroller/202402_arduino_esp_ntp_user_configuration.htm