cotestatnt ho utilizzato il tuo sketch per fare le prove ed il risultato non cambia, i valori di now() e hour() risultano diversi.
Ma hour() non è un sottoinsieme di now() ?
Scusami se ti sto facendo perdere del tempo.
#include <ESP8266WiFi.h>
#include <TimeLib.h>
#include <time.h>
// Timezone definition
#define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
/*--------------------------------------------------*/
static tm getDateTimeByParams(long time) {
struct tm *newtime;
const time_t tim = time;
newtime = localtime(&tim);
return *newtime;
}
/**
Input tm time format and return String with format pattern
by Renzo Mischianti <www.mischianti.org>
*/
static String getDateTimeStringByParams(tm *newtime, char* pattern = (char *)"%d.%m.%Y %H:%M:%S") {
char buffer[30];
strftime(buffer, 30, pattern, newtime);
return buffer;
}
/**
Input time in epoch format format and return String with format pattern
by Renzo Mischianti <www.mischianti.org>
*/
static String getTimeGMY_HMS(long time, char* pattern = (char *)"%d.%m.%Y-%H:%M:%S") {
// struct tm *newtime;
tm newtime;
newtime = getDateTimeByParams(time);
return getDateTimeStringByParams(&newtime, pattern);
}
char tmnow[9] = "00:00:00"; // "hh:mm:ss"
char dt[11] = "00.00.0000";
void loop() {
static uint32_t pTime;
if (millis() - pTime > 1000) {
pTime = millis();
time_t ora;
tm tInfo;
getLocalTime(&tInfo); //Update tInfo struct
Serial.print("\n tInfo.tm_mday : "); Serial.println(tInfo.tm_mday);
Serial.print(" tInfo.tm_mon +1 : "); Serial.println(tInfo.tm_mon+1);
Serial.print(" tInfo.tm_year+1900 : "); Serial.println(tInfo.tm_year+1900);
Serial.print(" tInfo.tm_hour : "); Serial.println(tInfo.tm_hour);
Serial.print(" tInfo.tm_min : "); Serial.println(tInfo.tm_min);
Serial.print(" tInfo.tm_sec : "); Serial.println(tInfo.tm_sec);
Serial.print(" tInfo.tm_wday : "); Serial.println(tInfo.tm_wday);
Serial.print(" tInfo.tm_isdst : "); Serial.println(tInfo.tm_isdst);
ora = mktime(&tInfo);
setTime(tInfo.tm_hour, tInfo.tm_min, tInfo.tm_sec, tInfo.tm_mday, tInfo.tm_mon + 1, tInfo.tm_year + 1900);
time_t ora_now;
ora_now = now();
Serial.println("\n---------- setTime(tmt.tm_hour, tmt.tm_min,...... ");
Serial.print("\n1. ora : "); Serial.println(ora);
Serial.print("1. now() : "); Serial.println(now());
Serial.print("1. ora in chiaro : ");Serial.print(ctime(&ora));
Serial.print("1. now() in chiaro : ");Serial.print(ctime(&ora_now));
Serial.print("1. getTimeGMY_HMS(now()): "); Serial.println(getTimeGMY_HMS(now()));
Serial.print("1. getTimeGMY_HMS(ora) : "); Serial.println(getTimeGMY_HMS(ora));
sprintf(tmnow, "%02u:%02u:%02u", hour(), minute(), second());
sprintf(dt, "%02d.%02d.%04d", day(), month(), year());
Serial.print("1. ora-data hour() : "); Serial.print(dt); Serial.print("-"); Serial.println(tmnow);
sprintf(tmnow, "%02u:%02u:%02u", hour(now()), minute(now()), second(now()));
sprintf(dt, "%02d.%02d.%04d", day(now()), month(now()), year(now()));
Serial.print("1. ora-data hour(now()) : "); Serial.print(dt); Serial.print("-"); Serial.println(tmnow);
setTime(ora);
// time_t ora_now;
ora_now = now();
Serial.println("\n---------- setTime(ora) --------------------");
Serial.print("\n2. ora : "); Serial.println(ora);
Serial.print("2. now() : "); Serial.println(now());
Serial.print("2. ora in chiaro : ");Serial.print(ctime(&ora));
Serial.print("2. now() in chiaro : ");Serial.print(ctime(&ora_now));
Serial.print("2. getTimeGMY_HMS(now()): "); Serial.println(getTimeGMY_HMS(now()));
Serial.print("2. getTimeGMY_HMS(ora) : "); Serial.println(getTimeGMY_HMS(ora));
sprintf(tmnow, "%02u:%02u:%02u", hour(), minute(), second());
sprintf(dt, "%02d.%02d.%04d", day(), month(), year());
Serial.print("2. ora-data hour() : "); Serial.print(dt); Serial.print("-"); Serial.println(tmnow);
sprintf(tmnow, "%02u:%02u:%02u", hour(now()), minute(now()), second(now()));
sprintf(dt, "%02d.%02d.%04d", day(now()), month(now()), year(now()));
Serial.print("2. ora-data hour(now()) : "); Serial.print(dt); Serial.print("-"); Serial.println(tmnow);
}
delay(60000); // Increase simulation performance
}
void setup() {
Serial.begin(115200);
Serial.println("Connecting to WiFi ");
WiFi.begin("R_1_Mya_2.4", "1_Marina_0hkl");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("\nWiFi connected. ");
Serial.println("IP address: ");
Serial.println(WiFi.localIP().toString());
// Sync time with NTP server
configTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
}
e questo è il log
16:25:22.551 -> .......
16:25:26.335 -> WiFi connected. IP address:
16:25:26.335 -> 192.168.2.169
16:25:26.382 ->
16:25:26.382 -> tInfo.tm_mday : 28
16:25:26.382 -> tInfo.tm_mon +1 : 3
16:25:26.429 -> tInfo.tm_year+1900 : 2023
16:25:26.429 -> tInfo.tm_hour : 16
16:25:26.429 -> tInfo.tm_min : 25
16:25:26.429 -> tInfo.tm_sec : 25
16:25:26.429 -> tInfo.tm_wday : 2
16:25:26.429 -> tInfo.tm_isdst : 1
16:25:26.429 ->
16:25:26.429 -> ---------- setTime(tmt.tm_hour, tmt.tm_min,......
16:25:26.429 ->
16:25:26.429 -> 1. ora : 1680013525
16:25:26.429 -> 1. now() : 1680020725
16:25:26.429 -> 1. ora in chiaro : Tue Mar 28 16:25:25 2023
16:25:26.429 -> 1. now() in chiaro : Tue Mar 28 18:25:25 2023
16:25:26.429 -> 1. getTimeGMY_HMS(now()): 28.03.2023-18:25:25
16:25:26.429 -> 1. getTimeGMY_HMS(ora) : 28.03.2023-16:25:25
16:25:26.429 -> 1. ora-data hour() : 28.03.2023-16:25:25
16:25:26.476 -> 1. ora-data hour(now()) : 28.03.2023-16:25:25
16:25:26.476 ->
16:25:26.476 -> ---------- setTime(ora) --------------------
16:25:26.476 ->
16:25:26.476 -> 2. ora : 1680013525
16:25:26.476 -> 2. now() : 1680013525
16:25:26.476 -> 2. ora in chiaro : Tue Mar 28 16:25:25 2023
16:25:26.476 -> 2. now() in chiaro : Tue Mar 28 16:25:25 2023
16:25:26.476 -> 2. getTimeGMY_HMS(now()): 28.03.2023-16:25:25
16:25:26.476 -> 2. getTimeGMY_HMS(ora) : 28.03.2023-16:25:25
16:25:26.476 -> 2. ora-data hour() : 28.03.2023-14:25:25
16:25:26.476 -> 2. ora-data hour(now()) : 28.03.2023-14:25:25