TolentinoCotesta

struct tm

Sep 8th, 2021 (edited)
1,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. // Definizione Timezone (fuso orario, intervalli ora legale/solare)
  2. #include <time.h>
  3. #define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
  4.  
  5. // Struttura che andrà a memorizzare il timedate aggiornato
  6. struct tm tInfo;
  7.  
  8.  
  9. // Cosi compila sia con ESP8266 che ESP32
  10. // (al momento ho solo gli 8266 disponibili)
  11. #ifdef ESP8266
  12.   #include <ESP8266WiFi.h>
  13. #elif defined(ESP32)
  14.   #include <WiFi.h>
  15. #endif
  16.  
  17. int numDaysWork = 0;
  18. int oldNumDaysWork = -1;
  19.  
  20. const char* ssid  =  "xxxxxxx";     // SSID WiFi network
  21. const char* pass  =  "xxxxxxx";     // Password  WiFi network
  22.  
  23.  
  24.  
  25. void stampaData() {
  26.   // Stampo la data attuale sulla seriale
  27.   Serial.printf("%02d/%02d/%04d - %02d:%02d:%02d\n",
  28.                 tInfo.tm_mday, tInfo.tm_mon + 1, tInfo.tm_year + 1900,
  29.                 tInfo.tm_hour, tInfo.tm_min, tInfo.tm_sec);
  30. }
  31.  
  32.  
  33. void checkMidnight() {
  34.   static bool incremento = false;
  35.  
  36.   // Recupero timedate attuale e localizzato secondo il timestamp
  37.   time_t now = time(nullptr);
  38.  
  39.   // Aggiorno la struct tm
  40.   tInfo = *localtime(&now);
  41.  
  42.   // Verifico se è mezzanotte
  43.   if ((tInfo.tm_sec == 0) && (!incremento)) {
  44.     incremento = true;
  45.     Serial.println("E' mezzanotte");
  46.     numDaysWork++;
  47.     stampaData();    
  48.   }
  49.  
  50.   if(tInfo.tm_sec == 1){
  51.     incremento = false;
  52.   }
  53. }
  54.  
  55. void setup() {
  56.    // initialize the Serial
  57.   Serial.begin(115200);
  58.   Serial.println("\nStarting...");
  59.  
  60.   WiFi.mode(WIFI_STA);
  61.   WiFi.begin(ssid, pass);
  62.   delay(500);
  63.   while (WiFi.status() != WL_CONNECTED) {
  64.     Serial.print('.');
  65.     delay(500);
  66.   }Serial.println();
  67.  
  68. // Sync time with NTP
  69. #ifdef ESP8266
  70.   configTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
  71. #elif defined(ESP32)  
  72.   configTzTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
  73. #endif
  74.  
  75.   // E' necessario un po' di tempo per sincronizzare il tempo con il server remoto
  76.   delay(2000);
  77.   // Recupero timedate attuale e localizzato secondo il timestamp
  78.   time_t now = time(nullptr);
  79.  
  80.   // Aggiorno la struct tm tInfo
  81.   tInfo = *localtime(&now);
  82.  
  83.   // Stampo la data attuale sulla seriale
  84.   stampaData();
  85. }
  86.  
  87. void loop() {
  88.   checkMidnight();
  89.  
  90.   // Stampo sulla serial il di numDaysWork quando cambia
  91.   if( numDaysWork != oldNumDaysWork) {
  92.     oldNumDaysWork = numDaysWork;
  93.     Serial.print("Days: ");
  94.     Serial.println(numDaysWork);
  95.   }
  96. }
Add Comment
Please, Sign In to add comment