Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This Sketch uses ezTime library to create a Serial monitor NTP clock
- * It is the bare minimum needed to do local time use NTP
- * The default NTP resync period of ezTime is 30 minutes
- * It prints a simple "Local Time" for seconds 1 to 9
- * On the minute a full default Time Stamp is printed
- *
- * Provide official timezone names
- * https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
- *
- * NOTE: Only tested on ESP32
- */
- #include <ezTime.h> //Library to use our system clock for NTP time
- #if defined (ESP32) //Lets the compiler work with
- #include <WiFi.h> //ESP32
- #elif defined (ESP8266) //or
- #include <ESP8266WiFi.h> //ESP8266 without changing any script
- #endif
- Timezone myTZ; //Creates our Timezone object to get "Local Time"
- const char* ssid = "Your_ssid";
- const char* password = "Your_password";
- int lastSec;
- String getMyTime(){ //Custom Clock Display
- char buf[22];
- String mT = "";
- sprintf(buf, "Local Time %02u:%02u:%02u", myTZ.hour(), myTZ.minute(), myTZ.second());
- mT += buf;
- return mT;
- }
- void setup() {
- Serial.begin(115200);while(!Serial){;}
- WiFi.begin(ssid, password);
- while(WiFi.status() != WL_CONNECTED);
- waitForSync(); //Default method for NTP sync every 30min
- myTZ.setLocation(F("Pacific/Auckland"));//Sets our Timezone object
- lastSec = myTZ.second();
- }
- void loop() {
- events(); //Needed to run the NTP setup method
- if(myTZ.second() != lastSec){
- lastSec = myTZ.second();
- if(myTZ.second() != 0){
- String myTime = getMyTime();
- Serial.println(myTime); //Use's the Custom Clock Display
- }else{
- Serial.println("\n" + myTZ.dateTime() + "\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment