Advertisement
macca-nz

ESP32Time using inbuilt 64bit time_t

Apr 21st, 2023 (edited)
1,455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * This sketch will set your ESP32 RTC using an NTP server
  3.  * The "time_t" NTP functions are now part of the ESP32 WiFi library.
  4.  *
  5.  * The "ESP32Time.h" library is used to set the ESP32's RTC from the NTP UNIX time update
  6.  * Once the RTC has been set all Serial Monitor Clock Print's are
  7.  * generated from the ESP32's RTC.
  8.  *
  9.  * In it's current form the sketch has been configured to print a time stamp every second
  10.  * and then resync to NTP on the hour change
  11.  *
  12.  * List of TZ strings here <https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv>
  13.  * Time print formatting options <https://cplusplus.com/reference/ctime/strftime/>
  14.  *
  15.  */
  16.  
  17. #define TZ_INFO "NZST-12NZDT,M9.5.0,M4.1.0/3"       //NZST/NZDT  UTC +12/+13      
  18.  
  19. #include <WiFi.h>
  20. #include <ESP32Time.h>
  21. ESP32Time rtc;
  22. time_t now;
  23.  
  24. const char ssid[] = "Your_SSID";
  25. const char password[] = "Your_Password";                            
  26. const int32_t DEFAULT_TIME = 1609459200;            //1st Jan 2021 00:00:00
  27. int lastHour, lastSecond;
  28.  
  29. void NTPupdate(void){
  30.     volatile int32_t epoch = 0;
  31.     Serial.println("\nConnecting to WiFi.....");
  32.     WiFi.begin(ssid, password);
  33.     WiFi.setAutoReconnect(false);
  34.     while(WiFi.status() != WL_CONNECTED);
  35.     Serial.print("IP address: ");Serial.println(WiFi.localIP());
  36.     Serial.println("WiFi Connected.....");
  37.     Serial.println("Getting NTP Time Update");
  38.     while(epoch < DEFAULT_TIME){          //Wait till the NTP server reponds    
  39.         epoch = time(&now);               //Gets the current Unix time
  40.     }
  41.     rtc.setTime(epoch);             //Sets RTC using UNIX Time (EPOCH)
  42.     Serial.println(rtc.getTime("NTP Update Success!!.....\n%a %d-%B-%Y %T UTC: %z"));
  43.     if(WiFi.status() == WL_CONNECTED){
  44.         WiFi.disconnect(true, false);
  45.         Serial.println("[WiFi] Disconnected from WiFi!!\n");
  46.     }
  47.     lastHour = rtc.getHour();
  48.     lastSecond = rtc.getSecond();
  49.     return;
  50. }
  51.  
  52. void setup(void) {
  53.     Serial.begin(115200);
  54.     while(!Serial);
  55.     configTime(0, 0, "pool.ntp.org", "time.nist.gov");
  56.     setenv("TZ", TZ_INFO, 1); tzset();
  57.     NTPupdate();
  58. }
  59.  
  60. void loop(void) {
  61.     if(lastSecond != rtc.getSecond()){              //When RTC second is 10, 30 or 50 a resync
  62.         lastSecond = rtc.getSecond();               //with the time Server is actioned
  63.         Serial.println(rtc.getTime("(%Z) Local Time %I:%M:%S (%p)"));     //Creates our Clock
  64.             if(lastHour != rtc.getHour()){
  65.                 NTPupdate();
  66.             }
  67.     }      
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement