From 00d717d9dd00e7c99843d2c7149d898e1cae1823 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 2 Nov 2020 16:46:50 +0000 Subject: [PATCH] use time library and daylight savings --- esp_clock.ino | 100 ++++++++++++++++++-------------------------------- 1 file changed, 36 insertions(+), 64 deletions(-) diff --git a/esp_clock.ino b/esp_clock.ino index 9997dcc..4174502 100644 --- a/esp_clock.ino +++ b/esp_clock.ino @@ -1,7 +1,4 @@ -//########################################################################## -// Includes and defines -//########################################################################## - +#include #include #include #include @@ -10,41 +7,38 @@ #include #include // Support for the Backpack FeatherWing #include // Adafruit's graphics library +#include +#include +#include -#define TIME_24_HOUR #define DISPLAY_ADDRESS 0x70 #define BRIGHTNESS 1 -//########################################################################## -// Globals -//########################################################################## - // Create display object Adafruit_7segment clockDisplay = Adafruit_7segment(); -int hours = 0; // Track hours -int minutes = 0; // Track minutes -int seconds = 0; // Track seconds -int tzOffset = +2; // Time zone offset (-4 = US Eastern time) +// Central European Time (Frankfurt, Paris) +TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; // Central European Summer Time +TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; // Central European Standard Time +Timezone CE(CEST, CET); -#ifdef TIME_24_HOUR -byte displayHours[24] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}; -#else -byte displayHours[24] = {12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; -#endif +int currentHour = -1; -int status = WL_IDLE_STATUS; -bool shouldUpdate = true; String currentSSID; String currentPsk; -ESP8266WiFiMulti wifiMulti; WiFiUDP ntpUDP; -NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", tzOffset*3600); +NTPClient timeClient(ntpUDP, "europe.pool.ntp.org"); + +void displayTime(); +void displayColon(bool on); +void setupOTA(); +void printWiFiStatus(); void setup() { Serial.begin(9600); // Start the serial console + ESP8266WiFiMulti wifiMulti; wifiMulti.addAP("vulturul", "***REMOVED***"); wifiMulti.addAP("Miracle", "***REMOVED***"); @@ -55,7 +49,6 @@ void setup() { Serial.println("Connected to network."); currentSSID = WiFi.SSID(); currentPsk = WiFi.psk(); - printWiFiStatus(); // Display WiFi status data setupOTA(); @@ -68,15 +61,23 @@ void setup() { void loop() { ArduinoOTA.handle(); - if (shouldUpdate && WiFi.status() == WL_CONNECTED && timeClient.forceUpdate()) { - shouldUpdate = false; - hours = timeClient.getHours(); - minutes = timeClient.getMinutes(); - seconds = timeClient.getSeconds(); - Serial.println(timeClient.getFormattedTime()); + if ((currentHour != hour())) { + if (WiFi.status() != WL_CONNECTED) { + WiFi.forceSleepWake(); + WiFi.begin(currentSSID.c_str(), currentPsk.c_str()); + Serial.println("Reconnecting to WiFi netowrk..."); + for (int i = 0; i < 4; i++) { + delay(1000); + } + } + printWiFiStatus(); + if (WiFi.status() == WL_CONNECTED && timeClient.forceUpdate()) { + time_t newTime = CE.toLocal(timeClient.getEpochTime()); + setTime(newTime); + Serial.println(asctime(localtime(&newTime))); + } WiFi.forceSleepBegin(); - } else { - incrementTime(); + currentHour = hour(); } displayTime(); displayColon(true); @@ -85,35 +86,10 @@ void loop() { delay(500); } -void incrementTime() { - if (++seconds >= 60) { - seconds = 0; - if (++minutes == 60) { - minutes = 0; - Serial.println("Minutes set to zero - should query NTP on next loop()"); - if (++hours == 24) hours = 0; - } - if ((minutes == 0)) { - WiFi.forceSleepWake(); - WiFi.begin(currentSSID.c_str(), currentPsk.c_str()); - for (int i = 0; i < 4; i++) { - Serial.println("Reconnecting to WiFi netowrk..."); - delay(1000); - seconds++; - } - if (WiFi.status() == WL_CONNECTED) { - shouldUpdate = true; - } else { - WiFi.forceSleepBegin(); - } - printWiFiStatus(); - } - } -} - void displayTime() { - int displayHour = displayHours[hours]; - int displayValue = displayHour*100 + minutes; + int displayHour = hour(); + int displayMinute = minute(); + int displayValue = displayHour*100 + displayMinute; // Print the time on the display clockDisplay.print(displayValue, DEC); @@ -123,7 +99,7 @@ void displayTime() { // which can look confusing. Go in and explicitly add these zeros. if (displayHour == 0) { clockDisplay.writeDigitNum(1, 0); - if (minutes < 10) { + if (displayMinute < 10) { clockDisplay.writeDigitNum(3, 0); } } @@ -131,7 +107,6 @@ void displayTime() { void displayColon(bool on) { clockDisplay.drawColon(on); - clockDisplay.writeDisplay(); } @@ -154,9 +129,6 @@ void setupOTA() { else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); ArduinoOTA.begin(); - Serial.println("Ready"); - Serial.print("IP address: "); - Serial.println(WiFi.localIP()); } void printWiFiStatus() {