macca-nz

Minimal ezTime ESP32/8266 with debug info

Apr 26th, 2023 (edited)
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.25 KB | Source Code | 0 0
  1. /*
  2.  * Bare bones minimal ezTime sketch using NTP sync of 60 seconds
  3.  * Debug info on Sync
  4.  * Two main loop library defined clock stamps
  5.  *
  6.  * Provide official timezone names for "myTZ.setLocation(F("TZ_Name"));"
  7.  * https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  8.  *
  9.  * NOTE: Only tested on ESP32
  10.  */
  11.  
  12. #include <ezTime.h>               //Library to use our system clock for NTP time
  13.  
  14. #if defined (ESP32)               //Lets the compiler work with
  15.     #include <WiFi.h>             //ESP32
  16. #elif defined (ESP8266)           //or
  17.     #include <ESP8266WiFi.h>      //ESP8266 without changing any script
  18. #endif
  19.  
  20. Timezone myTZ;                    //Creates our Timezone object to get "Local Time"
  21.  
  22. const char* ssid = "Your_ssid";            
  23. const char* password = "Your_password";  
  24.  
  25. int lastSec, count = 0;
  26.  
  27. void setup() {
  28.     Serial.begin(115200);while(!Serial){;}
  29.     #ifdef ESP32
  30.       Serial.println("\nConnecting ESP32 to WiFi\n");
  31.     #else ifdef ESP8266
  32.       Serial.println("\nConnecting ESP8266 to WiFi\n");
  33.     #endif
  34.     WiFi.begin(ssid, password);
  35.     while(WiFi.status() != WL_CONNECTED);
  36.     setInterval(60);                        //Overrides default NTP update to 60 seconds
  37.     setDebug(INFO);                         //Provides some key timing debug data on update
  38.     waitForSync();                          //Default NTP method - must run loop "events();"
  39.     myTZ.setLocation(F("Pacific/Auckland"));
  40.     Serial.print(F("Local (GeoIP):   "));
  41.   if (myTZ.setLocation()) {
  42.     Serial.println("\n" + myTZ.dateTime());
  43.   } else {
  44.     Serial.println(errorString());
  45.   }
  46.   Serial.print("\nNTP Sync done.......\nStarting System Clock in: ");
  47.   int i = 6;
  48.   while(i > 0){i--;Serial.print(i);Serial.print(" ");delay(1000);}
  49.   Serial.println("\n");
  50. }
  51.  
  52. void loop() {
  53.     events();                       //Needed to run the NTP setup method
  54.     if(lastSec != myTZ.second()){
  55.         lastSec = myTZ.second();
  56.         count++; count %= 10;
  57.             if(count < 9){          //Simple Clock Display with two Stamp formats
  58.                 Serial.println("RFC822 Stamp:\t" + myTZ.dateTime(RFC822));
  59.             }else{
  60.                 Serial.println("\nDefault Stamp:\t" + myTZ.dateTime() + "\n");
  61.             }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment