// makerguides.com: synchronizing ESP32 time with SNTP server #include "WiFi.h" #include "esp_sntp.h" #include #include #include #define HARDWARE_TYPE MD_MAX72XX::FC16_HW #define MAX_DEVICES 4 #define CLK_PIN 18 #define DATA_PIN 23 #define CS_PIN 5 MD_Parola Display = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES); const char* ssid = "---------"; const char* password = "-----------"; // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv //const char *timezone = "AEST-10AEDT,M10.1.0,M4.1.0/3"; //Sydney Australie const char *timezone = "CET-1CEST,M3.5.0,M10.5.0/3";//PARIS String Time, heure, minute; String Formatted_date; void initWiFi() { WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Attente connection wifi...."); } } void initSNTP() { sntp_set_sync_interval(60 * 60 * 1000UL); // 1 h esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL); esp_sntp_setservername(0, "pool.ntp.org"); esp_sntp_init(); setenv("TZ", timezone, 1); tzset(); } void wait4SNTP() { Serial.println("String Time, hour, minute;String Formatted_date;synchronizing..."); while (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) { delay(100); } } void obtainTime() { // https://cplusplus.com/reference/ctime/tm/ char buff1[4]; char buff2[4]; struct tm t; getLocalTime(&t); sprintf(buff1, "%02d", t.tm_hour); sprintf(buff2,"%02d" , t.tm_min); Serial.println("Heures"); Serial.println(buff1); Serial.println("Minutes"); Serial.println(buff2); heure = buff1; minute = buff2; Time = heure +":"+ minute; Display. setTextAlignment(PA_CENTER); Display.print(Time); } void setup() { Serial.begin(115200); initWiFi(); initSNTP(); wait4SNTP(); Display.begin(); Display.setIntensity(0); Display.displayClear(); } void loop() { obtainTime(); delay(1000); }