NTP Montreal date and time - need to turn this into a function in another script

Montreal_Time_v6_OK_Final.ino (3,0 Ko)

Would like to turn this into a function that could be used in another script.

Hi,
Can you please post your code into a new post please?
To add code please click this link;

What controller model has it been written for?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

It seems you're using bloated code for a previous version of the ESP8266.
Most of the code you have comes already with the ESP8266WiFi.h library.
Have a look at this page.
Leo..

1 Like

Using ESP 12E

Here is code:

// Montreal_Time_v6_OK_Final.ino
// https://forum.arduino.cc/t/ntpclient-local-usa-eastcoast-time-solved/1028467/18
/* Daylight Saving Time in Montreal
Start DST:	Sunday 12 mars     2023 — 1 hour forward
End   DST:	Sunday 05 novembre 2023 — 1 hour backward
*/
//==============================================================
#ifndef STASSID
#define STASSID "MY_WIFI_SSID"     // set your SSID
#define STAPSK "MY_WIFI_PASSWORD"  // set your wifi password
#endif
//===============================================================
#define MY_NTP_SERVER "ca.pool.ntp.org"   /* Configuration of NTP */
#define MY_TZ "EST5EDT,M3.2.0,M11.1.0 I"  // Montreal daylight time included
//===============================================================
#include <ESP8266WiFi.h>  // we need wifi to get internet access //--------------- test ESP32
#include <time.h>         // for time() ctime()
// Globals ======================================================
time_t now;  // this are the seconds since Epoch (1970) - UTC
tm tm;
String Current_Month;
String Current_Day;
String Current_Hour;
String Current_Minute;
String Current_Seconds;
// Padding Month, day, hour, min, sec if less than 10 add 0 =====
void showTime() {
  time(&now);                // read the current time
  localtime_r(&now, &tm);    // update the structure tm with the current time
  if (tm.tm_mon + 1 < 10) {  // Month
    Current_Month = "0" + String(tm.tm_mon + 1);
  } else {
    Current_Month = String(tm.tm_mon + 1);
  }
  if (tm.tm_mday < 10) {  // Day
    Current_Day = "0" + String(tm.tm_mday);
  } else {
    Current_Day = String(tm.tm_mday);
  }
  if (tm.tm_hour < 10) {  // Hour:
    Current_Hour = "0" + String(tm.tm_hour);
  } else {
    Current_Hour = String(tm.tm_hour);
  }
  if (tm.tm_min < 10) {  // Minute:
    Current_Minute = "0" + String(tm.tm_min);
  } else {
    Current_Minute = String(tm.tm_min);
  }
  if (tm.tm_sec < 10) {  // Seconds:
    Current_Seconds = "0" + String(tm.tm_sec);
  } else {
    Current_Seconds = String(tm.tm_sec);
  }
  Serial.print("Formatted Date: ");
  Serial.println(String(tm.tm_year + 1900) + "-" + Current_Month + "-" + (Current_Day));
  Serial.println();
  Serial.print("Formatted Time: ");
  Serial.println((Current_Hour) + ":" + Current_Minute + ":" + (Current_Seconds));
  Serial.println();
}
//===============================================================
void setup() {
  Serial.begin(115200);
  Serial.println("\nNTP TZ DST - bare minimum");
  configTime(MY_TZ, MY_NTP_SERVER);  // --> Here is the IMPORTANT ONE LINER needed in your sketch!
  // start network
  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);
  WiFi.begin(STASSID, STAPSK);
  while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected");  // by default, the NTP will be started after 60 secs
}
//===============================================================
void loop() {
  showTime();
  delay(1000);  // dirty delay
}

what exactly are you meaning?
showTime() is already an example of a function for how to access the data elements of the structure.

Hi noiasca,
Does ShowTime retrieves actual date and time with Daylight Savings time for eastern US without usin RTC?

What I wanted is to have ")" padded in front of moths when the month is less than 10.
Format "YYYY-MM-DD" same for time as "HH;MM;SS" when those are less than 10.

i MEAN What I wanted is to have "0" padded

Is this what you want.
Leo..

#include <ESP8266WiFi.h>
time_t now;
tm tm;

void setup() {
  Serial.begin(115200);
  configTime("EST5EDT,M3.2.0,M11.1.0 I", "ca.pool.ntp.org");
  WiFi.mode(WIFI_STA);
  WiFi.begin("MY_WIFI_SSID", "MY_WIFI_PASSWORD"); // enter credentials here ........
  while (WiFi.status() != WL_CONNECTED) delay(200);
  Serial.println(F("\nConnected"));
}

void loop() {
  time(&now);
  localtime_r(&now, &tm);
  printf("%02u:%02u:%02u\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
  delay(1000);
}

the code you posted does already a padding. What should be different?

Hi, this is great for time, How would I go for formatted date yyyy-mm-dd ?

Look at the printf options for formatting.
%02u means leading zero, 2 digits, unsigned int (could have used d for int).
The names of the time struct are in the NTP link I gave you.
Colons in between, and a newline \n at the end.
Leo..

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