Advertisement
Krejzi_Dark

nodemcu RTC Clock

Jan 29th, 2021
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.33 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WiFiMulti.h>
  3. #include <NTPClient.h>
  4. #include <WiFiUdp.h>
  5. #include <Wire.h>
  6. #include <U8x8lib.h>
  7. #include <virtuabotixRTC.h>
  8.  
  9. const char *ssid = "ssid";
  10. const char *pw   = "pass";
  11. const long timezoneOffset = 1 * 60 * 60; // ? hours * 60 * 60
  12.  
  13. const char          *ntpServer  = "pool.ntp.org"; // change it to local NTP server if needed
  14. const unsigned long updateDelay = 60000;         // update time every 15 min (15 * 60 * 1000 = 9000000)
  15. const unsigned long retryDelay  = 5000;           // retry 5 sec later if time query failed
  16.  
  17. //Week Days
  18. String weekDays[7] = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
  19. String currentDays[7] = {"NDZ", "PON", "WT", "SR", "CZW", "PT", "SOB"};
  20.  
  21. //Month names
  22. String months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  23.  
  24. unsigned long lastUpdatedTime = updateDelay * -1;
  25. unsigned int  second_prev = 0;
  26. bool          colon_switch = false;
  27.  
  28. ESP8266WiFiMulti WiFiMulti;
  29. WiFiUDP ntpUDP;
  30. NTPClient timeClient(ntpUDP, ntpServer);
  31.  
  32. //https://github.com/olikraus/u8g2/wiki/u8x8reference
  33. U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(SCL, SDA, U8X8_PIN_NONE);
  34.  
  35. // Init the DS1302
  36. virtuabotixRTC myRTC(14, 13, 16);
  37.  
  38. void setup() {
  39.  
  40.   Serial.begin(9600);
  41.   u8x8.begin();
  42.   u8x8.setFont(u8x8_font_7x14B_1x2_f);
  43.   u8x8.drawString(0, 0, "Connecting");
  44.   u8x8.drawString(0, 3, "  to WiFi...");
  45.  
  46.   Serial.println("Connecting to WiFi...");
  47.   WiFiMulti.addAP(ssid, pw);
  48.   while (WiFiMulti.run() != WL_CONNECTED) {
  49.     delay(200);
  50.     Serial.print(".");
  51.   }
  52.   Serial.println("\nConnected.");
  53.  
  54.   u8x8.clear();
  55.  
  56.   timeClient.setTimeOffset(timezoneOffset);
  57.   timeClient.begin();
  58.  
  59.   //myRTC.setDS1302Time(00, 59, 23, 6, 10, 1, 2014);
  60. }
  61.  
  62. void loop() {
  63.   myRTC.updateTime();
  64.  
  65.   /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66.   // Start printing elements as individuals
  67.   Serial.print("RTC Date / Time: ");
  68.   if (myRTC.dayofmonth < 10) Serial.print("0"); Serial.print(myRTC.dayofmonth);
  69.   Serial.print("/");
  70.   if (myRTC.month < 10) Serial.print("0"); Serial.print(myRTC.month);
  71.   Serial.print("/");
  72.   Serial.print(myRTC.year);
  73.   Serial.print("  ");
  74.   if (myRTC.hours < 10) Serial.print("0"); Serial.print(myRTC.hours);
  75.   Serial.print(":");
  76.   if (myRTC.minutes < 10) Serial.print("0"); Serial.print(myRTC.minutes);
  77.   Serial.print(":");
  78.   if (myRTC.seconds < 10) Serial.print("0"); Serial.println(myRTC.seconds);
  79.  
  80.   String string_year = String(myRTC.year);
  81.   String currentDayName = currentDays[myRTC.dayofweek];
  82.  
  83.   if (WiFiMulti.run() == WL_CONNECTED && millis() - lastUpdatedTime >= updateDelay) {
  84.     bool updated = timeClient.update();
  85.     Serial.println("NTP time updated.");
  86.  
  87.     unsigned long epochTime = timeClient.getEpochTime();
  88.     String formattedTime = timeClient.getFormattedTime();
  89.  
  90.     int currentHour = timeClient.getHours();
  91.     int currentMinute = timeClient.getMinutes();
  92.     int currentSecond = timeClient.getSeconds();
  93.  
  94.     int currentDay = timeClient.getDay();
  95.  
  96.     String weekDay = weekDays[timeClient.getDay()];
  97.  
  98.     //Get a time structure
  99.     struct tm *ptm = gmtime ((time_t *)&epochTime);
  100.     int monthDay = ptm->tm_mday;
  101.     int currentMonth = ptm->tm_mon + 1;
  102.     String currentMonthName = months[currentMonth - 1];
  103.     int currentYear = ptm->tm_year + 1900;
  104.  
  105.     Serial.print("Epoch Time: ");
  106.     Serial.println(epochTime);
  107.  
  108.     Serial.print("Formatted Time: ");
  109.     Serial.println(formattedTime);
  110.  
  111.     Serial.print("Hour: ");
  112.     Serial.println(currentHour);
  113.  
  114.     Serial.print("Minutes: ");
  115.     Serial.println(currentMinute);
  116.  
  117.     Serial.print("Seconds: ");
  118.     Serial.println(currentSecond);
  119.  
  120.     Serial.print("Day number: ");
  121.     Serial.println(currentDay);
  122.  
  123.     Serial.print("Week Day: ");
  124.     Serial.println(weekDay);
  125.  
  126.     Serial.print("Month day: ");
  127.     Serial.println(monthDay);
  128.  
  129.     Serial.print("Month: ");
  130.     Serial.println(currentMonth);
  131.  
  132.     Serial.print("Month name: ");
  133.     Serial.println(currentMonthName);
  134.  
  135.     Serial.print("Year: ");
  136.     Serial.println(currentYear);
  137.  
  138.     //Print complete date:
  139.     String currentDate = (monthDay < 10 ? "0" : "") + String(monthDay) + "/" + (currentMonth < 10 ? "0" : "") + String(currentMonth) + "/" + String(currentYear) + "  " +
  140.                          (currentHour < 10 ? "0" : "") + String(currentHour) + ":" + (currentMinute < 10 ? "0" : "") + String(currentMinute) + ":" + (currentSecond < 10 ? "0" : "") + String(currentSecond);
  141.     Serial.print("NTP Date / Time: ");
  142.     Serial.println(currentDate);
  143.  
  144.     if (updated) {
  145.       lastUpdatedTime = millis();
  146.  
  147.       myRTC.setDS1302Time(currentSecond, currentMinute, currentHour, currentDay, monthDay, currentMonth, currentYear);
  148.       Serial.println("RTC time updated.");
  149.  
  150.     } else {
  151.       Serial.println("Failed to update time. Waiting for retry...");
  152.       lastUpdatedTime = millis() - updateDelay + retryDelay;
  153.     }
  154.   } else {
  155.     Serial.println("WiFi disconnected!");
  156.   }
  157.  
  158.   unsigned long t = millis();
  159.  
  160.   if (myRTC.seconds != second_prev) colon_switch = !colon_switch;
  161.  
  162.   String fDate = (myRTC.dayofmonth < 10 ? "0" : "") + String(myRTC.dayofmonth) + "/" + (myRTC.month < 10 ? "0" : "") + String(myRTC.month);
  163.   String fTime = (myRTC.hours < 10 ? "0" : "") + String(myRTC.hours) + (colon_switch ? ":" : " ") + (myRTC.minutes < 10 ? "0" : "") + String(myRTC.minutes);
  164.  
  165.   u8x8.setFont(u8x8_font_lucasarts_scumm_subtitle_o_2x2_f);
  166.   u8x8.drawString(1, 0, strcpy(new char[fDate.length() + 1], fDate.c_str()));
  167.   u8x8.setFont(u8x8_font_pxplusibmcga_f);
  168.   u8x8.drawString(12, 0, strcpy(new char[string_year.length() + 1], string_year.c_str()));
  169.   u8x8.setFont(u8x8_font_victoriamedium8_r);
  170.   u8x8.drawString(12, 1, strcpy(new char[currentDayName.length() + 1], currentDayName.c_str()));
  171.   u8x8.setFont(u8x8_font_inb33_3x6_f);
  172.   u8x8.drawString(1, 2, strcpy(new char[fTime.length() + 1], fTime.c_str()));
  173.  
  174.  
  175.   second_prev = myRTC.seconds;
  176.  
  177.   int diff = millis() - t;
  178.   delay(diff >= 0 ? (500 - (millis() - t)) : 0);
  179.  
  180. }#include <ESP8266WiFi.h>
  181. #include <ESP8266WiFiMulti.h>
  182. #include <NTPClient.h>
  183. #include <WiFiUdp.h>
  184. #include <Wire.h>
  185. #include <U8x8lib.h>
  186. #include <virtuabotixRTC.h>
  187.  
  188. //const char *ssid = "HUAWEI_P20_lite";
  189. //const char *pw   = "141456789";
  190.  
  191. const char *ssid = "KTLNET";
  192. const char *pw   = "skrzyzowanyszczurnataborecie";
  193. const long timezoneOffset = 1 * 60 * 60; // ? hours * 60 * 60
  194.  
  195. const char          *ntpServer  = "pool.ntp.org"; // change it to local NTP server if needed
  196. const unsigned long updateDelay = 60000;         // update time every 15 min (15 * 60 * 1000)
  197. const unsigned long retryDelay  = 5000;           // retry 5 sec later if time query failed
  198.  
  199. //Week Days
  200. String weekDays[7] = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
  201. String currentDays[7] = {"NDZ", "PON", "WT", "SR", "CZW", "PT", "SOB"};
  202.  
  203. //Month names
  204. String months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  205.  
  206. unsigned long lastUpdatedTime = updateDelay * -1;
  207. unsigned int  second_prev = 0;
  208. bool          colon_switch = false;
  209.  
  210. ESP8266WiFiMulti WiFiMulti;
  211. WiFiUDP ntpUDP;
  212. NTPClient timeClient(ntpUDP, ntpServer);
  213.  
  214. //https://github.com/olikraus/u8g2/wiki/u8x8reference
  215. U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(SCL, SDA, U8X8_PIN_NONE);
  216.  
  217. // Init the DS1302
  218. virtuabotixRTC myRTC(14, 13, 16);
  219.  
  220. void setup() {
  221.  
  222.   Serial.begin(9600);
  223.   u8x8.begin();
  224.   u8x8.setFont(u8x8_font_7x14B_1x2_f);
  225.   u8x8.drawString(0, 0, "Connecting");
  226.   u8x8.drawString(0, 3, "  to WiFi...");
  227.  
  228.   Serial.println("Connecting to WiFi...");
  229.   WiFiMulti.addAP(ssid, pw);
  230.   while (WiFiMulti.run() != WL_CONNECTED) {
  231.     delay(200);
  232.     Serial.print(".");
  233.   }
  234.   Serial.println("\nConnected.");
  235.  
  236.   u8x8.clear();
  237.  
  238.   timeClient.setTimeOffset(timezoneOffset);
  239.   timeClient.begin();
  240.  
  241.   //myRTC.setDS1302Time(00, 59, 23, 6, 10, 1, 2014);
  242. }
  243.  
  244. void loop() {
  245.   myRTC.updateTime();
  246.  
  247.   /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  248.   // Start printing elements as individuals
  249.   Serial.print("RTC Date / Time: ");
  250.   if (myRTC.dayofmonth < 10) Serial.print("0"); Serial.print(myRTC.dayofmonth);
  251.   Serial.print("/");
  252.   if (myRTC.month < 10) Serial.print("0"); Serial.print(myRTC.month);
  253.   Serial.print("/");
  254.   Serial.print(myRTC.year);
  255.   Serial.print("  ");
  256.   if (myRTC.hours < 10) Serial.print("0"); Serial.print(myRTC.hours);
  257.   Serial.print(":");
  258.   if (myRTC.minutes < 10) Serial.print("0"); Serial.print(myRTC.minutes);
  259.   Serial.print(":");
  260.   if (myRTC.seconds < 10) Serial.print("0"); Serial.println(myRTC.seconds);
  261.  
  262.   String string_year = String(myRTC.year);
  263.   String currentDayName = currentDays[myRTC.dayofweek];
  264.  
  265.   if (WiFiMulti.run() == WL_CONNECTED && millis() - lastUpdatedTime >= updateDelay) {
  266.     bool updated = timeClient.update();
  267.     Serial.println("NTP time updated.");
  268.  
  269.     unsigned long epochTime = timeClient.getEpochTime();
  270.     String formattedTime = timeClient.getFormattedTime();
  271.  
  272.     int currentHour = timeClient.getHours();
  273.     int currentMinute = timeClient.getMinutes();
  274.     int currentSecond = timeClient.getSeconds();
  275.  
  276.     int currentDay = timeClient.getDay();
  277.  
  278.     String weekDay = weekDays[timeClient.getDay()];
  279.  
  280.     //Get a time structure
  281.     struct tm *ptm = gmtime ((time_t *)&epochTime);
  282.     int monthDay = ptm->tm_mday;
  283.     int currentMonth = ptm->tm_mon + 1;
  284.     String currentMonthName = months[currentMonth - 1];
  285.     int currentYear = ptm->tm_year + 1900;
  286.  
  287.     Serial.print("Epoch Time: ");
  288.     Serial.println(epochTime);
  289.  
  290.     Serial.print("Formatted Time: ");
  291.     Serial.println(formattedTime);
  292.  
  293.     Serial.print("Hour: ");
  294.     Serial.println(currentHour);
  295.  
  296.     Serial.print("Minutes: ");
  297.     Serial.println(currentMinute);
  298.  
  299.     Serial.print("Seconds: ");
  300.     Serial.println(currentSecond);
  301.  
  302.     Serial.print("Day number: ");
  303.     Serial.println(currentDay);
  304.  
  305.     Serial.print("Week Day: ");
  306.     Serial.println(weekDay);
  307.  
  308.     Serial.print("Month day: ");
  309.     Serial.println(monthDay);
  310.  
  311.     Serial.print("Month: ");
  312.     Serial.println(currentMonth);
  313.  
  314.     Serial.print("Month name: ");
  315.     Serial.println(currentMonthName);
  316.  
  317.     Serial.print("Year: ");
  318.     Serial.println(currentYear);
  319.  
  320.     //Print complete date:
  321.     String currentDate = (monthDay < 10 ? "0" : "") + String(monthDay) + "/" + (currentMonth < 10 ? "0" : "") + String(currentMonth) + "/" + String(currentYear) + "  " +
  322.                          (currentHour < 10 ? "0" : "") + String(currentHour) + ":" + (currentMinute < 10 ? "0" : "") + String(currentMinute) + ":" + (currentSecond < 10 ? "0" : "") + String(currentSecond);
  323.     Serial.print("NTP Date / Time: ");
  324.     Serial.println(currentDate);
  325.  
  326.     if (updated) {
  327.       lastUpdatedTime = millis();
  328.  
  329.       myRTC.setDS1302Time(currentSecond, currentMinute, currentHour, currentDay, monthDay, currentMonth, currentYear);
  330.       Serial.println("RTC time updated.");
  331.  
  332.     } else {
  333.       Serial.println("Failed to update time. Waiting for retry...");
  334.       lastUpdatedTime = millis() - updateDelay + retryDelay;
  335.     }
  336.   } else {
  337.     Serial.println("WiFi disconnected!");
  338.   }
  339.  
  340.   unsigned long t = millis();
  341.  
  342.   if (myRTC.seconds != second_prev) colon_switch = !colon_switch;
  343.  
  344.   String fDate = (myRTC.dayofmonth < 10 ? "0" : "") + String(myRTC.dayofmonth) + "/" + (myRTC.month < 10 ? "0" : "") + String(myRTC.month);
  345.   String fTime = (myRTC.hours < 10 ? "0" : "") + String(myRTC.hours) + (colon_switch ? ":" : " ") + (myRTC.minutes < 10 ? "0" : "") + String(myRTC.minutes);
  346.  
  347.   u8x8.setFont(u8x8_font_lucasarts_scumm_subtitle_o_2x2_f);
  348.   u8x8.drawString(1, 0, strcpy(new char[fDate.length() + 1], fDate.c_str()));
  349.   u8x8.setFont(u8x8_font_pxplusibmcga_f);
  350.   u8x8.drawString(12, 0, strcpy(new char[string_year.length() + 1], string_year.c_str()));
  351.   u8x8.setFont(u8x8_font_victoriamedium8_r);
  352.   u8x8.drawString(12, 1, strcpy(new char[currentDayName.length() + 1], currentDayName.c_str()));
  353.   u8x8.setFont(u8x8_font_inb33_3x6_f);
  354.   u8x8.drawString(1, 2, strcpy(new char[fTime.length() + 1], fTime.c_str()));
  355.  
  356.  
  357.   second_prev = myRTC.seconds;
  358.  
  359.   int diff = millis() - t;
  360.   delay(diff >= 0 ? (500 - (millis() - t)) : 0);
  361.  
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement