Advertisement
TolentinoCotesta

ESP8266 TimeZone

Apr 11th, 2020
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <time.h>                       // time() ctime()
  3. #include <sys/time.h>                   // struct timeval
  4.  
  5.  
  6. #ifndef STASSID
  7. #define STASSID "xxxxxxxxxx"
  8. #define STAPSK  "xxxxxxxxxx"
  9. #endif
  10.  
  11. const char* ssid     = STASSID;
  12. const char* password = STAPSK;
  13.  
  14. time_t now;
  15. struct tm localTime;
  16.  
  17. void setup() {
  18.   Serial.begin(115200);
  19.  
  20.   // We start by connecting to a WiFi network
  21.  
  22.   Serial.println();
  23.   Serial.println();
  24.   Serial.print("Connecting to ");
  25.   Serial.println(ssid);
  26.  
  27.   /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
  28.      would try to act as both a client and an access-point and could cause
  29.      network-issues with your other WiFi-devices on your WiFi-network. */
  30.   WiFi.mode(WIFI_STA);
  31.   WiFi.begin(ssid, password);
  32.  
  33.   while (WiFi.status() != WL_CONNECTED) {
  34.     delay(500);
  35.     Serial.print(".");
  36.   }
  37.  
  38.   Serial.println("");
  39.   Serial.println("WiFi connected");
  40.   Serial.println("IP address: ");
  41.   Serial.println(WiFi.localIP());
  42.   delay(1000);
  43.  
  44.   configTime(0, 0, "time.google.com", "time.windows.com", "pool.ntp.org");
  45.  
  46.   // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.json  
  47.   setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
  48.   tzset();
  49.  
  50.   now = time(nullptr);    
  51.   localTime = *localtime(&now);  
  52.  
  53. }
  54.  
  55. void loop() {
  56.   static uint32_t updateTime;
  57.   if(millis() - updateTime > 1000){
  58.     updateTime = millis();
  59.    
  60.     // Aggiorno la variabile now
  61.     now = time(nullptr);  
  62.     // e la struttura localTime con il valore di now
  63.     localTime = *localtime(&now);
  64.     // La struttura localTime ora contiene il dateTime aggiornato
  65.  
  66.     // Ora stampiamo sulla seriale la data aggiornata    
  67.     char buf[30];
  68.     strftime(buf, sizeof(buf), "\n %d/%m/%Y - %H:%M:%S", &localTime);    
  69.     Serial.println(buf);    
  70.  
  71.     snprintf(buf, 30, "\n Ora legale: %s", localTime.tm_isdst ? "SI" : "NO");
  72.     Serial.println(buf);  
  73.    
  74.  
  75.  
  76.     /* localTime è una struttura di tipo tm definita in questo modo nel file time.h
  77.        struct tm
  78.         {
  79.           int  tm_sec;
  80.           int tm_min;
  81.           int tm_hour;
  82.           int tm_mday;
  83.           int tm_mon;
  84.           int tm_year;
  85.           int tm_wday;
  86.           int tm_yday;
  87.           int tm_isdst;
  88.         #ifdef __TM_GMTOFF
  89.           long  __TM_GMTOFF;
  90.         #endif
  91.         #ifdef __TM_ZONE
  92.           const char *__TM_ZONE;
  93.         #endif
  94.         };
  95.      */
  96.    
  97.     // E' ovviamente possibile estrarre ogni singola informazione di cui abbiamo bisogno
  98.     /*
  99.      int Year = localTime.tm_year;  
  100.      int Month = localTime.tm_mon;
  101.      int Day = localTime.tm_mday;
  102.      int Hour = localTime.tm_hour;
  103.      int Min = localTime.tm_min;
  104.      int Sec = localTime.tm_sec;    
  105.      */    
  106.   }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement