ap5Lj9rB2AMoQ7

Untitled

Dec 12th, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. // RTC demo for ESP32, that includes TZ and DST adjustments
  2. // Get the POSIX style TZ format string from https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
  3. // Created by Hardy Maxa
  4. // Complete project details at: https://RandomNerdTutorials.com/esp32-ntp-timezones-daylight-saving/
  5.  
  6. #include <ESP8266WiFi.h>
  7. #include <time.h>
  8.  
  9. const char * ssid=" ";
  10. const char * wifipw=" ";
  11.  
  12. bool getLocalTime(struct tm * info, uint32_t ms = 5000)
  13. {
  14. uint32_t start = millis();
  15. time_t now;
  16. while((millis()-start) <= ms) {
  17. time(&now);
  18. localtime_r(&now, info);
  19. if(info->tm_year > (2016 - 1900)){
  20. return true;
  21. }
  22. delay(10);
  23. }
  24. return false;
  25. }
  26.  
  27. void setTimezone(String timezone){
  28. Serial.printf(" Setting Timezone to %s\n",timezone.c_str());
  29. setenv("TZ",timezone.c_str(),1); // Now adjust the TZ. Clock settings are adjusted to show the new local time
  30. tzset();
  31. }
  32.  
  33. void initTime(String timezone){
  34. struct tm timeinfo;
  35.  
  36. Serial.println("Setting up time");
  37. configTime(0, 0, "pool.ntp.org"); // First connect to NTP server, with 0 TZ offset
  38. if(!getLocalTime(&timeinfo)){
  39. Serial.println(" Failed to obtain time");
  40. return;
  41. }
  42. Serial.println(" Got the time from NTP");
  43. // Now we can set the real timezone
  44. setTimezone(timezone);
  45. }
  46.  
  47. void printLocalTime(){
  48. struct tm timeinfo;
  49. if(!getLocalTime(&timeinfo)){
  50. Serial.println("Failed to obtain time 1");
  51. return;
  52. }
  53. Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S zone %Z %z");
  54. }
  55.  
  56. void startWifi(){
  57. WiFi.begin(ssid, wifipw);
  58. Serial.println("Connecting Wifi");
  59. while (WiFi.status() != WL_CONNECTED) {
  60. Serial.print(".");
  61. delay(500);
  62. }
  63. Serial.print("Wifi RSSI=");
  64. Serial.println(WiFi.RSSI());
  65. }
  66.  
  67. void setTime(int yr, int month, int mday, int hr, int minute, int sec, int isDst){
  68. struct tm tm;
  69.  
  70. tm.tm_year = yr - 1900; // Set date
  71. tm.tm_mon = month-1;
  72. tm.tm_mday = mday;
  73. tm.tm_hour = hr; // Set time
  74. tm.tm_min = minute;
  75. tm.tm_sec = sec;
  76. tm.tm_isdst = isDst; // 1 or 0
  77. time_t t = mktime(&tm);
  78. Serial.printf("Setting time: %s", asctime(&tm));
  79. struct timeval now = { .tv_sec = t };
  80. settimeofday(&now, NULL);
  81. }
  82.  
  83. void setup(){
  84. Serial.begin(115200);
  85. Serial.setDebugOutput(true);
  86. startWifi();
  87.  
  88. initTime("WET0WEST,M3.5.0/1,M10.5.0"); // Set for Melbourne/AU
  89. printLocalTime();
  90. }
  91.  
  92. void loop() {
  93. int i;
  94.  
  95. // put your main code here, to run repeatedly:
  96. Serial.println("Lets show the time for a bit. Starting with TZ set for Melbourne/Australia");
  97. for(i=0; i<10; i++){
  98. delay(1000);
  99. printLocalTime();
  100. }
  101. Serial.println();
  102. Serial.println("Now - change timezones to Berlin");
  103. setTimezone("CET-1CEST,M3.5.0,M10.5.0/3");
  104. for(i=0; i<10; i++){
  105. delay(1000);
  106. printLocalTime();
  107. }
  108.  
  109. Serial.println();
  110. Serial.println("Now - Lets change back to Lisbon and watch Daylight savings take effect");
  111. setTimezone("WET0WEST,M3.5.0/1,M10.5.0");
  112. printLocalTime();
  113.  
  114. Serial.println();
  115. Serial.println("Now change the time. 1 min before DST takes effect. (1st Sunday of Oct)");
  116. Serial.println("AEST = Australian Eastern Standard Time. = UTC+10");
  117. Serial.println("AEDT = Australian Eastern Daylight Time. = UTC+11");
  118. setTime(2021,10,31,0,59,50,0); // Set it to 1 minute before daylight savings comes in.
  119.  
  120. for(i=0; i<20; i++){
  121. delay(1000);
  122. printLocalTime();
  123. }
  124.  
  125. Serial.println("Now change the time. 1 min before DST should finish. (1st Sunday of April)");
  126. setTime(2021,3,28,1,59,50,1); // Set it to 1 minute before daylight savings comes in. Note. isDst=1 to indicate that the time we set is in DST.
  127.  
  128. for(i=0; i<20; i++){
  129. delay(1000);
  130. printLocalTime();
  131. }
  132.  
  133. // Now lets watch the time and see how long it takes for NTP to fix the clock
  134. Serial.println("Waiting for NTP update (expect in about 1 hour)");
  135. while(1) {
  136. delay(1000);
  137. printLocalTime();
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment