{"id":827,"date":"2021-05-23T07:15:11","date_gmt":"2021-05-23T06:15:11","guid":{"rendered":"https:\/\/www.kolkataonweb.com\/code-bank\/?p=827"},"modified":"2021-05-22T19:51:56","modified_gmt":"2021-05-22T18:51:56","slug":"esp-8266-and-ntp-onboard-timekeeping","status":"publish","type":"post","link":"https:\/\/www.kolkataonweb.com\/code-bank\/arduino\/esp-8266-and-ntp-onboard-timekeeping\/","title":{"rendered":"ESP-8266 and NTP = onboard timekeeping"},"content":{"rendered":"<p>We can sync the time of a ESP-8266 with a NTP server and then keep time count on the device. It will not need any other additional devices like a DS1307 clock. And once synced with the NTP server it can keep the time even if there is no internet\u00a0 &#8211; very handy.<\/p>\n<p>The time formatting and display functions are standard C++ functions.<\/p>\n<pre class='wp-code-highlight prettyprint'>#include &lt;ESP8266WiFi.h&gt;\r\n\r\n#include \"TinyDHT.h\"\r\n\r\nconst char *ssid     = \"xxxxx\";\r\nconst char *password = \"xxxxxx\";\r\n\r\n\r\n#define DHTPIN11 14          \/\/ What digital pin we're connected to\r\n#define DHTTYPE11 DHT11     \/\/ DHT 11\r\n\r\nDHT dht11(DHTPIN11, DHTTYPE11);\r\n\r\nstruct tm* tm;\r\n\r\nfloat temperature, relHumidity;\r\nString currentTime;\r\n\r\n#define countof(a) (sizeof(a) \/ sizeof(a[0]))\r\n\r\n#define LED 13\r\n\r\nvoid setup() {\r\n\r\n  time_t now = 0;\r\n\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\/\/sync time with the NTP server. First param 19800 seconds = +5:30 for IST, Second Param 0 = DST \r\n configTime(19800, 0, \"pool.ntp.org\");\r\n\r\n\/\/waiting for time to get synced before proceeding. This is not strictly required. Please see after code \r\n  do\r\n  {\r\n    now = time(nullptr);\r\n    delay(500);\r\n    Serial.print(\"*\");\r\n  }while(now &lt; time(nullptr));\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\n\r\nvoid loop() {\r\n  \/\/ Your code here \r\n  time_t now = time(nullptr);\r\n  \r\n  char datestring[30];\r\n  \r\n  strftime(datestring, \r\n            countof(datestring),\r\n            \"%d\/%m\/%Y %I:%M:%S %p\",\r\n            localtime(&amp;now)\r\n            );\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}<\/pre>\n<p><em><strong>configtime<\/strong> listens for connection in the back and will sync as soon as a connection is available. So if a time is not critical for the successive codes or functions then the loop that waits for the sync can be skipped. <\/em><\/p>\n<pre class='wp-code-highlight prettyprint'>configTime(19800, 0, \"pool.ntp.org\");\r\n<em>do \/\/loop and wait for a sync\r\n{\r\n  now = time(nullptr);\r\n  delay(500);\r\n  Serial.print(\"*\");\r\n}while(now &lt; time(nullptr));\r\n<\/em>\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can sync the time of a ESP-8266 with a NTP server and then keep time count on the device. It will not need any other additional devices like a DS1307 clock. And once synced with the NTP server it can keep the time even if there is no internet\u00a0 &#8211; very handy. The time&hellip; <a class=\"more-link\" href=\"https:\/\/www.kolkataonweb.com\/code-bank\/arduino\/esp-8266-and-ntp-onboard-timekeeping\/\">Continue reading <span class=\"screen-reader-text\">ESP-8266 and NTP = onboard timekeeping<\/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":[36,302,20,303,301,15,68,304],"class_list":["post-827","post","type-post","status-publish","format-standard","hentry","category-arduino","category-esp-8266","tag-arduino-clock","tag-arduino-ntp","tag-esp-8266","tag-esp-ntp","tag-esp-time-keeping","tag-esp-01","tag-esp-12","tag-iot-ntp","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/827","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=827"}],"version-history":[{"count":3,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/827\/revisions"}],"predecessor-version":[{"id":831,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/827\/revisions\/831"}],"wp:attachment":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/media?parent=827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/categories?post=827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/tags?post=827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}