Advertisement
macca-nz

NTP Time Stamp V2 (uses a String)

Mar 31st, 2022 (edited)
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //NTP Serial monitor time stamp print by making a String to the format desired
  2.  
  3. #include <WiFi.h>
  4. #include <WiFiUdp.h>
  5. #include "time.h"
  6.  
  7. String      time_str;
  8. uint32_t    previousTime = 0;
  9. const char* ssid     = "YOUR SSID HERE";
  10. const char* password = "YOUR WIFI PASSWORD";
  11.  
  12. //NTP TZ_INFO for Auto Zone and Daylight savings Offsets
  13. // This from Github    NZST-12NZDT,M9.5.0,M4.1.0/3
  14. // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
  15. const char* NTP_SERVER = "pool.ntp.org";  
  16. const char* TZ_INFO    = "NZST-12NZDT,M9.5.0,M4.1.0/3";
  17.  
  18. WiFiClient client; // wifi client object
  19.  
  20. void setup() {
  21.   Serial.begin(115200);
  22.   if (!StartWiFi(ssid,password))
  23.   Serial.println("Failed to start WiFi Service after 20 attempts");;
  24.  
  25.   /* configTime(12*3600, 0, "pool.ntp.org"); // +1hour (1*60*60=3600=+1hour) ahead for DST in the UK */
  26.   configTime(0, 0, NTP_SERVER); // if TZ configured
  27.     setenv("TZ", TZ_INFO, 1);   // for TZ and Daylight Auto change
  28.   time_t now = time(nullptr);
  29.   delay(2000); // Wait for time to start
  30.   while (!update_time());  //Get the latest time
  31.   previousTime = millis();
  32. }
  33.  
  34. void loop() {
  35.  
  36.   if(millis() - previousTime >= 1000){
  37.     while (!update_time());
  38.     previousTime = millis();
  39.   }
  40.  
  41. }
  42.  
  43. bool update_time(){
  44.   time_t now = time(nullptr);
  45.   struct tm *now_tm;
  46.   int hour,min,second,day,month,year;
  47.   now = time(NULL);
  48.   now_tm = localtime(&now);
  49.   hour = now_tm->tm_hour;
  50.   min  = now_tm->tm_min;
  51.   int weekday = now_tm->tm_wday;
  52.   String week_days[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  53.   String wday = week_days[weekday];
  54.   second = now_tm->tm_sec;
  55.   day    = now_tm->tm_mday;
  56.   month  = now_tm->tm_mon + 1;
  57.   year   = now_tm->tm_year - 100;// YY only no CC element add 1900 for CC element too
  58.   // Needs to be in the format HH:MM:SS Sat 05-07-17
  59.   time_str = (hour<10?"0":"")+String(hour)+":"+(min<10?"0":"")+String(min)+":"+(second<10?"0":"")+String(second)+" "+wday+(day<10?" 0":" ")+String(day)+"-"+(month<10?"0":"")+String(month)+"-"+String(year);
  60.   Serial.println(time_str);
  61.   return true;
  62. }
  63.  
  64. int StartWiFi(const char* ssid, const char* password){
  65.   int connAttempts = 0;
  66.   Serial.println("\r\nConnecting to: "+String(ssid));
  67.   WiFi.begin(ssid, password);
  68.   while (WiFi.status() != WL_CONNECTED ) {
  69.     delay(500);
  70.     Serial.print(".");
  71.     if(connAttempts > 20) return false;
  72.     connAttempts++;
  73.   }
  74.   Serial.print("WiFi connected\r\nIP address: ");
  75.   Serial.println(WiFi.localIP());
  76.   return true;
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement