{"id":823,"date":"2021-05-22T15:59:16","date_gmt":"2021-05-22T14:59:16","guid":{"rendered":"https:\/\/www.kolkataonweb.com\/code-bank\/?p=823"},"modified":"2021-05-22T16:10:29","modified_gmt":"2021-05-22T15:10:29","slug":"sync-ds1302-with-ntp","status":"publish","type":"post","link":"https:\/\/www.kolkataonweb.com\/code-bank\/arduino\/sync-ds1302-with-ntp\/","title":{"rendered":"Sync Ds1302 with NTP"},"content":{"rendered":"<p>This code first syncs the DS1302 chip with a NTP server and then reads time from the DS1302 chip. Every time the system is booted the clock is synced with a NTP server.<\/p>\n<pre class='wp-code-highlight prettyprint'>#include &lt;ESP8266WiFi.h&gt;\r\n\r\n#include \"TinyDHT.h\" \/\/ Adafruit library <a href=\"https:\/\/github.com\/adafruit\/TinyDHT\">https:\/\/github.com\/adafruit\/TinyDHT<\/a>\r\n\r\n#include &lt;ThreeWire.h&gt; \/\/ needed by rtc\r\n#include &lt;RtcDS1302.h&gt; \/\/ Makuna library <a href=\"https:\/\/github.com\/Makuna\/Rtc\">https:\/\/github.com\/Makuna\/Rtc<\/a>\r\n\r\nconst char *ssid     = \"xxxxxxxxxxxx\";\r\nconst char *password = \"xxxx\";\r\n\r\n\r\n#define DHTPIN11 14          \/\/ Pin to which DHT11 connected to\r\n#define DHTTYPE11 DHT11     \/\/ DHT 11\r\n\r\nDHT dht11(DHTPIN11, DHTTYPE11);\r\n\r\nThreeWire myWire(5,16,4); \/\/ IO, SCLK, CE\/Reset\r\nRtcDS1302&lt;ThreeWire&gt; Rtc(myWire);\r\nRtcDateTime currentTimeEpoch;\r\n\r\n#define countof(a) (sizeof(a) \/ sizeof(a[0]))\r\n\r\nfloat temperature, relHumidity;\r\nString currentTime;\r\n\r\nvoid initialiseRTC()\r\n{\r\n    \/\/now = time(nullptr);\r\n    \/\/Serial.print(now);\r\n    \r\n    \/*if (!Rtc.IsDateTimeValid()) \r\n    {\r\n        \/\/ Common Causes:\r\n        \/\/    1) first time you ran and the device wasn't running yet\r\n        \/\/    2) the battery on the device is low or even missing\r\n\r\n        Serial.println(\"RTC lost confidence in the DateTime!\");\r\n        \/\/by default the RTC library assumes time from year 2000, but EPOCH starts from 1970. Severs returns EPOCH and that is the standard. So below converting the EPOCH to equivalent RTC date.   \r\n        currentTimeEpoch.InitWithEpoch32Time(time(nullptr));\r\n        Rtc.SetDateTime(currentTimeEpoch);\r\n    }*\/\r\n\r\n    if (!Rtc.GetIsRunning())\r\n    {\r\n        Serial.println(\"RTC was not actively running, starting now\");\r\n        Rtc.SetIsRunning(true);\r\n    }\r\n\r\n    if (Rtc.GetIsWriteProtected())\r\n    {\r\n        Serial.println(\"RTC was write protected, enabling writing\");\r\n        Rtc.SetIsWriteProtected(false);\r\n    }\r\n\r\n    \/\/here we are always syncing the clock when the device is booted. \r\n    currentTimeEpoch.InitWithEpoch32Time(time(nullptr));\r\n    Rtc.SetDateTime(currentTimeEpoch);\r\n    \r\n    if (!Rtc.GetIsWriteProtected())\r\n    {\r\n        Serial.println(\"RTC was not write protected, disabling writing\");\r\n        Rtc.SetIsWriteProtected(true);\r\n    }\r\n\r\n}\r\n\r\n#define LED 13\r\n\r\nvoid setup() {\r\n  time_t now = 0;\r\n  \/\/ Initialize serial and wait for port to open:\r\n  Serial.begin(9600);\r\n  \/\/ This delay gives the chance to wait for a Serial Monitor without blocking if none is found\r\n  delay(1500); \r\n\r\n  Serial.print(\"Connecting to \");\r\n  Serial.println(ssid);\r\n  WiFi.mode(WIFI_STA);\r\n  WiFi.begin(ssid, password);\r\n  while (WiFi.status() != WL_CONNECTED) {\r\n    delay(500);\r\n    Serial.print(\".\");\r\n  }\r\n  Serial.println(\"\");\r\n\r\n  Serial.println(\"WiFi connected\");\r\n  \r\n\/\/ here we are setting up the NTP call. Consequently we can just call time() to get the time. \r\n\/\/the first parameter is Timezone in seconds, second parameter is DST in seconds and third parameter is the NTP server. \r\n  configTime(19800, 0, \"pool.ntp.org\"); \r\n\r\n\/\/wait till the clock is synced, else wrong time will get set in the DS1302 chip \r\n  do   \r\n  {\r\n    \/\/ time(nullptr) - returns the time that was received from NTP server\r\n    now = time(nullptr);\r\n    delay(500);\r\n    Serial.print(\"*\");\r\n  }while(now &lt; time(nullptr));\r\n\r\n  Rtc.Begin(); \/\/initialise the RTC library\r\n  initialiseRTC(); \/\/ this will set the NTP time in DS1302 clock chip,\r\n  \r\n  pinMode(LED, OUTPUT);\r\n  \r\n  pinMode(DHTPIN11, INPUT);\r\n  dht11.begin();\r\n\r\n  onLedIndicatorChange();\r\n}\r\n\r\nvoid loop() {\r\n  \/\/ Your code here \r\n  RtcDateTime now = Rtc.GetDateTime(); \/\/ reading the time from DS1302 clock module\r\n  char datestring[20];\r\n  \r\n  snprintf_P(datestring, \r\n            countof(datestring),\r\n            PSTR(\"%02u\/%02u\/%04u %02u:%02u:%02u\"),\r\n            now.Month(),\r\n            now.Day(),\r\n            now.Year(),\r\n            now.Hour(),\r\n            now.Minute(),\r\n            now.Second() );\r\n   \r\n  currentTime = datestring; \r\n  Serial.println(currentTime);\r\n  temperature = dht11.readTemperature();\r\n  delay(200);\r\n  relHumidity = dht11.readHumidity();\r\n  delay(200);\r\n\r\n}\r\n\r\nvoid onLedIndicatorChange() {\r\n  \/\/ Do something\r\n  int ledIndicator = 1; \r\n  digitalWrite(LED, ledIndicator);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This code first syncs the DS1302 chip with a NTP server and then reads time from the DS1302 chip. Every time the system is booted the clock is synced with a NTP server. #include &lt;ESP8266WiFi.h&gt; #include &#8220;TinyDHT.h&#8221; \/\/ Adafruit library https:\/\/github.com\/adafruit\/TinyDHT #include &lt;ThreeWire.h&gt; \/\/ needed by rtc #include &lt;RtcDS1302.h&gt; \/\/ Makuna library https:\/\/github.com\/Makuna\/Rtc const char&hellip; <a class=\"more-link\" href=\"https:\/\/www.kolkataonweb.com\/code-bank\/arduino\/sync-ds1302-with-ntp\/\">Continue reading <span class=\"screen-reader-text\">Sync Ds1302 with NTP<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,64],"tags":[302,300,298,299,20,303,301,68,65,297,304],"class_list":["post-823","post","type-post","status-publish","format-standard","hentry","category-arduino","category-esp-8266","tag-arduino-ntp","tag-arduino-time-keeping","tag-ds1302","tag-ds1302-clock-module","tag-esp-8266","tag-esp-ntp","tag-esp-time-keeping","tag-esp-12","tag-esp-12e","tag-iot","tag-iot-ntp","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/comments?post=823"}],"version-history":[{"count":3,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/823\/revisions"}],"predecessor-version":[{"id":826,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/823\/revisions\/826"}],"wp:attachment":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/media?parent=823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/categories?post=823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/tags?post=823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}