├── .gitignore ├── WifiPass.h.config ├── README.md └── NTP_RTC_Sync.ino /.gitignore: -------------------------------------------------------------------------------- 1 | *DS_Store 2 | WifiPass.h 3 | -------------------------------------------------------------------------------- /WifiPass.h.config: -------------------------------------------------------------------------------- 1 | const char* ssid = "WIFI_SSID_HERE"; 2 | const char* pass = "WIFI_PASSWORD_HERE"; 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sync RTC clock with NTP 2 | 3 | ## Libs 4 | 5 | You must have the libs: 6 | 7 | * [Adafruit RTCLib](https://github.com/adafruit/RTClib) 8 | * [Arduino NTP Client](https://github.com/arduino-libraries/NTPClient) 9 | 10 | You can install from Arduino/ Manage Libraries menu 11 | 12 | ## Wifi 13 | 14 | Copy `WifiPass.h.config` to `WifiPass.h` and change the `ssid` and `password` to your wifi network information. 15 | 16 | ## Parameters 17 | Change the `NTP_SERVER` to a suitable server. 18 | 19 | ```C 20 | #define NTP_SERVER "0.us.pool.ntp.org" 21 | ``` 22 | 23 | Change the GMT time zone `GMT_TIME_ZONE` you can add a signed number 24 | 25 | ```C 26 | #define GMT_TIME_ZONE -7 27 | ``` 28 | 29 | You can force the time sync after the first time changing the `FORCE_RTC_UPDATE` to a random number between 0-255 30 | ```C 31 | #define FORCE_RTC_UPDATE 2 32 | ``` 33 | -------------------------------------------------------------------------------- /NTP_RTC_Sync.ino: -------------------------------------------------------------------------------- 1 | #include "WifiPass.h" 2 | #include 3 | #include //NTPClient by Arduino 4 | #include 5 | #include 6 | #include 7 | #include //RTClib by Adafruit 8 | 9 | //closest NTP Server 10 | #define NTP_SERVER "0.us.pool.ntp.org" 11 | 12 | //GMT Time Zone with sign 13 | #define GMT_TIME_ZONE -7 14 | 15 | //Force RTC update and store on EEPROM 16 | //change this to a random number between 0-255 to force time update 17 | #define FORCE_RTC_UPDATE 2 18 | 19 | char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 20 | 21 | RTC_DS3231 rtc; 22 | WiFiUDP ntpUDP; 23 | 24 | // You can specify the time server pool and the offset, (in seconds) 25 | // additionaly you can specify the update interval (in milliseconds). 26 | NTPClient timeClient(ntpUDP, NTP_SERVER, GMT_TIME_ZONE * 3600 , 60000); 27 | 28 | int timeUpdated = 0; 29 | 30 | void setup() { 31 | Serial.begin(9600); 32 | EEPROM.begin(4); 33 | 34 | if (!rtc.begin()) { 35 | Serial.println("Couldn't find RTC"); 36 | while (1); 37 | } 38 | 39 | Serial.println("Waiting to Start...."); 40 | delay(5000); 41 | 42 | //Read the EEPROM to check if time has been synced 43 | 44 | byte addvalue = EEPROM.read(timeUpdated); 45 | Serial.print("EEPROM: "); 46 | Serial.print(addvalue); 47 | Serial.print(" == "); 48 | Serial.print(FORCE_RTC_UPDATE); 49 | Serial.println(" ?"); 50 | if (addvalue != FORCE_RTC_UPDATE) { 51 | //if(true == false){ 52 | //time hasn' it been setup 53 | Serial.println("Forcing Time Update"); 54 | syncTime(); 55 | Serial.println("Updating EEPROM.."); 56 | EEPROM.write(timeUpdated, FORCE_RTC_UPDATE); 57 | EEPROM.commit(); 58 | 59 | } else { 60 | Serial.println("Time has been updated before...EEPROM CHECKED"); 61 | Serial.print("EEPROM: "); 62 | Serial.print(addvalue); 63 | Serial.print(" = "); 64 | Serial.print(FORCE_RTC_UPDATE); 65 | Serial.println("!"); 66 | } 67 | 68 | 69 | 70 | } 71 | 72 | void syncTime(void) { 73 | 74 | //Connect to Wifi 75 | //SSID and Password on WifiPass.h file 76 | WiFi.begin(ssid, password); 77 | while ( WiFi.status() != WL_CONNECTED ) { 78 | delay ( 500 ); 79 | Serial.print ( "." ); 80 | } 81 | 82 | timeClient.begin(); 83 | timeClient.update(); 84 | 85 | long actualTime = timeClient.getEpochTime(); 86 | Serial.print("Internet Epoch Time: "); 87 | Serial.println(actualTime); 88 | rtc.adjust(DateTime(actualTime)); 89 | 90 | 91 | 92 | //Turn Off WIFI after update 93 | WiFi.disconnect(); 94 | WiFi.mode(WIFI_OFF); 95 | WiFi.forceSleepBegin(); 96 | 97 | } 98 | 99 | 100 | void loop () { 101 | DateTime now = rtc.now(); 102 | 103 | Serial.print(now.year(), DEC); 104 | Serial.print('/'); 105 | Serial.print(now.month(), DEC); 106 | Serial.print('/'); 107 | Serial.print(now.day(), DEC); 108 | Serial.print(" ("); 109 | Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); 110 | Serial.print(") "); 111 | Serial.print(now.hour(), DEC); 112 | Serial.print(':'); 113 | Serial.print(now.minute(), DEC); 114 | Serial.print(':'); 115 | Serial.print(now.second(), DEC); 116 | Serial.println(); 117 | 118 | Serial.print(" since midnight 1/1/1970 = "); 119 | Serial.print(now.unixtime()); 120 | Serial.print("s = "); 121 | Serial.print(now.unixtime() / 86400L); 122 | Serial.println("d"); 123 | 124 | // calculate a date which is 7 days and 30 seconds into the future 125 | DateTime future (now + TimeSpan(7, 12, 30, 6)); 126 | 127 | Serial.print(" now + 7d + 30s: "); 128 | Serial.print(future.year(), DEC); 129 | Serial.print('/'); 130 | Serial.print(future.month(), DEC); 131 | Serial.print('/'); 132 | Serial.print(future.day(), DEC); 133 | Serial.print(' '); 134 | Serial.print(future.hour(), DEC); 135 | Serial.print(':'); 136 | Serial.print(future.minute(), DEC); 137 | Serial.print(':'); 138 | Serial.print(future.second(), DEC); 139 | Serial.println(); 140 | 141 | Serial.println(); 142 | delay(1000); 143 | } 144 | --------------------------------------------------------------------------------