macca-nz

NTP Example V1 (Uses char string)

Mar 23rd, 2022 (edited)
680
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*************************************************************************************************************************************************
  2.  *  TITLE: This sketch connects to a local WiFi network, connects to a network server and obtains the network time. It then converts this to a
  3.  *  readable format and displays it to the serial terminal and an OLED module. Refer to the video and post for more information.
  4.  *
  5.  *  By Frenoy Osburn
  6.  *  YouTube Video: https://youtu.be/3LkKYtQqzKo
  7.  *  BnBe Post: https://www.bitsnblobs.com/network-time-using-esp8266
  8.  *************************************************************************************************************************************************/
  9.  
  10.   /********************************************************************************************************************
  11.  *  Board Settings:
  12.  *  Board: "WeMos D1 R1 or Mini"
  13.  *  Upload Speed: "921600"
  14.  *  CPU Frequency: "80MHz"
  15.  *  Flash Size: "4MB (FS:@MB OTA:~1019KB)"
  16.  *  Debug Port: "Disabled"
  17.  *  Debug Level: "None"
  18.  *  VTables: "Flash"
  19.  *  IwIP Variant: "v2 Lower Memory"
  20.  *  Exception: "Legacy (new can return nullptr)"
  21.  *  Erase Flash: "Only Sketch"
  22.  *  SSL Support: "All SSL ciphers (most compatible)"
  23.  *  COM Port: Depends *On Your System*
  24.  *********************************************************************************************************************/
  25.  
  26.  /*
  27.   This is an example file for using the time function in ESP8266 or ESP32 tu get NTP time
  28.   It offers two functions:
  29.  
  30.   - getNTPtime(struct tm * info, uint32_t ms) where info is a structure which contains time
  31.   information and ms is the time the service waits till it gets a response from NTP.
  32.   Each time you cann this function it calls NTP over the net.
  33.  
  34.   If you do not want to call an NTP service every second, you can use
  35.   - getTimeReducedTraffic(int ms) where ms is the the time between two physical NTP server calls. Betwwn these calls,
  36.   the time structure is updated with the (inaccurate) timer. If you call NTP every few minutes you should be ok
  37.  
  38.   The time structure is called tm and has the following values:
  39.  
  40.   Definition of struct tm:
  41.   Member  Type  Meaning Range
  42.   tm_sec  int seconds after the minute  0-61*
  43.   tm_min  int minutes after the hour  0-59
  44.   tm_hour int hours since midnight  0-23
  45.   tm_mday int day of the month  1-31
  46.   tm_mon  int months since January  0-11
  47.   tm_year int years since 1900
  48.   tm_wday int days since Sunday 0-6
  49.   tm_yday int days since January 1  0-365
  50.   tm_isdst  int Daylight Saving Time flag
  51.  
  52.   because the values are somehow awkwardly defined, I introduce a function makeHumanreadable() where all values are adjusted according normal numbering.
  53.   e.g. January is month 1 and not 0 And Sunday or Monday is weekday 1 not 0 (according definition of MONDAYFIRST)
  54.  
  55.   Showtime is an example on how you can use the time in your sketch
  56.  
  57.   The functions are inspired by work of G6EJD ( https://www.youtube.com/channel/UCgtlqH_lkMdIa4jZLItcsTg )
  58. */
  59.  
  60.  
  61. #include <WiFi.h>
  62. #include <time.h>
  63.  
  64. const char* ssid       = "masuku";
  65. const char* password   = "mypasswort";
  66.  
  67. const char* NTP_SERVER = "pool.ntp.org"; //ch.pool.ntp.org
  68. const char* TZ_INFO    = "PST-8";  // enter your time zone (https://remotemonitoringsystems.ca/time-zone-abbreviations.php)
  69.  
  70. tm timeinfo;
  71. time_t now;
  72. long unsigned lastNTPtime;
  73. unsigned long lastEntryTime;
  74.  
  75. void setup()
  76. {
  77.  
  78.   Serial.begin(115200);
  79.   Serial.println("\n\nNTP Time Test\n");
  80.   WiFi.begin(ssid, password);
  81.  
  82.   Serial.print("Connecting to network");
  83.   int counter = 0;
  84.   while (WiFi.status() != WL_CONNECTED)
  85.   {
  86.     delay(200);    
  87.     if (++counter > 100)
  88.       ESP.restart();
  89.     Serial.print( "." );
  90.   }
  91.   Serial.println("\nWiFi connected\n\n");
  92.  
  93.   configTime(0, 0, NTP_SERVER);
  94.   // See https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv for Timezone codes for your region
  95.   setenv("TZ", TZ_INFO, 1);
  96.  
  97.   if (getNTPtime(10))
  98.   {  
  99.     // wait up to 10sec to sync
  100.   }
  101.   else
  102.   {
  103.     Serial.println("Time not set");
  104.     ESP.restart();
  105.   }
  106.   showTime(&timeinfo);
  107.   lastNTPtime = time(&now);
  108.   lastEntryTime = millis();
  109. }
  110.  
  111. void loop()
  112. {
  113.   getNTPtime(10);
  114.   showTime(&timeinfo);
  115.   delay(1000);
  116. }
  117.  
  118. bool getNTPtime(int sec)
  119. {
  120.   {
  121.     uint32_t start = millis();
  122.     do
  123.     {
  124.       time(&now);
  125.       localtime_r(&now, &timeinfo);
  126.       delay(10);
  127.     } while (((millis() - start) <= (1000 * sec)) && (timeinfo.tm_year < (2016 - 1900)));
  128.    
  129.     if (timeinfo.tm_year <= (2016 - 1900))
  130.         return false;  // the NTP call was not successful
  131.    
  132.     //Serial.print("Time Now: ");  
  133.     //Serial.println(now);
  134.   }
  135.   return true;
  136. }
  137.  
  138. void showTime(tm *localTime)
  139. {
  140.   char time_output[30];
  141.   sprintf(time_output, "%02d/%02d/%02d  %02d:%02d:%02d", localTime->tm_mday, localTime->tm_mon + 1, localTime->tm_year - 100, localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
  142.   Serial.println(time_output);
  143. }
  144.  
  145. char * getDOW(uint8_t tm_wday)
  146. {
  147.   switch(tm_wday)
  148.   {
  149.     case 0:
  150.       return "Sunday";
  151.       break;
  152.    
  153.     case 1:
  154.       return "Monday";
  155.       break;
  156.  
  157.     case 2:
  158.       return "Tuesday";
  159.       break;
  160.  
  161.     case 3:
  162.       return "Wednesday";
  163.       break;
  164.  
  165.     case 4:
  166.       return "Thursday";
  167.       break;
  168.  
  169.     case 5:
  170.       return "Friday";
  171.       break;
  172.  
  173.     case 6:
  174.       return "Saturday";
  175.       break;
  176.  
  177.     default:
  178.       return "Error";
  179.       break;
  180.   }
  181. }
RAW Paste Data Copied