use time library and daylight savings
This commit is contained in:
parent
e65a2846c7
commit
00d717d9dd
100
esp_clock.ino
100
esp_clock.ino
@ -1,7 +1,4 @@
|
|||||||
//##########################################################################
|
#include <Arduino.h>
|
||||||
// Includes and defines
|
|
||||||
//##########################################################################
|
|
||||||
|
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266WiFiMulti.h>
|
#include <ESP8266WiFiMulti.h>
|
||||||
#include <NTPClient.h>
|
#include <NTPClient.h>
|
||||||
@ -10,41 +7,38 @@
|
|||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
#include <Adafruit_LEDBackpack.h> // Support for the Backpack FeatherWing
|
#include <Adafruit_LEDBackpack.h> // Support for the Backpack FeatherWing
|
||||||
#include <Adafruit_GFX.h> // Adafruit's graphics library
|
#include <Adafruit_GFX.h> // Adafruit's graphics library
|
||||||
|
#include <SPI.h>
|
||||||
|
#include <Timezone.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#define TIME_24_HOUR
|
|
||||||
#define DISPLAY_ADDRESS 0x70
|
#define DISPLAY_ADDRESS 0x70
|
||||||
#define BRIGHTNESS 1
|
#define BRIGHTNESS 1
|
||||||
|
|
||||||
//##########################################################################
|
|
||||||
// Globals
|
|
||||||
//##########################################################################
|
|
||||||
|
|
||||||
// Create display object
|
// Create display object
|
||||||
Adafruit_7segment clockDisplay = Adafruit_7segment();
|
Adafruit_7segment clockDisplay = Adafruit_7segment();
|
||||||
|
|
||||||
int hours = 0; // Track hours
|
// Central European Time (Frankfurt, Paris)
|
||||||
int minutes = 0; // Track minutes
|
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; // Central European Summer Time
|
||||||
int seconds = 0; // Track seconds
|
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; // Central European Standard Time
|
||||||
int tzOffset = +2; // Time zone offset (-4 = US Eastern time)
|
Timezone CE(CEST, CET);
|
||||||
|
|
||||||
#ifdef TIME_24_HOUR
|
int currentHour = -1;
|
||||||
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 status = WL_IDLE_STATUS;
|
|
||||||
bool shouldUpdate = true;
|
|
||||||
String currentSSID;
|
String currentSSID;
|
||||||
String currentPsk;
|
String currentPsk;
|
||||||
|
|
||||||
ESP8266WiFiMulti wifiMulti;
|
|
||||||
WiFiUDP ntpUDP;
|
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() {
|
void setup() {
|
||||||
Serial.begin(9600); // Start the serial console
|
Serial.begin(9600); // Start the serial console
|
||||||
|
|
||||||
|
ESP8266WiFiMulti wifiMulti;
|
||||||
wifiMulti.addAP("vulturul", "***REMOVED***");
|
wifiMulti.addAP("vulturul", "***REMOVED***");
|
||||||
wifiMulti.addAP("Miracle", "***REMOVED***");
|
wifiMulti.addAP("Miracle", "***REMOVED***");
|
||||||
|
|
||||||
@ -55,7 +49,6 @@ void setup() {
|
|||||||
Serial.println("Connected to network.");
|
Serial.println("Connected to network.");
|
||||||
currentSSID = WiFi.SSID();
|
currentSSID = WiFi.SSID();
|
||||||
currentPsk = WiFi.psk();
|
currentPsk = WiFi.psk();
|
||||||
printWiFiStatus(); // Display WiFi status data
|
|
||||||
|
|
||||||
setupOTA();
|
setupOTA();
|
||||||
|
|
||||||
@ -68,15 +61,23 @@ void setup() {
|
|||||||
void loop() {
|
void loop() {
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
|
|
||||||
if (shouldUpdate && WiFi.status() == WL_CONNECTED && timeClient.forceUpdate()) {
|
if ((currentHour != hour())) {
|
||||||
shouldUpdate = false;
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
hours = timeClient.getHours();
|
WiFi.forceSleepWake();
|
||||||
minutes = timeClient.getMinutes();
|
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
|
||||||
seconds = timeClient.getSeconds();
|
Serial.println("Reconnecting to WiFi netowrk...");
|
||||||
Serial.println(timeClient.getFormattedTime());
|
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();
|
WiFi.forceSleepBegin();
|
||||||
} else {
|
currentHour = hour();
|
||||||
incrementTime();
|
|
||||||
}
|
}
|
||||||
displayTime();
|
displayTime();
|
||||||
displayColon(true);
|
displayColon(true);
|
||||||
@ -85,35 +86,10 @@ void loop() {
|
|||||||
delay(500);
|
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() {
|
void displayTime() {
|
||||||
int displayHour = displayHours[hours];
|
int displayHour = hour();
|
||||||
int displayValue = displayHour*100 + minutes;
|
int displayMinute = minute();
|
||||||
|
int displayValue = displayHour*100 + displayMinute;
|
||||||
|
|
||||||
// Print the time on the display
|
// Print the time on the display
|
||||||
clockDisplay.print(displayValue, DEC);
|
clockDisplay.print(displayValue, DEC);
|
||||||
@ -123,7 +99,7 @@ void displayTime() {
|
|||||||
// which can look confusing. Go in and explicitly add these zeros.
|
// which can look confusing. Go in and explicitly add these zeros.
|
||||||
if (displayHour == 0) {
|
if (displayHour == 0) {
|
||||||
clockDisplay.writeDigitNum(1, 0);
|
clockDisplay.writeDigitNum(1, 0);
|
||||||
if (minutes < 10) {
|
if (displayMinute < 10) {
|
||||||
clockDisplay.writeDigitNum(3, 0);
|
clockDisplay.writeDigitNum(3, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,7 +107,6 @@ void displayTime() {
|
|||||||
|
|
||||||
void displayColon(bool on) {
|
void displayColon(bool on) {
|
||||||
clockDisplay.drawColon(on);
|
clockDisplay.drawColon(on);
|
||||||
|
|
||||||
clockDisplay.writeDisplay();
|
clockDisplay.writeDisplay();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,9 +129,6 @@ void setupOTA() {
|
|||||||
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
||||||
});
|
});
|
||||||
ArduinoOTA.begin();
|
ArduinoOTA.begin();
|
||||||
Serial.println("Ready");
|
|
||||||
Serial.print("IP address: ");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void printWiFiStatus() {
|
void printWiFiStatus() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user