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 @@
|
||||
//##########################################################################
|
||||
// Includes and defines
|
||||
//##########################################################################
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WiFiMulti.h>
|
||||
#include <NTPClient.h>
|
||||
@ -10,41 +7,38 @@
|
||||
#include <ArduinoOTA.h>
|
||||
#include <Adafruit_LEDBackpack.h> // Support for the Backpack FeatherWing
|
||||
#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 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() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user