{"id":1821,"date":"2019-02-01T13:27:35","date_gmt":"2019-02-01T21:27:35","guid":{"rendered":"http:\/\/www.donluc.com\/?p=1821"},"modified":"2019-02-01T13:27:35","modified_gmt":"2019-02-01T21:27:35","slug":"project-10-esp8266-thing-precision-rtc-mk04","status":"publish","type":"post","link":"https:\/\/www.donluc.com\/?p=1821","title":{"rendered":"Project #10: ESP8266 Thing \u2013 Precision RTC &#8211; Mk04"},"content":{"rendered":"<div style=\"width: 720px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-1821-1\" width=\"720\" height=\"406\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03.mp4?_=1\" \/><a href=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03.mp4\">https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03.mp4<\/a><\/video><\/div>\n<p><a href=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03a.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03a.png\" alt=\"\" width=\"720\" height=\"626\" class=\"alignnone size-full wp-image-1823\" srcset=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03a.png 720w, https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03a-300x261.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03b.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03b.png\" alt=\"\" width=\"720\" height=\"540\" class=\"alignnone size-full wp-image-1824\" srcset=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03b.png 720w, https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03b-300x225.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03c.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03c.png\" alt=\"\" width=\"720\" height=\"1281\" class=\"alignnone size-full wp-image-1825\" srcset=\"https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03c.png 720w, https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03c-169x300.png 169w, https:\/\/www.donluc.com\/wp-content\/uploads\/2019\/02\/DL1901Mk03c-576x1024.png 576w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<p><strong>DS3231 Precision RTC FeatherWing<\/strong><\/p>\n<p>A Feather board without ambition is a Feather board without FeatherWings! This is the DS3231 Precision RTC FeatherWing: it adds an extremely accurate I2C-integrated Real Time Clock (RTC) with a Temperature Compensated Crystal Oscillator to any Feather main board. This RTC is the most precise you can get in a small, low power package. Most RTCs use an external 32kHz timing crystal that is used to keep time with low current draw.<\/p>\n<p>With a CR1220 12mm lithium battery plugged into the top of the FeatherWing, you can get years of precision timekeeping, even when main power is lost. Great for datalogging and clocks, or anything where you need to really know the time.<\/p>\n<p><strong>DonLuc1901Mk03<\/strong><\/p>\n<p>1 x SparkFun ESP8266 Thing<br \/>\n1 x SparkFun FTDI Basic Breakout &#8211; 3.3V<br \/>\n1 x DS3231 Precision RTC FeatherWing<br \/>\n1 x RHT03 Humidity and Temperature Sensor<br \/>\n6 x Jumper Wires 3&#8243; M\/M<br \/>\n3 x Jumper Wires 6&#8243; M\/M<br \/>\n1 x Full-Size Breadboard<br \/>\n1 x SparkFun Cerberus USB Cable<\/p>\n<p><strong>SparkFun ESP8266 Thing<\/strong><\/p>\n<p>LG1 &#8211; Digital 5<br \/>\nRHT &#8211; Digital 4<br \/>\nSDA &#8211; Digital 2<br \/>\nSCL &#8211; Digital 14<br \/>\nGND &#8211; GND<br \/>\nVIN &#8211; +3.3V<\/p>\n<p><strong>DonLuc1901Mk03p.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"DonLuc1901Mk03p.ino\">\r\n\/\/ ***** Don Luc Electronics *****\r\n\/\/ Software Version Information\r\n\/\/ Project #10: SparkFun ESP8266 Thing \u2013 DS3231 Precision RTC  - Mk04\r\n\/\/ 01-03\r\n\/\/ DonLuc1901Mk03p.ino 01-03\r\n\/\/ SparkFun ESP8266 Thing\r\n\/\/ DS3231 Precision RTC \r\n\/\/ RHT03 Humidity and Temperature Sensor\r\n\r\n\/\/ Include Library Code\r\n\/\/ WiFi\r\n#include &lt;ESP8266WiFi.h&gt;\r\n\/\/ RHT Humidity and Temperature Sensor\r\n#include &lt;SparkFun_RHT03.h&gt;\r\n\/\/ DS3231 Precision RTC \r\n#include &lt;RTClib.h&gt;\r\n#include &lt;Wire.h&gt;\r\n\r\n\/\/ WiFi Definitions \r\nconst char WiFiAPPSK[] = &quot;donlucmk01&quot;;\r\n\r\n\/\/ Pin Definitions \r\nconst int LED_PIN = 5;                  \/\/ Thing&#039;s onboard, green LED\r\nconst int ANALOG_PIN = A0;              \/\/ The only analog pin on the Thing\r\nconst int DIGITAL_PIN = 12;             \/\/ Digital pin to be read\r\n\r\n\/\/ WiFi\r\nWiFiServer server(80);\r\n\r\n\/\/ RHT Humidity and Temperature Sensor\r\nconst int RHT03_DATA_PIN = 4;           \/\/ RHT03 data pin Digital 4\r\nRHT03 rht;                              \/\/ This creates a RTH03 object, which we&#039;ll use to interact with the sensor\r\nfloat latestHumidity;\r\nfloat latestTempC;\r\nfloat latestTempF;\r\n\r\n\/\/ DS3231 Precision RTC \r\nRTC_DS3231 RTC;\r\nString sDate;\r\nString sTime;\r\n  \r\nvoid loop() \r\n{\r\n\r\n  \/\/ RHT03 Humidity and Temperature Sensor\r\n  isRHT03();\r\n\r\n  \/\/ DS3231 Precision RTC \r\n  timeRTC();\r\n  \r\n  \/\/ Check if a client has connected\r\n  WiFiClient client = server.available();\r\n  if (!client) {\r\n    return;\r\n  }\r\n\r\n  \/\/ Read the first line of the request\r\n  String req = client.readStringUntil(&#039;\\r&#039;);\r\n  Serial.println(req);\r\n  client.flush();\r\n\r\n  \/\/ Match the request\r\n  int val = -1; \/\/ We&#039;ll use &#039;val&#039; to keep track of both the request type (read\/set) and value if set.\r\n  if (req.indexOf(&quot;\/led\/0&quot;) != -1)\r\n    val = 0; \/\/ Will write LED low\r\n  else if (req.indexOf(&quot;\/led\/1&quot;) != -1)\r\n    val = 1; \/\/ Will write LED high\r\n  else if (req.indexOf(&quot;\/read&quot;) != -1)\r\n    val = -2; \/\/ Will print pin reads\r\n  \/\/ Otherwise request will be invalid. We&#039;ll say as much in HTML\r\n  \/\/ Set GPIO5 according to the request\r\n  if (val &gt;= 0)\r\n    digitalWrite(LED_PIN, val);\r\n\r\n  client.flush();\r\n\r\n  \/\/ Prepare the response. Start with the common header:\r\n  String s = &quot;HTTP\/1.1 200 OK\\r\\n&quot;;\r\n  s += &quot;Content-Type: text\/html\\r\\n\\r\\n&quot;;\r\n  s += &quot;&lt;!DOCTYPE HTML&gt;\\r\\n&lt;html&gt;\\r\\n&quot;;\r\n  \/\/ If we&#039;re setting the LED, print out a message saying we did\r\n  if (val &gt;= 0)\r\n  {\r\n    s += &quot;LED is now &quot;;\r\n    s += (val)?&quot;on&quot;:&quot;off&quot;;\r\n  }\r\n  else if (val == -2)\r\n  { \/\/ If we&#039;re reading pins, print out those values:\r\n    s += &quot;Date = &quot;;\r\n    s += sDate;\r\n    s += &quot;&lt;br&gt;&quot;;\r\n    s += &quot;Time = &quot;;\r\n    s += sTime;\r\n    s += &quot;&lt;br&gt;&quot;;\r\n    s += &quot;Analog Pin = &quot;;\r\n    s += String(analogRead(ANALOG_PIN));\r\n    s += &quot;&lt;br&gt;&quot;;                                       \/\/ Go to the next line.\r\n    s += &quot;Digital Pin 12 = &quot;;\r\n    s += String(digitalRead(DIGITAL_PIN));\r\n    s += &quot;&lt;br&gt;&quot;;                                       \/\/ Go to the next line.\r\n    s += &quot;Humidity and Temperature&quot;;\r\n    s += &quot;&lt;br&gt;&quot;;                                       \/\/ Go to the next line.    \r\n    s += &quot;Humidity : &quot;;\r\n    s += String(latestHumidity);                       \/\/ Humidity\r\n    s += &quot;%&quot;;\r\n    s += &quot;&lt;br&gt;&quot;; \/\/ Go to the next line.\r\n    s += &quot;Celsius: &quot;;\r\n    s += String(latestTempC);                          \/\/  Temperature *C\r\n    s += &quot;*C&quot;;\r\n    s += &quot;&lt;br&gt;&quot;; \/\/ Go to the next line.\r\n    s += &quot;Fahrenheit: &quot;;\r\n    s += String(latestTempF);                          \/\/ Temperature *F \r\n    s += &quot;*F&quot;;        \r\n  }\r\n  else\r\n  {\r\n    s += &quot;Invalid Request.&lt;br&gt; Try \/led\/1, \/led\/0, or \/read.&quot;;\r\n  }\r\n  s += &quot;&lt;\/html&gt;\\n&quot;;\r\n\r\n  \/\/ Send the response to the client\r\n  client.print(s);\r\n  delay(1);\r\n  Serial.println(&quot;Client disonnected&quot;);\r\n\r\n  \/\/ The client will actually be disconnected when the function returns and &#039;client&#039; object is detroyed\r\n  \r\n}\r\n<\/pre>\n<p><strong>getRHT.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"getRHT.ino\">\r\n\/\/ RHT03 Humidity and Temperature Sensor\r\nvoid isRHT03(){\r\n\r\n  \/\/ Call rht.update() to get new humidity and temperature values from the sensor.\r\n  int updateRet = rht.update();\r\n\r\n  \/\/ The humidity(), tempC(), and tempF() functions can be called -- after \r\n  \/\/ a successful update() -- to get the last humidity and temperature value \r\n  latestHumidity = rht.humidity();\r\n  latestTempC = rht.tempC();\r\n  latestTempF = rht.tempF();\r\n  \r\n}\r\n<\/pre>\n<p><strong>getRTCDS3231.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"getRTCDS3231.ino\">\r\n\/\/ DS3231 Precision RTC \r\nvoid setupRTC() {\r\n\r\n  \/\/ DS3231 Precision RTC   \r\n  RTC.begin();\r\n  if (! RTC.begin()) {\r\n    while (1);\r\n  }\r\n  \r\n  DateTime now = RTC.now();\r\n\r\n  if (RTC.lostPower()) {\r\n    \/\/ Following line sets the RTC to the date &amp; time this sketch was compiled\r\n    RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));\r\n  }\r\n  \r\n}\r\n\/\/ timeRTC\r\nvoid timeRTC() {\r\n\r\n    \/\/ DS3231 Precision RTC \r\n    sDate = &quot;&quot;;\r\n    sTime = &quot;&quot;;\r\n    \r\n    DateTime now = RTC.now();\r\n\r\n    \/\/ sData\r\n    sDate += String(now.year(), DEC);\r\n    sDate += &quot;\/&quot;;\r\n    sDate += String(now.month(), DEC);\r\n    sDate += &quot;\/&quot;;\r\n    sDate += String(now.day(), DEC);\r\n    \r\n    \/\/ sTime\r\n    sTime += String(now.hour(), DEC);\r\n    sTime += &quot;:&quot;;\r\n    sTime += String(now.minute(), DEC);\r\n    sTime += &quot;:&quot;;\r\n    sTime += String(now.second(), DEC);\r\n\r\n}\r\n<\/pre>\n<p><strong>setWiFi.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"setWiFi.ino\">\r\n\/\/ WiFi\r\nvoid setupWiFi()\r\n{\r\n  \r\n  \/\/ WiFi mode WIFI_AP\r\n  WiFi.mode(WIFI_AP);\r\n\r\n  \/\/ Append the last two bytes of the MAC (HEX&#039;d) to &quot;Thing-&quot;:\r\n  uint8_t mac[WL_MAC_ADDR_LENGTH];\r\n  WiFi.softAPmacAddress(mac);\r\n  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +\r\n                 String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);\r\n  macID.toUpperCase();\r\n  String AP_NameString = &quot;ESP8266 Thing &quot; + macID;\r\n\r\n  char AP_NameChar[AP_NameString.length() + 1];\r\n  memset(AP_NameChar, 0, AP_NameString.length() + 1);\r\n\r\n  for (int i=0; i&lt;AP_NameString.length(); i++)\r\n    AP_NameChar[i] = AP_NameString.charAt(i);\r\n\r\n  WiFi.softAP(AP_NameChar, WiFiAPPSK);\r\n  \r\n}\r\n\/\/ init Hardware\r\nvoid initHardware()\r\n{\r\n\r\n  \/\/ Serial\r\n  Serial.begin(115200);\r\n  \/\/ LED Green\r\n  pinMode(DIGITAL_PIN, INPUT_PULLUP);\r\n  pinMode(LED_PIN, OUTPUT);\r\n  digitalWrite(LED_PIN, LOW);\r\n  \/\/ RHT03 Humidity and Temperature Sensor\r\n  \/\/ Call rht.begin() to initialize the sensor and our data pin\r\n  rht.begin(RHT03_DATA_PIN);\r\n  \/\/ DS3231 Precision RTC \r\n  setupRTC();\r\n  \r\n}\r\n<\/pre>\n<p><strong>setup.ino<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"setup.ino\">\r\n\/\/ Setup\r\nvoid setup() \r\n{\r\n\r\n  \/\/ Hardware\r\n  initHardware();\r\n  \/\/ WiFi\r\n  setupWiFi();\r\n  server.begin();\r\n  \r\n}\r\n<\/pre>\n<p><strong>Don Luc<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>DS3231 Precision RTC FeatherWing A Feather board without ambition is a Feather board without FeatherWings! This is the DS3231 Precision RTC FeatherWing: it adds an extremely accurate I2C-integrated Real Time Clock (RTC) with a Temperature Compensated Crystal Oscillator to any Feather main board. This RTC is the most precise you can get in a small, &#8230; <a title=\"Project #10: ESP8266 Thing \u2013 Precision RTC &#8211; Mk04\" class=\"read-more\" href=\"https:\/\/www.donluc.com\/?p=1821\" aria-label=\"Read more about Project #10: ESP8266 Thing \u2013 Precision RTC &#8211; Mk04\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77,47,38,78,59,5,79,10,43],"tags":[],"class_list":["post-1821","post","type-post","status-publish","format-standard","hentry","category-esp8266","category-adafruit","category-digitalelectronics","category-esp8266-microcontrollers","category-fritzing","category-microcontrollers","category-program_esp8266","category-projects","category-sparkfun"],"_links":{"self":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts\/1821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1821"}],"version-history":[{"count":3,"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts\/1821\/revisions"}],"predecessor-version":[{"id":4215,"href":"https:\/\/www.donluc.com\/index.php?rest_route=\/wp\/v2\/posts\/1821\/revisions\/4215"}],"wp:attachment":[{"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.donluc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}