Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266WiFiMulti.h>
- #include <NTPClient.h>
- #include <WiFiUdp.h>
- #include <Wire.h>
- #include <U8x8lib.h>
- #include <virtuabotixRTC.h>
- const char *ssid = "ssid";
- const char *pw = "pass";
- const long timezoneOffset = 1 * 60 * 60; // ? hours * 60 * 60
- const char *ntpServer = "pool.ntp.org"; // change it to local NTP server if needed
- const unsigned long updateDelay = 60000; // update time every 15 min (15 * 60 * 1000 = 9000000)
- const unsigned long retryDelay = 5000; // retry 5 sec later if time query failed
- //Week Days
- String weekDays[7] = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
- String currentDays[7] = {"NDZ", "PON", "WT", "SR", "CZW", "PT", "SOB"};
- //Month names
- String months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
- unsigned long lastUpdatedTime = updateDelay * -1;
- unsigned int second_prev = 0;
- bool colon_switch = false;
- ESP8266WiFiMulti WiFiMulti;
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, ntpServer);
- //https://github.com/olikraus/u8g2/wiki/u8x8reference
- U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(SCL, SDA, U8X8_PIN_NONE);
- // Init the DS1302
- virtuabotixRTC myRTC(14, 13, 16);
- void setup() {
- Serial.begin(9600);
- u8x8.begin();
- u8x8.setFont(u8x8_font_7x14B_1x2_f);
- u8x8.drawString(0, 0, "Connecting");
- u8x8.drawString(0, 3, " to WiFi...");
- Serial.println("Connecting to WiFi...");
- WiFiMulti.addAP(ssid, pw);
- while (WiFiMulti.run() != WL_CONNECTED) {
- delay(200);
- Serial.print(".");
- }
- Serial.println("\nConnected.");
- u8x8.clear();
- timeClient.setTimeOffset(timezoneOffset);
- timeClient.begin();
- //myRTC.setDS1302Time(00, 59, 23, 6, 10, 1, 2014);
- }
- void loop() {
- myRTC.updateTime();
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Start printing elements as individuals
- Serial.print("RTC Date / Time: ");
- if (myRTC.dayofmonth < 10) Serial.print("0"); Serial.print(myRTC.dayofmonth);
- Serial.print("/");
- if (myRTC.month < 10) Serial.print("0"); Serial.print(myRTC.month);
- Serial.print("/");
- Serial.print(myRTC.year);
- Serial.print(" ");
- if (myRTC.hours < 10) Serial.print("0"); Serial.print(myRTC.hours);
- Serial.print(":");
- if (myRTC.minutes < 10) Serial.print("0"); Serial.print(myRTC.minutes);
- Serial.print(":");
- if (myRTC.seconds < 10) Serial.print("0"); Serial.println(myRTC.seconds);
- String string_year = String(myRTC.year);
- String currentDayName = currentDays[myRTC.dayofweek];
- if (WiFiMulti.run() == WL_CONNECTED && millis() - lastUpdatedTime >= updateDelay) {
- bool updated = timeClient.update();
- Serial.println("NTP time updated.");
- unsigned long epochTime = timeClient.getEpochTime();
- String formattedTime = timeClient.getFormattedTime();
- int currentHour = timeClient.getHours();
- int currentMinute = timeClient.getMinutes();
- int currentSecond = timeClient.getSeconds();
- int currentDay = timeClient.getDay();
- String weekDay = weekDays[timeClient.getDay()];
- //Get a time structure
- struct tm *ptm = gmtime ((time_t *)&epochTime);
- int monthDay = ptm->tm_mday;
- int currentMonth = ptm->tm_mon + 1;
- String currentMonthName = months[currentMonth - 1];
- int currentYear = ptm->tm_year + 1900;
- Serial.print("Epoch Time: ");
- Serial.println(epochTime);
- Serial.print("Formatted Time: ");
- Serial.println(formattedTime);
- Serial.print("Hour: ");
- Serial.println(currentHour);
- Serial.print("Minutes: ");
- Serial.println(currentMinute);
- Serial.print("Seconds: ");
- Serial.println(currentSecond);
- Serial.print("Day number: ");
- Serial.println(currentDay);
- Serial.print("Week Day: ");
- Serial.println(weekDay);
- Serial.print("Month day: ");
- Serial.println(monthDay);
- Serial.print("Month: ");
- Serial.println(currentMonth);
- Serial.print("Month name: ");
- Serial.println(currentMonthName);
- Serial.print("Year: ");
- Serial.println(currentYear);
- //Print complete date:
- String currentDate = (monthDay < 10 ? "0" : "") + String(monthDay) + "/" + (currentMonth < 10 ? "0" : "") + String(currentMonth) + "/" + String(currentYear) + " " +
- (currentHour < 10 ? "0" : "") + String(currentHour) + ":" + (currentMinute < 10 ? "0" : "") + String(currentMinute) + ":" + (currentSecond < 10 ? "0" : "") + String(currentSecond);
- Serial.print("NTP Date / Time: ");
- Serial.println(currentDate);
- if (updated) {
- lastUpdatedTime = millis();
- myRTC.setDS1302Time(currentSecond, currentMinute, currentHour, currentDay, monthDay, currentMonth, currentYear);
- Serial.println("RTC time updated.");
- } else {
- Serial.println("Failed to update time. Waiting for retry...");
- lastUpdatedTime = millis() - updateDelay + retryDelay;
- }
- } else {
- Serial.println("WiFi disconnected!");
- }
- unsigned long t = millis();
- if (myRTC.seconds != second_prev) colon_switch = !colon_switch;
- String fDate = (myRTC.dayofmonth < 10 ? "0" : "") + String(myRTC.dayofmonth) + "/" + (myRTC.month < 10 ? "0" : "") + String(myRTC.month);
- String fTime = (myRTC.hours < 10 ? "0" : "") + String(myRTC.hours) + (colon_switch ? ":" : " ") + (myRTC.minutes < 10 ? "0" : "") + String(myRTC.minutes);
- u8x8.setFont(u8x8_font_lucasarts_scumm_subtitle_o_2x2_f);
- u8x8.drawString(1, 0, strcpy(new char[fDate.length() + 1], fDate.c_str()));
- u8x8.setFont(u8x8_font_pxplusibmcga_f);
- u8x8.drawString(12, 0, strcpy(new char[string_year.length() + 1], string_year.c_str()));
- u8x8.setFont(u8x8_font_victoriamedium8_r);
- u8x8.drawString(12, 1, strcpy(new char[currentDayName.length() + 1], currentDayName.c_str()));
- u8x8.setFont(u8x8_font_inb33_3x6_f);
- u8x8.drawString(1, 2, strcpy(new char[fTime.length() + 1], fTime.c_str()));
- second_prev = myRTC.seconds;
- int diff = millis() - t;
- delay(diff >= 0 ? (500 - (millis() - t)) : 0);
- }#include <ESP8266WiFi.h>
- #include <ESP8266WiFiMulti.h>
- #include <NTPClient.h>
- #include <WiFiUdp.h>
- #include <Wire.h>
- #include <U8x8lib.h>
- #include <virtuabotixRTC.h>
- //const char *ssid = "HUAWEI_P20_lite";
- //const char *pw = "141456789";
- const char *ssid = "KTLNET";
- const char *pw = "skrzyzowanyszczurnataborecie";
- const long timezoneOffset = 1 * 60 * 60; // ? hours * 60 * 60
- const char *ntpServer = "pool.ntp.org"; // change it to local NTP server if needed
- const unsigned long updateDelay = 60000; // update time every 15 min (15 * 60 * 1000)
- const unsigned long retryDelay = 5000; // retry 5 sec later if time query failed
- //Week Days
- String weekDays[7] = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
- String currentDays[7] = {"NDZ", "PON", "WT", "SR", "CZW", "PT", "SOB"};
- //Month names
- String months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
- unsigned long lastUpdatedTime = updateDelay * -1;
- unsigned int second_prev = 0;
- bool colon_switch = false;
- ESP8266WiFiMulti WiFiMulti;
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, ntpServer);
- //https://github.com/olikraus/u8g2/wiki/u8x8reference
- U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(SCL, SDA, U8X8_PIN_NONE);
- // Init the DS1302
- virtuabotixRTC myRTC(14, 13, 16);
- void setup() {
- Serial.begin(9600);
- u8x8.begin();
- u8x8.setFont(u8x8_font_7x14B_1x2_f);
- u8x8.drawString(0, 0, "Connecting");
- u8x8.drawString(0, 3, " to WiFi...");
- Serial.println("Connecting to WiFi...");
- WiFiMulti.addAP(ssid, pw);
- while (WiFiMulti.run() != WL_CONNECTED) {
- delay(200);
- Serial.print(".");
- }
- Serial.println("\nConnected.");
- u8x8.clear();
- timeClient.setTimeOffset(timezoneOffset);
- timeClient.begin();
- //myRTC.setDS1302Time(00, 59, 23, 6, 10, 1, 2014);
- }
- void loop() {
- myRTC.updateTime();
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Start printing elements as individuals
- Serial.print("RTC Date / Time: ");
- if (myRTC.dayofmonth < 10) Serial.print("0"); Serial.print(myRTC.dayofmonth);
- Serial.print("/");
- if (myRTC.month < 10) Serial.print("0"); Serial.print(myRTC.month);
- Serial.print("/");
- Serial.print(myRTC.year);
- Serial.print(" ");
- if (myRTC.hours < 10) Serial.print("0"); Serial.print(myRTC.hours);
- Serial.print(":");
- if (myRTC.minutes < 10) Serial.print("0"); Serial.print(myRTC.minutes);
- Serial.print(":");
- if (myRTC.seconds < 10) Serial.print("0"); Serial.println(myRTC.seconds);
- String string_year = String(myRTC.year);
- String currentDayName = currentDays[myRTC.dayofweek];
- if (WiFiMulti.run() == WL_CONNECTED && millis() - lastUpdatedTime >= updateDelay) {
- bool updated = timeClient.update();
- Serial.println("NTP time updated.");
- unsigned long epochTime = timeClient.getEpochTime();
- String formattedTime = timeClient.getFormattedTime();
- int currentHour = timeClient.getHours();
- int currentMinute = timeClient.getMinutes();
- int currentSecond = timeClient.getSeconds();
- int currentDay = timeClient.getDay();
- String weekDay = weekDays[timeClient.getDay()];
- //Get a time structure
- struct tm *ptm = gmtime ((time_t *)&epochTime);
- int monthDay = ptm->tm_mday;
- int currentMonth = ptm->tm_mon + 1;
- String currentMonthName = months[currentMonth - 1];
- int currentYear = ptm->tm_year + 1900;
- Serial.print("Epoch Time: ");
- Serial.println(epochTime);
- Serial.print("Formatted Time: ");
- Serial.println(formattedTime);
- Serial.print("Hour: ");
- Serial.println(currentHour);
- Serial.print("Minutes: ");
- Serial.println(currentMinute);
- Serial.print("Seconds: ");
- Serial.println(currentSecond);
- Serial.print("Day number: ");
- Serial.println(currentDay);
- Serial.print("Week Day: ");
- Serial.println(weekDay);
- Serial.print("Month day: ");
- Serial.println(monthDay);
- Serial.print("Month: ");
- Serial.println(currentMonth);
- Serial.print("Month name: ");
- Serial.println(currentMonthName);
- Serial.print("Year: ");
- Serial.println(currentYear);
- //Print complete date:
- String currentDate = (monthDay < 10 ? "0" : "") + String(monthDay) + "/" + (currentMonth < 10 ? "0" : "") + String(currentMonth) + "/" + String(currentYear) + " " +
- (currentHour < 10 ? "0" : "") + String(currentHour) + ":" + (currentMinute < 10 ? "0" : "") + String(currentMinute) + ":" + (currentSecond < 10 ? "0" : "") + String(currentSecond);
- Serial.print("NTP Date / Time: ");
- Serial.println(currentDate);
- if (updated) {
- lastUpdatedTime = millis();
- myRTC.setDS1302Time(currentSecond, currentMinute, currentHour, currentDay, monthDay, currentMonth, currentYear);
- Serial.println("RTC time updated.");
- } else {
- Serial.println("Failed to update time. Waiting for retry...");
- lastUpdatedTime = millis() - updateDelay + retryDelay;
- }
- } else {
- Serial.println("WiFi disconnected!");
- }
- unsigned long t = millis();
- if (myRTC.seconds != second_prev) colon_switch = !colon_switch;
- String fDate = (myRTC.dayofmonth < 10 ? "0" : "") + String(myRTC.dayofmonth) + "/" + (myRTC.month < 10 ? "0" : "") + String(myRTC.month);
- String fTime = (myRTC.hours < 10 ? "0" : "") + String(myRTC.hours) + (colon_switch ? ":" : " ") + (myRTC.minutes < 10 ? "0" : "") + String(myRTC.minutes);
- u8x8.setFont(u8x8_font_lucasarts_scumm_subtitle_o_2x2_f);
- u8x8.drawString(1, 0, strcpy(new char[fDate.length() + 1], fDate.c_str()));
- u8x8.setFont(u8x8_font_pxplusibmcga_f);
- u8x8.drawString(12, 0, strcpy(new char[string_year.length() + 1], string_year.c_str()));
- u8x8.setFont(u8x8_font_victoriamedium8_r);
- u8x8.drawString(12, 1, strcpy(new char[currentDayName.length() + 1], currentDayName.c_str()));
- u8x8.setFont(u8x8_font_inb33_3x6_f);
- u8x8.drawString(1, 2, strcpy(new char[fTime.length() + 1], fTime.c_str()));
- second_prev = myRTC.seconds;
- int diff = millis() - t;
- delay(diff >= 0 ? (500 - (millis() - t)) : 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement