Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Definizione Timezone (fuso orario, intervalli ora legale/solare)
- #include <time.h>
- #define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
- // Struttura che andrà a memorizzare il timedate aggiornato
- struct tm tInfo;
- // Cosi compila sia con ESP8266 che ESP32
- // (al momento ho solo gli 8266 disponibili)
- #ifdef ESP8266
- #include <ESP8266WiFi.h>
- #elif defined(ESP32)
- #include <WiFi.h>
- #endif
- int numDaysWork = 0;
- int oldNumDaysWork = -1;
- const char* ssid = "xxxxxxx"; // SSID WiFi network
- const char* pass = "xxxxxxx"; // Password WiFi network
- void stampaData() {
- // Stampo la data attuale sulla seriale
- Serial.printf("%02d/%02d/%04d - %02d:%02d:%02d\n",
- tInfo.tm_mday, tInfo.tm_mon + 1, tInfo.tm_year + 1900,
- tInfo.tm_hour, tInfo.tm_min, tInfo.tm_sec);
- }
- void checkMidnight() {
- static bool incremento = false;
- // Recupero timedate attuale e localizzato secondo il timestamp
- time_t now = time(nullptr);
- // Aggiorno la struct tm
- tInfo = *localtime(&now);
- // Verifico se è mezzanotte
- if ((tInfo.tm_sec == 0) && (!incremento)) {
- incremento = true;
- Serial.println("E' mezzanotte");
- numDaysWork++;
- stampaData();
- }
- if(tInfo.tm_sec == 1){
- incremento = false;
- }
- }
- void setup() {
- // initialize the Serial
- Serial.begin(115200);
- Serial.println("\nStarting...");
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, pass);
- delay(500);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print('.');
- delay(500);
- }Serial.println();
- // Sync time with NTP
- #ifdef ESP8266
- configTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
- #elif defined(ESP32)
- configTzTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
- #endif
- // E' necessario un po' di tempo per sincronizzare il tempo con il server remoto
- delay(2000);
- // Recupero timedate attuale e localizzato secondo il timestamp
- time_t now = time(nullptr);
- // Aggiorno la struct tm tInfo
- tInfo = *localtime(&now);
- // Stampo la data attuale sulla seriale
- stampaData();
- }
- void loop() {
- checkMidnight();
- // Stampo sulla serial il di numDaysWork quando cambia
- if( numDaysWork != oldNumDaysWork) {
- oldNumDaysWork = numDaysWork;
- Serial.print("Days: ");
- Serial.println(numDaysWork);
- }
- }
Add Comment
Please, Sign In to add comment