├── .gitattributes ├── NTP_Example └── NTP_Example.ino └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /NTP_Example/NTP_Example.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This is an example file for using the time function in ESP8266 or ESP32 tu get NTP time 3 | It offers two functions: 4 | 5 | - getNTPtime(struct tm * info, uint32_t ms) where info is a structure which contains time 6 | information and ms is the time the service waits till it gets a response from NTP. 7 | Each time you cann this function it calls NTP over the net. 8 | 9 | If you do not want to call an NTP service every second, you can use 10 | - getTimeReducedTraffic(int ms) where ms is the the time between two physical NTP server calls. Betwwn these calls, 11 | the time structure is updated with the (inaccurate) timer. If you call NTP every few minutes you should be ok 12 | 13 | The time structure is called tm and has teh following values: 14 | 15 | Definition of struct tm: 16 | Member Type Meaning Range 17 | tm_sec int seconds after the minute 0-61* 18 | tm_min int minutes after the hour 0-59 19 | tm_hour int hours since midnight 0-23 20 | tm_mday int day of the month 1-31 21 | tm_mon int months since January 0-11 22 | tm_year int years since 1900 23 | tm_wday int days since Sunday 0-6 24 | tm_yday int days since January 1 0-365 25 | tm_isdst int Daylight Saving Time flag 26 | 27 | because the values are somhow akwardly defined, I introduce a function makeHumanreadable() where all values are adjusted according normal numbering. 28 | e.g. January is month 1 and not 0 And Sunday or monday is weekday 1 not 0 (according definition of MONDAYFIRST) 29 | 30 | Showtime is an example on how you can use the time in your sketch 31 | 32 | The functions are inspired by work of G6EJD ( https://www.youtube.com/channel/UCgtlqH_lkMdIa4jZLItcsTg ) 33 | 34 | */ 35 | 36 | #ifdef ESP8266 37 | #include 38 | #else 39 | #include 40 | #endif 41 | #include 42 | // #include 43 | 44 | /* 45 | The credentials.h file at least has to contain: 46 | char mySSID[]="your SSID"; 47 | char myPASSWORD[]="your Password"; 48 | 49 | It has to be placed in the libraries folder 50 | 51 | If you do not want a credentials file. delete the line: #include 52 | */ 53 | 54 | #ifdef CREDENTIALS 55 | const char* ssid = mySSID; 56 | const char* password = myPASSWORD; 57 | #else 58 | const char* ssid = "yourSSID"; 59 | const char* password = "YourPassword"; 60 | #endif 61 | 62 | const char* NTP_SERVER = "ch.pool.ntp.org"; 63 | const char* TZ_INFO = "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00"; // enter your time zone (https://remotemonitoringsystems.ca/time-zone-abbreviations.php) 64 | 65 | tm timeinfo; 66 | time_t now; 67 | long unsigned lastNTPtime; 68 | unsigned long lastEntryTime; 69 | 70 | 71 | void setup() { 72 | Serial.begin(115200); 73 | Serial.println("\n\nNTP Time Test\n"); 74 | WiFi.begin(ssid, password); 75 | 76 | int counter = 0; 77 | while (WiFi.status() != WL_CONNECTED) { 78 | delay(200); 79 | if (++counter > 100) ESP.restart(); 80 | Serial.print ( "." ); 81 | } 82 | Serial.println("\n\nWiFi connected\n\n"); 83 | 84 | configTime(0, 0, NTP_SERVER); 85 | // See https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv for Timezone codes for your region 86 | setenv("TZ", TZ_INFO, 1); 87 | 88 | if (getNTPtime(10)) { // wait up to 10sec to sync 89 | } else { 90 | Serial.println("Time not set"); 91 | ESP.restart(); 92 | } 93 | showTime(timeinfo); 94 | lastNTPtime = time(&now); 95 | lastEntryTime = millis(); 96 | } 97 | 98 | 99 | void loop() { 100 | // getTimeReducedTraffic(3600); 101 | getNTPtime(10); 102 | showTime(timeinfo); 103 | delay(1000); 104 | } 105 | 106 | bool getNTPtime(int sec) { 107 | 108 | { 109 | uint32_t start = millis(); 110 | do { 111 | time(&now); 112 | localtime_r(&now, &timeinfo); 113 | Serial.print("."); 114 | delay(10); 115 | } while (((millis() - start) <= (1000 * sec)) && (timeinfo.tm_year < (2016 - 1900))); 116 | if (timeinfo.tm_year <= (2016 - 1900)) return false; // the NTP call was not successful 117 | Serial.print("now "); Serial.println(now); 118 | char time_output[30]; 119 | strftime(time_output, 30, "%a %d-%m-%y %T", localtime(&now)); 120 | Serial.println(time_output); 121 | Serial.println(); 122 | } 123 | return true; 124 | } 125 | 126 | 127 | // This function is obsolete because the time() function only calls the NTP server every hour. So you can always use getNTPtime() 128 | // It can be deleted and only stays here for the video 129 | 130 | /* 131 | void getTimeReducedTraffic(int sec) { 132 | tm *ptm; 133 | if ((millis() - lastEntryTime) < (1000 * sec)) { 134 | now = lastNTPtime + (int)(millis() - lastEntryTime) / 1000; 135 | } else { 136 | lastEntryTime = millis(); 137 | lastNTPtime = time(&now); 138 | now = lastNTPtime; 139 | Serial.println("Get NTP time"); 140 | } 141 | ptm = localtime(&now); 142 | timeinfo = *ptm; 143 | } 144 | */ 145 | 146 | void showTime(tm localTime) { 147 | Serial.print(localTime.tm_mday); 148 | Serial.print('/'); 149 | Serial.print(localTime.tm_mon + 1); 150 | Serial.print('/'); 151 | Serial.print(localTime.tm_year - 100); 152 | Serial.print('-'); 153 | Serial.print(localTime.tm_hour); 154 | Serial.print(':'); 155 | Serial.print(localTime.tm_min); 156 | Serial.print(':'); 157 | Serial.print(localTime.tm_sec); 158 | Serial.print(" Day of Week "); 159 | if (localTime.tm_wday == 0) Serial.println(7); 160 | else Serial.println(localTime.tm_wday); 161 | } 162 | 163 | 164 | /* 165 | // Shorter way of displaying the time 166 | void showTime(tm localTime) { 167 | Serial.printf( 168 | "%04d-%02d-%02d %02d:%02d:%02d, day %d, %s time\n", 169 | localTime.tm_year + 1900, 170 | localTime.tm_mon + 1, 171 | localTime.tm_mday, 172 | localTime.tm_hour, 173 | localTime.tm_min, 174 | localTime.tm_sec, 175 | (localTime.tm_wday > 0 ? localTime.tm_wday : 7 ), 176 | (localTime.tm_isdst == 1 ? "summer" : "standard") 177 | ); 178 | } 179 | 180 | */ 181 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NTP time for ESP8266 and ESP32 2 | NTP time example for ESP8266 and ESP32 based on standard functionality 3 | 4 | See video: https://youtu.be/r2UAmBLBBRM 5 | 6 | Correction: The time() function only calls the NTP server every hour. So there is no need for the function 7 | getTimeReducedTraffic(). The function getNTPtime() does more or less the same... 8 | Thank you, Jose Baars for your testing! 9 | --------------------------------------------------------------------------------